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, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 50 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) : |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 51 | camera3_stream(), |
| 52 | mId(id), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 53 | mSetId(setId), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 54 | mName(String8::format("Camera3Stream[%d]", id)), |
| 55 | mMaxSize(maxSize), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 56 | mState(STATE_CONSTRUCTED), |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 57 | mStatusId(StatusTracker::NO_STATUS_ID), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 58 | mStreamUnpreparable(true), |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 59 | mUsage(0), |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 60 | mOldUsage(0), |
| 61 | mOldMaxBuffers(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 62 | mPrepared(false), |
| 63 | mPreparedBufferIdx(0), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 64 | mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX), |
| 65 | mBufferLimitLatency(kBufferLimitLatencyBinSize) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 66 | |
| 67 | camera3_stream::stream_type = type; |
| 68 | camera3_stream::width = width; |
| 69 | camera3_stream::height = height; |
| 70 | camera3_stream::format = format; |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 71 | camera3_stream::data_space = dataSpace; |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 72 | camera3_stream::rotation = rotation; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 73 | camera3_stream::max_buffers = 0; |
| 74 | camera3_stream::priv = NULL; |
| 75 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 76 | if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) && |
| 77 | maxSize == 0) { |
| 78 | ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 79 | mState = STATE_ERROR; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | int Camera3Stream::getId() const { |
| 84 | return mId; |
| 85 | } |
| 86 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 87 | int Camera3Stream::getStreamSetId() const { |
| 88 | return mSetId; |
| 89 | } |
| 90 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 91 | uint32_t Camera3Stream::getWidth() const { |
| 92 | return camera3_stream::width; |
| 93 | } |
| 94 | |
| 95 | uint32_t Camera3Stream::getHeight() const { |
| 96 | return camera3_stream::height; |
| 97 | } |
| 98 | |
| 99 | int Camera3Stream::getFormat() const { |
| 100 | return camera3_stream::format; |
| 101 | } |
| 102 | |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 103 | android_dataspace Camera3Stream::getDataSpace() const { |
| 104 | return camera3_stream::data_space; |
| 105 | } |
| 106 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 107 | uint64_t Camera3Stream::getUsage() const { |
| 108 | return mUsage; |
| 109 | } |
| 110 | |
| 111 | void Camera3Stream::setUsage(uint64_t usage) { |
| 112 | mUsage = usage; |
| 113 | } |
| 114 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 115 | camera3_stream* Camera3Stream::startConfiguration() { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 116 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 117 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 118 | status_t res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 119 | |
| 120 | switch (mState) { |
| 121 | case STATE_ERROR: |
| 122 | ALOGE("%s: In error state", __FUNCTION__); |
| 123 | return NULL; |
| 124 | case STATE_CONSTRUCTED: |
| 125 | // OK |
| 126 | break; |
| 127 | case STATE_IN_CONFIG: |
| 128 | case STATE_IN_RECONFIG: |
| 129 | // Can start config again with no trouble; but don't redo |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 130 | // mOldUsage/mOldMaxBuffers |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 131 | return this; |
| 132 | case STATE_CONFIGURED: |
Chien-Yu Chen | 90746f4 | 2015-04-15 13:50:13 -0700 | [diff] [blame] | 133 | if (hasOutstandingBuffersLocked()) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 134 | ALOGE("%s: Cannot configure stream; has outstanding buffers", |
| 135 | __FUNCTION__); |
| 136 | return NULL; |
| 137 | } |
| 138 | break; |
| 139 | default: |
| 140 | ALOGE("%s: Unknown state %d", __FUNCTION__, mState); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 144 | mOldUsage = mUsage; |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 145 | mOldMaxBuffers = camera3_stream::max_buffers; |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 146 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 147 | res = getEndpointUsage(&mUsage); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 148 | if (res != OK) { |
| 149 | ALOGE("%s: Cannot query consumer endpoint usage!", |
| 150 | __FUNCTION__); |
| 151 | return NULL; |
| 152 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 153 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 154 | // Stop tracking if currently doing so |
| 155 | if (mStatusId != StatusTracker::NO_STATUS_ID) { |
| 156 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 157 | if (statusTracker != 0) { |
| 158 | statusTracker->removeComponent(mStatusId); |
| 159 | } |
| 160 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 161 | } |
| 162 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 163 | if (mState == STATE_CONSTRUCTED) { |
| 164 | mState = STATE_IN_CONFIG; |
| 165 | } else { // mState == STATE_CONFIGURED |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 166 | 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] | 167 | mState = STATE_IN_RECONFIG; |
| 168 | } |
| 169 | |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | bool Camera3Stream::isConfiguring() const { |
| 174 | Mutex::Autolock l(mLock); |
| 175 | return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG); |
| 176 | } |
| 177 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 178 | status_t Camera3Stream::finishConfiguration() { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 179 | ATRACE_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 180 | Mutex::Autolock l(mLock); |
| 181 | switch (mState) { |
| 182 | case STATE_ERROR: |
| 183 | ALOGE("%s: In error state", __FUNCTION__); |
| 184 | return INVALID_OPERATION; |
| 185 | case STATE_IN_CONFIG: |
| 186 | case STATE_IN_RECONFIG: |
| 187 | // OK |
| 188 | break; |
| 189 | case STATE_CONSTRUCTED: |
| 190 | case STATE_CONFIGURED: |
| 191 | ALOGE("%s: Cannot finish configuration that hasn't been started", |
| 192 | __FUNCTION__); |
| 193 | return INVALID_OPERATION; |
| 194 | default: |
| 195 | ALOGE("%s: Unknown state", __FUNCTION__); |
| 196 | return INVALID_OPERATION; |
| 197 | } |
| 198 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 199 | // Register for idle tracking |
| 200 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 201 | if (statusTracker != 0) { |
| 202 | mStatusId = statusTracker->addComponent(); |
| 203 | } |
| 204 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 205 | // Check if the stream configuration is unchanged, and skip reallocation if |
| 206 | // so. As documented in hardware/camera3.h:configure_streams(). |
| 207 | if (mState == STATE_IN_RECONFIG && |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 208 | mOldUsage == mUsage && |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 209 | mOldMaxBuffers == camera3_stream::max_buffers) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 210 | mState = STATE_CONFIGURED; |
| 211 | return OK; |
| 212 | } |
| 213 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 214 | // Reset prepared state, since buffer config has changed, and existing |
| 215 | // allocations are no longer valid |
| 216 | mPrepared = false; |
| 217 | mStreamUnpreparable = false; |
| 218 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 219 | status_t res; |
| 220 | res = configureQueueLocked(); |
| 221 | if (res != OK) { |
| 222 | ALOGE("%s: Unable to configure stream %d queue: %s (%d)", |
| 223 | __FUNCTION__, mId, strerror(-res), res); |
| 224 | mState = STATE_ERROR; |
| 225 | return res; |
| 226 | } |
| 227 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 228 | mState = STATE_CONFIGURED; |
| 229 | |
| 230 | return res; |
| 231 | } |
| 232 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 233 | status_t Camera3Stream::cancelConfiguration() { |
| 234 | ATRACE_CALL(); |
| 235 | Mutex::Autolock l(mLock); |
| 236 | switch (mState) { |
| 237 | case STATE_ERROR: |
| 238 | ALOGE("%s: In error state", __FUNCTION__); |
| 239 | return INVALID_OPERATION; |
| 240 | case STATE_IN_CONFIG: |
| 241 | case STATE_IN_RECONFIG: |
| 242 | // OK |
| 243 | break; |
| 244 | case STATE_CONSTRUCTED: |
| 245 | case STATE_CONFIGURED: |
| 246 | ALOGE("%s: Cannot cancel configuration that hasn't been started", |
| 247 | __FUNCTION__); |
| 248 | return INVALID_OPERATION; |
| 249 | default: |
| 250 | ALOGE("%s: Unknown state", __FUNCTION__); |
| 251 | return INVALID_OPERATION; |
| 252 | } |
| 253 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 254 | mUsage = mOldUsage; |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 255 | camera3_stream::max_buffers = mOldMaxBuffers; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 256 | |
Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 257 | mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 258 | return OK; |
| 259 | } |
| 260 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 261 | bool Camera3Stream::isUnpreparable() { |
| 262 | ATRACE_CALL(); |
| 263 | |
| 264 | Mutex::Autolock l(mLock); |
| 265 | return mStreamUnpreparable; |
| 266 | } |
| 267 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 268 | status_t Camera3Stream::startPrepare(int maxCount) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 269 | ATRACE_CALL(); |
| 270 | |
| 271 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 272 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 273 | if (maxCount < 0) { |
| 274 | ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0", |
| 275 | __FUNCTION__, mId, maxCount); |
| 276 | return BAD_VALUE; |
| 277 | } |
| 278 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 279 | // This function should be only called when the stream is configured already. |
| 280 | if (mState != STATE_CONFIGURED) { |
| 281 | ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED " |
| 282 | "state %d", __FUNCTION__, mId, mState); |
| 283 | return INVALID_OPERATION; |
| 284 | } |
| 285 | |
| 286 | // This function can't be called if the stream has already received filled |
| 287 | // buffers |
| 288 | if (mStreamUnpreparable) { |
| 289 | ALOGE("%s: Stream %d: Can't prepare stream that's already in use", |
| 290 | __FUNCTION__, mId); |
| 291 | return INVALID_OPERATION; |
| 292 | } |
| 293 | |
| 294 | if (getHandoutOutputBufferCountLocked() > 0) { |
| 295 | ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers", |
| 296 | __FUNCTION__, mId); |
| 297 | return INVALID_OPERATION; |
| 298 | } |
| 299 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 300 | |
| 301 | |
| 302 | size_t pipelineMax = getBufferCountLocked(); |
| 303 | size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ? |
| 304 | pipelineMax : static_cast<size_t>(maxCount); |
| 305 | size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ? |
| 306 | pipelineMax : clampedCount; |
| 307 | |
| 308 | mPrepared = bufferCount <= mLastMaxCount; |
| 309 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 310 | if (mPrepared) return OK; |
| 311 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 312 | mLastMaxCount = bufferCount; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 313 | |
| 314 | mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount); |
| 315 | mPreparedBufferIdx = 0; |
| 316 | |
| 317 | mState = STATE_PREPARING; |
| 318 | |
| 319 | return NOT_ENOUGH_DATA; |
| 320 | } |
| 321 | |
| 322 | bool Camera3Stream::isPreparing() const { |
| 323 | Mutex::Autolock l(mLock); |
| 324 | return mState == STATE_PREPARING; |
| 325 | } |
| 326 | |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 327 | bool Camera3Stream::isAbandoned() const { |
| 328 | Mutex::Autolock l(mLock); |
| 329 | return mState == STATE_ABANDONED; |
| 330 | } |
| 331 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 332 | status_t Camera3Stream::prepareNextBuffer() { |
| 333 | ATRACE_CALL(); |
| 334 | |
| 335 | Mutex::Autolock l(mLock); |
| 336 | status_t res = OK; |
| 337 | |
| 338 | // This function should be only called when the stream is preparing |
| 339 | if (mState != STATE_PREPARING) { |
| 340 | ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING " |
| 341 | "state %d", __FUNCTION__, mId, mState); |
| 342 | return INVALID_OPERATION; |
| 343 | } |
| 344 | |
| 345 | // Get next buffer - this may allocate, and take a while for large buffers |
| 346 | res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) ); |
| 347 | if (res != OK) { |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 348 | ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation", |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 349 | __FUNCTION__, mId, mPreparedBufferIdx); |
| 350 | return NO_INIT; |
| 351 | } |
| 352 | |
| 353 | mPreparedBufferIdx++; |
| 354 | |
| 355 | // Check if we still have buffers left to allocate |
| 356 | if (mPreparedBufferIdx < mPreparedBuffers.size()) { |
| 357 | return NOT_ENOUGH_DATA; |
| 358 | } |
| 359 | |
| 360 | // Done with prepare - mark stream as such, and return all buffers |
| 361 | // via cancelPrepare |
| 362 | mPrepared = true; |
| 363 | |
| 364 | return cancelPrepareLocked(); |
| 365 | } |
| 366 | |
| 367 | status_t Camera3Stream::cancelPrepare() { |
| 368 | ATRACE_CALL(); |
| 369 | |
| 370 | Mutex::Autolock l(mLock); |
| 371 | |
| 372 | return cancelPrepareLocked(); |
| 373 | } |
| 374 | |
| 375 | status_t Camera3Stream::cancelPrepareLocked() { |
| 376 | status_t res = OK; |
| 377 | |
| 378 | // This function should be only called when the stream is mid-preparing. |
| 379 | if (mState != STATE_PREPARING) { |
| 380 | ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in " |
| 381 | "PREPARING state %d", __FUNCTION__, mId, mState); |
| 382 | return INVALID_OPERATION; |
| 383 | } |
| 384 | |
| 385 | // Return all valid buffers to stream, in ERROR state to indicate |
| 386 | // they weren't filled. |
| 387 | for (size_t i = 0; i < mPreparedBufferIdx; i++) { |
| 388 | mPreparedBuffers.editItemAt(i).release_fence = -1; |
| 389 | mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 390 | returnBufferLocked(mPreparedBuffers[i], 0); |
| 391 | } |
| 392 | mPreparedBuffers.clear(); |
| 393 | mPreparedBufferIdx = 0; |
| 394 | |
| 395 | mState = STATE_CONFIGURED; |
| 396 | |
| 397 | return res; |
| 398 | } |
| 399 | |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 400 | status_t Camera3Stream::tearDown() { |
| 401 | ATRACE_CALL(); |
| 402 | Mutex::Autolock l(mLock); |
| 403 | |
| 404 | status_t res = OK; |
| 405 | |
| 406 | // This function should be only called when the stream is configured. |
| 407 | if (mState != STATE_CONFIGURED) { |
| 408 | ALOGE("%s: Stream %d: Can't tear down stream if stream is not in " |
| 409 | "CONFIGURED state %d", __FUNCTION__, mId, mState); |
| 410 | return INVALID_OPERATION; |
| 411 | } |
| 412 | |
| 413 | // If any buffers have been handed to the HAL, the stream cannot be torn down. |
| 414 | if (getHandoutOutputBufferCountLocked() > 0) { |
| 415 | ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers", |
| 416 | __FUNCTION__, mId); |
| 417 | return INVALID_OPERATION; |
| 418 | } |
| 419 | |
| 420 | // Free buffers by disconnecting and then reconnecting to the buffer queue |
| 421 | // Only unused buffers will be dropped immediately; buffers that have been filled |
| 422 | // and are waiting to be acquired by the consumer and buffers that are currently |
| 423 | // acquired will be freed once they are released by the consumer. |
| 424 | |
| 425 | res = disconnectLocked(); |
| 426 | if (res != OK) { |
| 427 | if (res == -ENOTCONN) { |
| 428 | // queue has been disconnected, nothing left to do, so exit with success |
| 429 | return OK; |
| 430 | } |
| 431 | ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)", |
| 432 | __FUNCTION__, mId, strerror(-res), res); |
| 433 | return res; |
| 434 | } |
| 435 | |
| 436 | mState = STATE_IN_CONFIG; |
| 437 | |
| 438 | res = configureQueueLocked(); |
| 439 | if (res != OK) { |
| 440 | ALOGE("%s: Unable to configure stream %d queue: %s (%d)", |
| 441 | __FUNCTION__, mId, strerror(-res), res); |
| 442 | mState = STATE_ERROR; |
| 443 | return res; |
| 444 | } |
| 445 | |
| 446 | // Reset prepared state, since we've reconnected to the queue and can prepare again. |
| 447 | mPrepared = false; |
| 448 | mStreamUnpreparable = false; |
| 449 | |
| 450 | mState = STATE_CONFIGURED; |
| 451 | |
| 452 | return OK; |
| 453 | } |
| 454 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 455 | status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer, |
| 456 | const std::vector<size_t>& surface_ids) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 457 | ATRACE_CALL(); |
| 458 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 459 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 460 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 461 | // This function should be only called when the stream is configured already. |
| 462 | if (mState != STATE_CONFIGURED) { |
| 463 | ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d", |
| 464 | __FUNCTION__, mId, mState); |
| 465 | return INVALID_OPERATION; |
| 466 | } |
| 467 | |
| 468 | // Wait for new buffer returned back if we are running into the limit. |
| 469 | if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) { |
| 470 | ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.", |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 471 | __FUNCTION__, camera3_stream::max_buffers); |
| 472 | nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 473 | res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 474 | nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC); |
| 475 | mBufferLimitLatency.add(waitStart, waitEnd); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 476 | if (res != OK) { |
| 477 | if (res == TIMED_OUT) { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 478 | ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)", |
| 479 | __FUNCTION__, kWaitForBufferDuration / 1000000LL, |
| 480 | camera3_stream::max_buffers); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 481 | } |
| 482 | return res; |
| 483 | } |
| 484 | } |
| 485 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 486 | res = getBufferLocked(buffer, surface_ids); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 487 | if (res == OK) { |
| 488 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true); |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 489 | if (buffer->buffer) { |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 490 | Mutex::Autolock l(mOutstandingBuffersLock); |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 491 | mOutstandingBuffers.push_back(*buffer->buffer); |
| 492 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 498 | bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{ |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 499 | if (buffer.buffer == nullptr) { |
| 500 | return false; |
| 501 | } |
| 502 | |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 503 | Mutex::Autolock l(mOutstandingBuffersLock); |
| 504 | |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 505 | for (auto b : mOutstandingBuffers) { |
| 506 | if (b == *buffer.buffer) { |
| 507 | return true; |
| 508 | } |
| 509 | } |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) { |
| 514 | if (buffer.buffer == nullptr) { |
| 515 | return; |
| 516 | } |
| 517 | |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 518 | Mutex::Autolock l(mOutstandingBuffersLock); |
| 519 | |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 520 | for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) { |
| 521 | if (*b == *buffer.buffer) { |
| 522 | mOutstandingBuffers.erase(b); |
| 523 | return; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 528 | status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer, |
| 529 | nsecs_t timestamp) { |
| 530 | ATRACE_CALL(); |
| 531 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 532 | |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 533 | // Check if this buffer is outstanding. |
| 534 | if (!isOutstandingBuffer(buffer)) { |
| 535 | ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId); |
| 536 | return BAD_VALUE; |
| 537 | } |
| 538 | |
Shuzhen Wang | 1c484a6 | 2017-07-14 15:14:19 -0700 | [diff] [blame] | 539 | removeOutstandingBuffer(buffer); |
| 540 | |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 541 | /** |
| 542 | * TODO: Check that the state is valid first. |
| 543 | * |
| 544 | * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED. |
| 545 | * >= HAL3.2 CONFIGURED only |
| 546 | * |
| 547 | * Do this for getBuffer as well. |
| 548 | */ |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 549 | status_t res = returnBufferLocked(buffer, timestamp); |
| 550 | if (res == OK) { |
| 551 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true); |
| 552 | } |
| 553 | |
Chien-Yu Chen | b83c1fe | 2015-09-10 16:15:21 -0700 | [diff] [blame] | 554 | // Even if returning the buffer failed, we still want to signal whoever is waiting for the |
| 555 | // buffer to be returned. |
| 556 | mOutputBufferReturnedSignal.signal(); |
| 557 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 558 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame] | 561 | status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) { |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 562 | ATRACE_CALL(); |
| 563 | Mutex::Autolock l(mLock); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 564 | status_t res = OK; |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 565 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 566 | // This function should be only called when the stream is configured already. |
| 567 | if (mState != STATE_CONFIGURED) { |
| 568 | ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d", |
| 569 | __FUNCTION__, mId, mState); |
| 570 | return INVALID_OPERATION; |
| 571 | } |
| 572 | |
| 573 | // Wait for new buffer returned back if we are running into the limit. |
Eino-Ville Talvala | ba43525 | 2017-06-21 16:07:25 -0700 | [diff] [blame] | 574 | if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) { |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 575 | ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.", |
| 576 | __FUNCTION__, camera3_stream::max_buffers); |
| 577 | res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration); |
| 578 | if (res != OK) { |
| 579 | if (res == TIMED_OUT) { |
| 580 | ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__, |
| 581 | kWaitForBufferDuration / 1000000LL); |
| 582 | } |
| 583 | return res; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | res = getInputBufferLocked(buffer); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 588 | if (res == OK) { |
| 589 | fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false); |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 590 | if (buffer->buffer) { |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 591 | Mutex::Autolock l(mOutstandingBuffersLock); |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 592 | mOutstandingBuffers.push_back(*buffer->buffer); |
| 593 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return res; |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) { |
| 600 | ATRACE_CALL(); |
| 601 | Mutex::Autolock l(mLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 602 | |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 603 | // Check if this buffer is outstanding. |
| 604 | if (!isOutstandingBuffer(buffer)) { |
| 605 | ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId); |
| 606 | return BAD_VALUE; |
| 607 | } |
| 608 | |
Shuzhen Wang | 1c484a6 | 2017-07-14 15:14:19 -0700 | [diff] [blame] | 609 | removeOutstandingBuffer(buffer); |
| 610 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 611 | status_t res = returnInputBufferLocked(buffer); |
| 612 | if (res == OK) { |
| 613 | fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false); |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 614 | mInputBufferReturnedSignal.signal(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 615 | } |
Chien-Yu Chen | e02e932 | 2016-04-11 16:59:33 -0700 | [diff] [blame] | 616 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 617 | return res; |
| 618 | } |
| 619 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 620 | status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) { |
| 621 | ATRACE_CALL(); |
| 622 | Mutex::Autolock l(mLock); |
| 623 | |
| 624 | return getInputBufferProducerLocked(producer); |
| 625 | } |
| 626 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 627 | void Camera3Stream::fireBufferListenersLocked( |
Shuzhen Wang | b0fdc1e | 2016-03-20 23:21:39 -0700 | [diff] [blame] | 628 | const camera3_stream_buffer& buffer, bool acquired, bool output) { |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 629 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 630 | |
| 631 | // TODO: finish implementing |
| 632 | |
| 633 | Camera3StreamBufferListener::BufferInfo info = |
| 634 | Camera3StreamBufferListener::BufferInfo(); |
| 635 | info.mOutput = output; |
Shuzhen Wang | b0fdc1e | 2016-03-20 23:21:39 -0700 | [diff] [blame] | 636 | info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 637 | // TODO: rest of fields |
| 638 | |
| 639 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 640 | it != end; |
| 641 | ++it) { |
| 642 | |
| 643 | sp<Camera3StreamBufferListener> listener = it->promote(); |
| 644 | if (listener != 0) { |
| 645 | if (acquired) { |
| 646 | listener->onBufferAcquired(info); |
| 647 | } else { |
| 648 | listener->onBufferReleased(info); |
| 649 | } |
| 650 | } |
| 651 | } |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 654 | bool Camera3Stream::hasOutstandingBuffers() const { |
| 655 | ATRACE_CALL(); |
| 656 | Mutex::Autolock l(mLock); |
| 657 | return hasOutstandingBuffersLocked(); |
| 658 | } |
| 659 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 660 | status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) { |
| 661 | Mutex::Autolock l(mLock); |
| 662 | sp<StatusTracker> oldTracker = mStatusTracker.promote(); |
| 663 | if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) { |
| 664 | oldTracker->removeComponent(mStatusId); |
| 665 | } |
| 666 | mStatusId = StatusTracker::NO_STATUS_ID; |
| 667 | mStatusTracker = statusTracker; |
| 668 | |
| 669 | return OK; |
| 670 | } |
| 671 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 672 | status_t Camera3Stream::disconnect() { |
| 673 | ATRACE_CALL(); |
| 674 | Mutex::Autolock l(mLock); |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 675 | ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId); |
| 676 | status_t res = disconnectLocked(); |
| 677 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 678 | mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId); |
| 679 | mBufferLimitLatency.reset(); |
| 680 | |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 681 | if (res == -ENOTCONN) { |
| 682 | // "Already disconnected" -- not an error |
| 683 | return OK; |
| 684 | } else { |
| 685 | return res; |
| 686 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 689 | void Camera3Stream::dump(int fd, const Vector<String16> &args) const |
| 690 | { |
| 691 | (void)args; |
| 692 | mBufferLimitLatency.dump(fd, |
| 693 | " Latency histogram for wait on max_buffers"); |
| 694 | } |
| 695 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 696 | status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *, |
| 697 | const std::vector<size_t>&) { |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 698 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 699 | return INVALID_OPERATION; |
| 700 | } |
| 701 | status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &, |
| 702 | nsecs_t) { |
| 703 | ALOGE("%s: This type of stream does not support output", __FUNCTION__); |
| 704 | return INVALID_OPERATION; |
| 705 | } |
| 706 | status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) { |
| 707 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 708 | return INVALID_OPERATION; |
| 709 | } |
| 710 | status_t Camera3Stream::returnInputBufferLocked( |
| 711 | const camera3_stream_buffer &) { |
| 712 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 713 | return INVALID_OPERATION; |
| 714 | } |
Eino-Ville Talvala | 02bf032 | 2016-02-18 12:41:10 -0800 | [diff] [blame] | 715 | status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) { |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 716 | ALOGE("%s: This type of stream does not support input", __FUNCTION__); |
| 717 | return INVALID_OPERATION; |
| 718 | } |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 719 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 720 | void Camera3Stream::addBufferListener( |
| 721 | wp<Camera3StreamBufferListener> listener) { |
| 722 | Mutex::Autolock l(mLock); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 723 | |
| 724 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 725 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 726 | it != end; |
| 727 | ) { |
| 728 | if (*it == listener) { |
| 729 | ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__); |
| 730 | return; |
| 731 | } |
| 732 | it++; |
| 733 | } |
| 734 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 735 | mBufferListenerList.push_back(listener); |
| 736 | } |
| 737 | |
| 738 | void Camera3Stream::removeBufferListener( |
| 739 | const sp<Camera3StreamBufferListener>& listener) { |
| 740 | Mutex::Autolock l(mLock); |
| 741 | |
| 742 | bool erased = true; |
| 743 | List<wp<Camera3StreamBufferListener> >::iterator it, end; |
| 744 | for (it = mBufferListenerList.begin(), end = mBufferListenerList.end(); |
| 745 | it != end; |
| 746 | ) { |
| 747 | |
| 748 | if (*it == listener) { |
| 749 | it = mBufferListenerList.erase(it); |
| 750 | erased = true; |
| 751 | } else { |
| 752 | ++it; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | if (!erased) { |
| 757 | ALOGW("%s: Could not find listener to remove, already removed", |
| 758 | __FUNCTION__); |
| 759 | } |
| 760 | } |
| 761 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 762 | void Camera3Stream::setBufferFreedListener( |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 763 | wp<Camera3StreamBufferFreedListener> listener) { |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 764 | Mutex::Autolock l(mLock); |
| 765 | // Only allow set listener during stream configuration because stream is guaranteed to be IDLE |
| 766 | // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks |
| 767 | if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) { |
| 768 | ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__); |
| 769 | return; |
| 770 | } |
| 771 | mBufferFreedListener = listener; |
| 772 | } |
| 773 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 774 | }; // namespace camera3 |
| 775 | |
| 776 | }; // namespace android |