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, |
| 49 | uint32_t width, uint32_t height, size_t maxSize, int format) : |
| 50 | camera3_stream(), |
| 51 | mId(id), |
| 52 | mName(String8::format("Camera3Stream[%d]", id)), |
| 53 | mMaxSize(maxSize), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 54 | mState(STATE_CONSTRUCTED), |
| 55 | mStatusId(StatusTracker::NO_STATUS_ID) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 56 | |
| 57 | camera3_stream::stream_type = type; |
| 58 | camera3_stream::width = width; |
| 59 | camera3_stream::height = height; |
| 60 | camera3_stream::format = format; |
| 61 | camera3_stream::usage = 0; |
| 62 | camera3_stream::max_buffers = 0; |
| 63 | camera3_stream::priv = NULL; |
| 64 | |
| 65 | if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) { |
| 66 | ALOGE("%s: BLOB format with size == 0", __FUNCTION__); |
| 67 | mState = STATE_ERROR; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int Camera3Stream::getId() const { |
| 72 | return mId; |
| 73 | } |
| 74 | |
| 75 | uint32_t Camera3Stream::getWidth() const { |
| 76 | return camera3_stream::width; |
| 77 | } |
| 78 | |
| 79 | uint32_t Camera3Stream::getHeight() const { |
| 80 | return camera3_stream::height; |
| 81 | } |
| 82 | |
| 83 | int Camera3Stream::getFormat() const { |
| 84 | return camera3_stream::format; |
| 85 | } |
| 86 | |
| 87 | camera3_stream* Camera3Stream::startConfiguration() { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 88 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 89 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 90 | status_t res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 91 | |
| 92 | switch (mState) { |
| 93 | case STATE_ERROR: |
| 94 | ALOGE("%s: In error state", __FUNCTION__); |
| 95 | return NULL; |
| 96 | case STATE_CONSTRUCTED: |
| 97 | // OK |
| 98 | break; |
| 99 | case STATE_IN_CONFIG: |
| 100 | case STATE_IN_RECONFIG: |
| 101 | // Can start config again with no trouble; but don't redo |
| 102 | // oldUsage/oldMaxBuffers |
| 103 | return this; |
| 104 | case STATE_CONFIGURED: |
| 105 | if (stream_type == CAMERA3_STREAM_INPUT) { |
| 106 | ALOGE("%s: Cannot configure an input stream twice", |
| 107 | __FUNCTION__); |
| 108 | return NULL; |
| 109 | } else if (hasOutstandingBuffersLocked()) { |
| 110 | ALOGE("%s: Cannot configure stream; has outstanding buffers", |
| 111 | __FUNCTION__); |
| 112 | return NULL; |
| 113 | } |
| 114 | break; |
| 115 | default: |
| 116 | ALOGE("%s: Unknown state %d", __FUNCTION__, mState); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 120 | oldUsage = camera3_stream::usage; |
| 121 | oldMaxBuffers = camera3_stream::max_buffers; |
| 122 | |
| 123 | res = getEndpointUsage(&(camera3_stream::usage)); |
| 124 | if (res != OK) { |
| 125 | ALOGE("%s: Cannot query consumer endpoint usage!", |
| 126 | __FUNCTION__); |
| 127 | return NULL; |
| 128 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 129 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 130 | // Stop tracking if currently doing so |
| 131 | if (mStatusId != StatusTracker::NO_STATUS_ID) { |
| 132 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 133 | if (statusTracker != 0) { |
| 134 | statusTracker->removeComponent(mStatusId); |
| 135 | } |
| 136 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 137 | } |
| 138 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 139 | if (mState == STATE_CONSTRUCTED) { |
| 140 | mState = STATE_IN_CONFIG; |
| 141 | } else { // mState == STATE_CONFIGURED |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 142 | 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] | 143 | mState = STATE_IN_RECONFIG; |
| 144 | } |
| 145 | |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | bool Camera3Stream::isConfiguring() const { |
| 150 | Mutex::Autolock l(mLock); |
| 151 | return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG); |
| 152 | } |
| 153 | |
| 154 | status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 155 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 156 | Mutex::Autolock l(mLock); |
| 157 | switch (mState) { |
| 158 | case STATE_ERROR: |
| 159 | ALOGE("%s: In error state", __FUNCTION__); |
| 160 | return INVALID_OPERATION; |
| 161 | case STATE_IN_CONFIG: |
| 162 | case STATE_IN_RECONFIG: |
| 163 | // OK |
| 164 | break; |
| 165 | case STATE_CONSTRUCTED: |
| 166 | case STATE_CONFIGURED: |
| 167 | ALOGE("%s: Cannot finish configuration that hasn't been started", |
| 168 | __FUNCTION__); |
| 169 | return INVALID_OPERATION; |
| 170 | default: |
| 171 | ALOGE("%s: Unknown state", __FUNCTION__); |
| 172 | return INVALID_OPERATION; |
| 173 | } |
| 174 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 175 | // Register for idle tracking |
| 176 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 177 | if (statusTracker != 0) { |
| 178 | mStatusId = statusTracker->addComponent(); |
| 179 | } |
| 180 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 181 | // Check if the stream configuration is unchanged, and skip reallocation if |
| 182 | // so. As documented in hardware/camera3.h:configure_streams(). |
| 183 | if (mState == STATE_IN_RECONFIG && |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 184 | oldUsage == camera3_stream::usage && |
| 185 | oldMaxBuffers == camera3_stream::max_buffers) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 186 | mState = STATE_CONFIGURED; |
| 187 | return OK; |
| 188 | } |
| 189 | |
| 190 | status_t res; |
| 191 | res = configureQueueLocked(); |
| 192 | if (res != OK) { |
| 193 | ALOGE("%s: Unable to configure stream %d queue: %s (%d)", |
| 194 | __FUNCTION__, mId, strerror(-res), res); |
| 195 | mState = STATE_ERROR; |
| 196 | return res; |
| 197 | } |
| 198 | |
| 199 | res = registerBuffersLocked(hal3Device); |
| 200 | if (res != OK) { |
| 201 | ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)", |
| 202 | __FUNCTION__, strerror(-res), res); |
| 203 | mState = STATE_ERROR; |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | mState = STATE_CONFIGURED; |
| 208 | |
| 209 | return res; |
| 210 | } |
| 211 | |
| 212 | status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) { |
| 213 | ATRACE_CALL(); |
| 214 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 215 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 216 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 217 | // This function should be only called when the stream is configured already. |
| 218 | if (mState != STATE_CONFIGURED) { |
| 219 | ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d", |
| 220 | __FUNCTION__, mId, mState); |
| 221 | return INVALID_OPERATION; |
| 222 | } |
| 223 | |
| 224 | // Wait for new buffer returned back if we are running into the limit. |
| 225 | if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) { |
| 226 | ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.", |
| 227 | __FUNCTION__, camera3_stream::max_buffers); |
| 228 | res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
| 229 | if (res != OK) { |
| 230 | if (res == TIMED_OUT) { |
| 231 | ALOGE("%s: wait for output buffer return timed out after %lldms", __FUNCTION__, |
| 232 | kWaitForBufferDuration / 1000000LL); |
| 233 | } |
| 234 | return res; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | res = getBufferLocked(buffer); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 239 | if (res == OK) { |
| 240 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true); |
| 241 | } |
| 242 | |
| 243 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer, |
| 247 | nsecs_t timestamp) { |
| 248 | ATRACE_CALL(); |
| 249 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 250 | |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 251 | /** |
| 252 | * TODO: Check that the state is valid first. |
| 253 | * |
| 254 | * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED. |
| 255 | * >= HAL3.2 CONFIGURED only |
| 256 | * |
| 257 | * Do this for getBuffer as well. |
| 258 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 259 | status_t res = returnBufferLocked(buffer, timestamp); |
| 260 | if (res == OK) { |
| 261 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 262 | mOutputBufferReturnedSignal.signal(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 268 | status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) { |
| 269 | ATRACE_CALL(); |
| 270 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 271 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 272 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 273 | // This function should be only called when the stream is configured already. |
| 274 | if (mState != STATE_CONFIGURED) { |
| 275 | ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d", |
| 276 | __FUNCTION__, mId, mState); |
| 277 | return INVALID_OPERATION; |
| 278 | } |
| 279 | |
| 280 | // Wait for new buffer returned back if we are running into the limit. |
| 281 | if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) { |
| 282 | ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.", |
| 283 | __FUNCTION__, camera3_stream::max_buffers); |
| 284 | res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
| 285 | if (res != OK) { |
| 286 | if (res == TIMED_OUT) { |
| 287 | ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__, |
| 288 | kWaitForBufferDuration / 1000000LL); |
| 289 | } |
| 290 | return res; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | res = getInputBufferLocked(buffer); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 295 | if (res == OK) { |
| 296 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false); |
| 297 | } |
| 298 | |
| 299 | return res; |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) { |
| 303 | ATRACE_CALL(); |
| 304 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 305 | |
| 306 | status_t res = returnInputBufferLocked(buffer); |
| 307 | if (res == OK) { |
| 308 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 309 | mInputBufferReturnedSignal.signal(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 310 | } |
| 311 | return res; |
| 312 | } |
| 313 | |
| 314 | void Camera3Stream::fireBufferListenersLocked( |
| 315 | const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) { |
| 316 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 317 | |
| 318 | // TODO: finish implementing |
| 319 | |
| 320 | Camera3StreamBufferListener::BufferInfo info = |
| 321 | Camera3StreamBufferListener::BufferInfo(); |
| 322 | info.mOutput = output; |
| 323 | // TODO: rest of fields |
| 324 | |
| 325 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 326 | it != end; |
| 327 | ++it) { |
| 328 | |
| 329 | sp<Camera3StreamBufferListener> listener = it->promote(); |
| 330 | if (listener != 0) { |
| 331 | if (acquired) { |
| 332 | listener->onBufferAcquired(info); |
| 333 | } else { |
| 334 | listener->onBufferReleased(info); |
| 335 | } |
| 336 | } |
| 337 | } |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 340 | bool Camera3Stream::hasOutstandingBuffers() const { |
| 341 | ATRACE_CALL(); |
| 342 | Mutex::Autolock l(mLock); |
| 343 | return hasOutstandingBuffersLocked(); |
| 344 | } |
| 345 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 346 | status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) { |
| 347 | Mutex::Autolock l(mLock); |
| 348 | sp<StatusTracker> oldTracker = mStatusTracker.promote(); |
| 349 | if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { |
| 350 | oldTracker->removeComponent(mStatusId); |
| 351 | } |
| 352 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 353 | mStatusTracker = statusTracker; |
| 354 | |
| 355 | return OK; |
| 356 | } |
| 357 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 358 | status_t Camera3Stream::disconnect() { |
| 359 | ATRACE_CALL(); |
| 360 | Mutex::Autolock l(mLock); |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 361 | ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId); |
| 362 | status_t res = disconnectLocked(); |
| 363 | |
| 364 | if (res == -ENOTCONN) { |
| 365 | // "Already disconnected" -- not an error |
| 366 | return OK; |
| 367 | } else { |
| 368 | return res; |
| 369 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) { |
| 373 | ATRACE_CALL(); |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * >= CAMERA_DEVICE_API_VERSION_3_2: |
| 377 | * |
| 378 | * camera3_device_t->ops->register_stream_buffers() is not called and must |
| 379 | * be NULL. |
| 380 | */ |
| 381 | if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 382 | ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__); |
| 383 | |
Igor Murashkin | c758f22 | 2014-08-19 15:14:29 -0700 | [diff] [blame] | 384 | if (hal3Device->ops->register_stream_buffers != NULL) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 385 | ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; " |
| 386 | "must be set to NULL in camera3_device::ops", __FUNCTION__); |
| 387 | return INVALID_OPERATION; |
| 388 | } else { |
Zhijun He | 13c878f | 2014-05-06 11:33:52 -0700 | [diff] [blame] | 389 | ALOGD("%s: Skipping NULL check for deprecated register_stream_buffers", __FUNCTION__); |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return OK; |
| 393 | } else { |
| 394 | ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__); |
| 395 | } |
| 396 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 397 | status_t res; |
| 398 | |
| 399 | size_t bufferCount = getBufferCountLocked(); |
| 400 | |
| 401 | Vector<buffer_handle_t*> buffers; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 402 | buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 403 | |
| 404 | camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set(); |
| 405 | bufferSet.stream = this; |
| 406 | bufferSet.num_buffers = bufferCount; |
| 407 | bufferSet.buffers = buffers.editArray(); |
| 408 | |
| 409 | Vector<camera3_stream_buffer_t> streamBuffers; |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 410 | streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 411 | |
| 412 | // Register all buffers with the HAL. This means getting all the buffers |
| 413 | // from the stream, providing them to the HAL with the |
| 414 | // register_stream_buffers() method, and then returning them back to the |
| 415 | // stream in the error state, since they won't have valid data. |
| 416 | // |
| 417 | // Only registered buffers can be sent to the HAL. |
| 418 | |
| 419 | uint32_t bufferIdx = 0; |
| 420 | for (; bufferIdx < bufferCount; bufferIdx++) { |
| 421 | res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) ); |
| 422 | if (res != OK) { |
| 423 | ALOGE("%s: Unable to get buffer %d for registration with HAL", |
| 424 | __FUNCTION__, bufferIdx); |
| 425 | // Skip registering, go straight to cleanup |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence); |
Mathias Agopian | d764424 | 2013-05-16 18:07:35 -0700 | [diff] [blame] | 430 | fence->waitForever("Camera3Stream::registerBuffers"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 431 | |
| 432 | buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer; |
| 433 | } |
| 434 | if (bufferIdx == bufferCount) { |
| 435 | // Got all buffers, register with HAL |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 436 | ALOGV("%s: Registering %zu buffers with camera HAL", |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 437 | __FUNCTION__, bufferCount); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 438 | ATRACE_BEGIN("camera3->register_stream_buffers"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 439 | res = hal3Device->ops->register_stream_buffers(hal3Device, |
| 440 | &bufferSet); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 441 | ATRACE_END(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Return all valid buffers to stream, in ERROR state to indicate |
| 445 | // they weren't filled. |
| 446 | for (size_t i = 0; i < bufferIdx; i++) { |
| 447 | streamBuffers.editItemAt(i).release_fence = -1; |
| 448 | streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 449 | returnBufferLocked(streamBuffers[i], 0); |
| 450 | } |
| 451 | |
| 452 | return res; |
| 453 | } |
| 454 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 455 | status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) { |
| 456 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 457 | return INVALID_OPERATION; |
| 458 | } |
| 459 | status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &, |
| 460 | nsecs_t) { |
| 461 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 462 | return INVALID_OPERATION; |
| 463 | } |
| 464 | status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) { |
| 465 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 466 | return INVALID_OPERATION; |
| 467 | } |
| 468 | status_t Camera3Stream::returnInputBufferLocked( |
| 469 | const camera3_stream_buffer &) { |
| 470 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 471 | return INVALID_OPERATION; |
| 472 | } |
| 473 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 474 | void Camera3Stream::addBufferListener( |
| 475 | wp<Camera3StreamBufferListener> listener) { |
| 476 | Mutex::Autolock l(mLock); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 477 | |
| 478 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 479 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 480 | it != end; |
| 481 | ) { |
| 482 | if (*it == listener) { |
| 483 | ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__); |
| 484 | return; |
| 485 | } |
| 486 | it++; |
| 487 | } |
| 488 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 489 | mBufferListenerList.push_back(listener); |
| 490 | } |
| 491 | |
| 492 | void Camera3Stream::removeBufferListener( |
| 493 | const sp<Camera3StreamBufferListener>& listener) { |
| 494 | Mutex::Autolock l(mLock); |
| 495 | |
| 496 | bool erased = true; |
| 497 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 498 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 499 | it != end; |
| 500 | ) { |
| 501 | |
| 502 | if (*it == listener) { |
| 503 | it = mBufferListenerList.erase(it); |
| 504 | erased = true; |
| 505 | } else { |
| 506 | ++it; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (!erased) { |
| 511 | ALOGW("%s: Could not find listener to remove, already removed", |
| 512 | __FUNCTION__); |
| 513 | } |
| 514 | } |
| 515 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 516 | }; // namespace camera3 |
| 517 | |
| 518 | }; // namespace android |