Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [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-IOStreamBase" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 22 | |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/Trace.h> |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 25 | #include "device3/Camera3IOStreamBase.h" |
| 26 | #include "device3/StatusTracker.h" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace camera3 { |
| 31 | |
| 32 | Camera3IOStreamBase::Camera3IOStreamBase(int id, camera3_stream_type_t type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame^] | 33 | uint32_t width, uint32_t height, size_t maxSize, int format, |
| 34 | android_dataspace dataSpace) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 35 | Camera3Stream(id, type, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame^] | 36 | width, height, maxSize, format, dataSpace), |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 37 | mTotalBufferCount(0), |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 38 | mHandoutTotalBufferCount(0), |
| 39 | mHandoutOutputBufferCount(0), |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 40 | mFrameCount(0), |
| 41 | mLastTimestamp(0) { |
| 42 | |
| 43 | mCombinedFence = new Fence(); |
| 44 | |
| 45 | if (maxSize > 0 && format != HAL_PIXEL_FORMAT_BLOB) { |
| 46 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 47 | format); |
| 48 | mState = STATE_ERROR; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Camera3IOStreamBase::~Camera3IOStreamBase() { |
| 53 | disconnectLocked(); |
| 54 | } |
| 55 | |
| 56 | bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const { |
| 57 | nsecs_t signalTime = mCombinedFence->getSignalTime(); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 58 | ALOGV("%s: Stream %d: Has %zu outstanding buffers," |
| 59 | " buffer signal time is %" PRId64, |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 60 | __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime); |
| 61 | if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 67 | void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const { |
| 68 | (void) args; |
| 69 | String8 lines; |
| 70 | lines.appendFormat(" State: %d\n", mState); |
| 71 | lines.appendFormat(" Dims: %d x %d, format 0x%x\n", |
| 72 | camera3_stream::width, camera3_stream::height, |
| 73 | camera3_stream::format); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 74 | lines.appendFormat(" Max size: %zu\n", mMaxSize); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 75 | lines.appendFormat(" Usage: %d, max HAL buffers: %d\n", |
| 76 | camera3_stream::usage, camera3_stream::max_buffers); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 77 | lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n", |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 78 | mFrameCount, mLastTimestamp); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 79 | lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n", |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 80 | mTotalBufferCount, mHandoutTotalBufferCount); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 81 | write(fd, lines.string(), lines.size()); |
| 82 | } |
| 83 | |
| 84 | status_t Camera3IOStreamBase::configureQueueLocked() { |
| 85 | status_t res; |
| 86 | |
| 87 | switch (mState) { |
| 88 | case STATE_IN_RECONFIG: |
| 89 | res = disconnectLocked(); |
| 90 | if (res != OK) { |
| 91 | return res; |
| 92 | } |
| 93 | break; |
| 94 | case STATE_IN_CONFIG: |
| 95 | // OK |
| 96 | break; |
| 97 | default: |
| 98 | ALOGE("%s: Bad state: %d", __FUNCTION__, mState); |
| 99 | return INVALID_OPERATION; |
| 100 | } |
| 101 | |
| 102 | return OK; |
| 103 | } |
| 104 | |
| 105 | size_t Camera3IOStreamBase::getBufferCountLocked() { |
| 106 | return mTotalBufferCount; |
| 107 | } |
| 108 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 109 | size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() { |
| 110 | return mHandoutOutputBufferCount; |
| 111 | } |
| 112 | |
| 113 | size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() { |
| 114 | return (mHandoutTotalBufferCount - mHandoutOutputBufferCount); |
| 115 | } |
| 116 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 117 | status_t Camera3IOStreamBase::disconnectLocked() { |
| 118 | switch (mState) { |
| 119 | case STATE_IN_RECONFIG: |
| 120 | case STATE_CONFIGURED: |
| 121 | // OK |
| 122 | break; |
| 123 | default: |
| 124 | // No connection, nothing to do |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 125 | ALOGV("%s: Stream %d: Already disconnected", |
| 126 | __FUNCTION__, mId); |
| 127 | return -ENOTCONN; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 130 | if (mHandoutTotalBufferCount > 0) { |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 131 | ALOGE("%s: Can't disconnect with %zu buffers still dequeued!", |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 132 | __FUNCTION__, mHandoutTotalBufferCount); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 133 | return INVALID_OPERATION; |
| 134 | } |
| 135 | |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer, |
| 140 | buffer_handle_t *handle, |
| 141 | int acquireFence, |
| 142 | int releaseFence, |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 143 | camera3_buffer_status_t status, |
| 144 | bool output) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Note that all fences are now owned by HAL. |
| 147 | */ |
| 148 | |
| 149 | // Handing out a raw pointer to this object. Increment internal refcount. |
| 150 | incStrong(this); |
| 151 | buffer.stream = this; |
| 152 | buffer.buffer = handle; |
| 153 | buffer.acquire_fence = acquireFence; |
| 154 | buffer.release_fence = releaseFence; |
| 155 | buffer.status = status; |
| 156 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 157 | // Inform tracker about becoming busy |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 158 | if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG && |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 159 | mState != STATE_IN_RECONFIG) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers |
| 162 | * before/after register_stream_buffers during initial configuration |
| 163 | * or re-configuration. |
| 164 | * |
| 165 | * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2 |
| 166 | */ |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 167 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 168 | if (statusTracker != 0) { |
| 169 | statusTracker->markComponentActive(mStatusId); |
| 170 | } |
| 171 | } |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 172 | mHandoutTotalBufferCount++; |
| 173 | |
| 174 | if (output) { |
| 175 | mHandoutOutputBufferCount++; |
| 176 | } |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const { |
| 180 | // Allow dequeue during IN_[RE]CONFIG for registration |
| 181 | if (mState != STATE_CONFIGURED && |
| 182 | mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) { |
| 183 | ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d", |
| 184 | __FUNCTION__, mId, mState); |
| 185 | return INVALID_OPERATION; |
| 186 | } |
| 187 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 188 | return OK; |
| 189 | } |
| 190 | |
| 191 | status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const { |
| 192 | // Allow buffers to be returned in the error state, to allow for disconnect |
| 193 | // and in the in-config states for registration |
| 194 | if (mState == STATE_CONSTRUCTED) { |
| 195 | ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d", |
| 196 | __FUNCTION__, mId, mState); |
| 197 | return INVALID_OPERATION; |
| 198 | } |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 199 | if (mHandoutTotalBufferCount == 0) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 200 | ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__, |
| 201 | mId); |
| 202 | return INVALID_OPERATION; |
| 203 | } |
| 204 | |
| 205 | return OK; |
| 206 | } |
| 207 | |
| 208 | status_t Camera3IOStreamBase::returnAnyBufferLocked( |
| 209 | const camera3_stream_buffer &buffer, |
| 210 | nsecs_t timestamp, |
| 211 | bool output) { |
| 212 | status_t res; |
| 213 | |
| 214 | // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be |
| 215 | // decrementing the internal refcount next. In case this is the last ref, we |
| 216 | // might get destructed on the decStrong(), so keep an sp around until the |
| 217 | // end of the call - otherwise have to sprinkle the decStrong on all exit |
| 218 | // points. |
| 219 | sp<Camera3IOStreamBase> keepAlive(this); |
| 220 | decStrong(this); |
| 221 | |
| 222 | if ((res = returnBufferPreconditionCheckLocked()) != OK) { |
| 223 | return res; |
| 224 | } |
| 225 | |
| 226 | sp<Fence> releaseFence; |
| 227 | res = returnBufferCheckedLocked(buffer, timestamp, output, |
| 228 | &releaseFence); |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 229 | // Res may be an error, but we still want to decrement our owned count |
| 230 | // to enable clean shutdown. So we'll just return the error but otherwise |
| 231 | // carry on |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 232 | |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 233 | if (releaseFence != 0) { |
| 234 | mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence); |
| 235 | } |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 236 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 237 | if (output) { |
| 238 | mHandoutOutputBufferCount--; |
| 239 | } |
| 240 | |
| 241 | mHandoutTotalBufferCount--; |
| 242 | if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG && |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 243 | mState != STATE_IN_RECONFIG) { |
Igor Murashkin | 13d315e | 2014-04-03 18:09:04 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers |
| 246 | * before/after register_stream_buffers during initial configuration |
| 247 | * or re-configuration. |
| 248 | * |
| 249 | * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2 |
| 250 | */ |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 251 | ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__, |
| 252 | mId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 253 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 254 | if (statusTracker != 0) { |
| 255 | statusTracker->markComponentIdle(mStatusId, mCombinedFence); |
| 256 | } |
| 257 | } |
| 258 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 259 | mBufferReturnedSignal.signal(); |
| 260 | |
| 261 | if (output) { |
| 262 | mLastTimestamp = timestamp; |
| 263 | } |
| 264 | |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 265 | return res; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | }; // namespace camera3 |
| 271 | |
| 272 | }; // namespace android |