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-OutputStream" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // This is needed for stdint.h to define INT64_MAX in C++ |
| 22 | #define __STDC_LIMIT_MACROS |
| 23 | |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Trace.h> |
| 26 | #include "Camera3OutputStream.h" |
| 27 | |
| 28 | #ifndef container_of |
| 29 | #define container_of(ptr, type, member) \ |
| 30 | (type *)((char*)(ptr) - offsetof(type, member)) |
| 31 | #endif |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | namespace camera3 { |
| 36 | |
| 37 | Camera3OutputStream::Camera3OutputStream(int id, |
| 38 | sp<ANativeWindow> consumer, |
| 39 | uint32_t width, uint32_t height, int format) : |
| 40 | Camera3Stream(id, CAMERA3_STREAM_OUTPUT, width, height, 0, format), |
| 41 | mConsumer(consumer), |
| 42 | mTransform(0), |
| 43 | mTotalBufferCount(0), |
| 44 | mDequeuedBufferCount(0), |
| 45 | mFrameCount(0), |
| 46 | mLastTimestamp(0) { |
| 47 | |
| 48 | mCombinedFence = new Fence(); |
| 49 | if (mConsumer == NULL) { |
| 50 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 51 | mState = STATE_ERROR; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Camera3OutputStream::Camera3OutputStream(int id, |
| 56 | sp<ANativeWindow> consumer, |
| 57 | uint32_t width, uint32_t height, size_t maxSize, int format) : |
| 58 | Camera3Stream(id, CAMERA3_STREAM_OUTPUT, |
| 59 | width, height, maxSize, format), |
Igor Murashkin | a55b545 | 2013-04-02 16:36:33 -0700 | [diff] [blame] | 60 | mConsumer(consumer), |
| 61 | mTransform(0), |
| 62 | mTotalBufferCount(0), |
| 63 | mDequeuedBufferCount(0), |
| 64 | mFrameCount(0), |
| 65 | mLastTimestamp(0) { |
| 66 | |
| 67 | mCombinedFence = new Fence(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 68 | |
| 69 | if (format != HAL_PIXEL_FORMAT_BLOB) { |
| 70 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 71 | format); |
| 72 | mState = STATE_ERROR; |
| 73 | } |
| 74 | |
| 75 | if (mConsumer == NULL) { |
| 76 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 77 | mState = STATE_ERROR; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | Camera3OutputStream::~Camera3OutputStream() { |
| 82 | disconnectLocked(); |
| 83 | } |
| 84 | |
| 85 | status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) { |
| 86 | ATRACE_CALL(); |
| 87 | status_t res; |
| 88 | |
| 89 | // Allow dequeue during IN_[RE]CONFIG for registration |
| 90 | if (mState != STATE_CONFIGURED && |
| 91 | mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) { |
| 92 | ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d", |
| 93 | __FUNCTION__, mId, mState); |
| 94 | return INVALID_OPERATION; |
| 95 | } |
| 96 | |
| 97 | // Only limit dequeue amount when fully configured |
| 98 | if (mState == STATE_CONFIGURED && |
| 99 | mDequeuedBufferCount == camera3_stream::max_buffers) { |
| 100 | ALOGE("%s: Stream %d: Already dequeued maximum number of simultaneous" |
| 101 | " buffers (%d)", __FUNCTION__, mId, |
| 102 | camera3_stream::max_buffers); |
| 103 | return INVALID_OPERATION; |
| 104 | } |
| 105 | |
| 106 | ANativeWindowBuffer* anb; |
| 107 | int fenceFd; |
| 108 | |
| 109 | res = mConsumer->dequeueBuffer(mConsumer.get(), &anb, &fenceFd); |
| 110 | if (res != OK) { |
| 111 | ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)", |
| 112 | __FUNCTION__, mId, strerror(-res), res); |
| 113 | return res; |
| 114 | } |
| 115 | |
| 116 | // Handing out a raw pointer to this object. Increment internal refcount. |
| 117 | incStrong(this); |
| 118 | buffer->stream = this; |
| 119 | buffer->buffer = &(anb->handle); |
| 120 | buffer->acquire_fence = fenceFd; |
| 121 | buffer->release_fence = -1; |
| 122 | buffer->status = CAMERA3_BUFFER_STATUS_OK; |
| 123 | |
| 124 | mDequeuedBufferCount++; |
| 125 | |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | status_t Camera3OutputStream::returnBufferLocked( |
| 130 | const camera3_stream_buffer &buffer, |
| 131 | nsecs_t timestamp) { |
| 132 | ATRACE_CALL(); |
| 133 | status_t res; |
| 134 | |
| 135 | // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be |
| 136 | // decrementing the internal refcount next. In case this is the last ref, we |
| 137 | // might get destructed on the decStrong(), so keep an sp around until the |
| 138 | // end of the call - otherwise have to sprinkle the decStrong on all exit |
| 139 | // points. |
| 140 | sp<Camera3OutputStream> keepAlive(this); |
| 141 | decStrong(this); |
| 142 | |
| 143 | // Allow buffers to be returned in the error state, to allow for disconnect |
| 144 | // and in the in-config states for registration |
| 145 | if (mState == STATE_CONSTRUCTED) { |
| 146 | ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d", |
| 147 | __FUNCTION__, mId, mState); |
| 148 | return INVALID_OPERATION; |
| 149 | } |
| 150 | if (mDequeuedBufferCount == 0) { |
| 151 | ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__, |
| 152 | mId); |
| 153 | return INVALID_OPERATION; |
| 154 | } |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 155 | |
| 156 | sp<Fence> releaseFence; |
| 157 | |
| 158 | /** |
| 159 | * Fence management - calculate Release Fence |
| 160 | */ |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 161 | if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) { |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 162 | if (buffer.release_fence != -1) { |
| 163 | ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when " |
| 164 | "there is an error", __FUNCTION__, mId, buffer.release_fence); |
| 165 | close(buffer.release_fence); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 166 | } |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Reassign release fence as the acquire fence in case of error |
| 170 | */ |
| 171 | releaseFence = new Fence(buffer.acquire_fence); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 172 | } else { |
| 173 | res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp); |
| 174 | if (res != OK) { |
| 175 | ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", |
| 176 | __FUNCTION__, mId, strerror(-res), res); |
| 177 | return res; |
| 178 | } |
| 179 | |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 180 | releaseFence = new Fence(buffer.release_fence); |
| 181 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 182 | |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 183 | int anwReleaseFence = releaseFence->dup(); |
| 184 | |
| 185 | /** |
| 186 | * Return buffer back to ANativeWindow |
| 187 | */ |
| 188 | if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) { |
| 189 | // Cancel buffer |
| 190 | res = mConsumer->cancelBuffer(mConsumer.get(), |
| 191 | container_of(buffer.buffer, ANativeWindowBuffer, handle), |
| 192 | anwReleaseFence); |
| 193 | if (res != OK) { |
| 194 | ALOGE("%s: Stream %d: Error cancelling buffer to native window:" |
| 195 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 196 | } |
| 197 | } else { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 198 | res = mConsumer->queueBuffer(mConsumer.get(), |
| 199 | container_of(buffer.buffer, ANativeWindowBuffer, handle), |
| 200 | anwReleaseFence); |
| 201 | if (res != OK) { |
| 202 | ALOGE("%s: Stream %d: Error queueing buffer to native window: %s (%d)", |
| 203 | __FUNCTION__, mId, strerror(-res), res); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 204 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 207 | if (res != OK) { |
| 208 | close(anwReleaseFence); |
| 209 | return res; |
| 210 | } |
| 211 | |
| 212 | mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence); |
| 213 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 214 | mDequeuedBufferCount--; |
| 215 | mBufferReturnedSignal.signal(); |
| 216 | mLastTimestamp = timestamp; |
| 217 | |
| 218 | return OK; |
| 219 | } |
| 220 | |
| 221 | bool Camera3OutputStream::hasOutstandingBuffersLocked() const { |
| 222 | nsecs_t signalTime = mCombinedFence->getSignalTime(); |
| 223 | ALOGV("%s: Stream %d: Has %d outstanding buffers," |
| 224 | " buffer signal time is %lld", |
| 225 | __FUNCTION__, mId, mDequeuedBufferCount, signalTime); |
| 226 | if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) { |
| 227 | return true; |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | status_t Camera3OutputStream::waitUntilIdle(nsecs_t timeout) { |
| 233 | status_t res; |
| 234 | { |
| 235 | Mutex::Autolock l(mLock); |
| 236 | while (mDequeuedBufferCount > 0) { |
| 237 | if (timeout != TIMEOUT_NEVER) { |
| 238 | nsecs_t startTime = systemTime(); |
| 239 | res = mBufferReturnedSignal.waitRelative(mLock, timeout); |
| 240 | if (res == TIMED_OUT) { |
| 241 | return res; |
| 242 | } else if (res != OK) { |
| 243 | ALOGE("%s: Error waiting for outstanding buffers: %s (%d)", |
| 244 | __FUNCTION__, strerror(-res), res); |
| 245 | return res; |
| 246 | } |
| 247 | nsecs_t deltaTime = systemTime() - startTime; |
| 248 | if (timeout <= deltaTime) { |
| 249 | timeout = 0; |
| 250 | } else { |
| 251 | timeout -= deltaTime; |
| 252 | } |
| 253 | } else { |
| 254 | res = mBufferReturnedSignal.wait(mLock); |
| 255 | if (res != OK) { |
| 256 | ALOGE("%s: Error waiting for outstanding buffers: %s (%d)", |
| 257 | __FUNCTION__, strerror(-res), res); |
| 258 | return res; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // No lock |
| 265 | |
| 266 | unsigned int timeoutMs; |
| 267 | if (timeout == TIMEOUT_NEVER) { |
| 268 | timeoutMs = Fence::TIMEOUT_NEVER; |
| 269 | } else if (timeout == 0) { |
| 270 | timeoutMs = 0; |
| 271 | } else { |
| 272 | // Round up to wait at least 1 ms |
| 273 | timeoutMs = (timeout + 999999) / 1000000; |
| 274 | } |
| 275 | |
| 276 | return mCombinedFence->wait(timeoutMs); |
| 277 | } |
| 278 | |
| 279 | void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const { |
| 280 | (void) args; |
| 281 | String8 lines; |
| 282 | lines.appendFormat(" Stream[%d]: Output\n", mId); |
| 283 | lines.appendFormat(" State: %d\n", mState); |
| 284 | lines.appendFormat(" Dims: %d x %d, format 0x%x\n", |
| 285 | camera3_stream::width, camera3_stream::height, |
| 286 | camera3_stream::format); |
| 287 | lines.appendFormat(" Max size: %d\n", mMaxSize); |
| 288 | lines.appendFormat(" Usage: %d, max HAL buffers: %d\n", |
| 289 | camera3_stream::usage, camera3_stream::max_buffers); |
| 290 | lines.appendFormat(" Frames produced: %d, last timestamp: %lld ns\n", |
| 291 | mFrameCount, mLastTimestamp); |
| 292 | lines.appendFormat(" Total buffers: %d, currently dequeued: %d\n", |
| 293 | mTotalBufferCount, mDequeuedBufferCount); |
| 294 | write(fd, lines.string(), lines.size()); |
| 295 | } |
| 296 | |
| 297 | status_t Camera3OutputStream::setTransform(int transform) { |
| 298 | ATRACE_CALL(); |
| 299 | Mutex::Autolock l(mLock); |
| 300 | return setTransformLocked(transform); |
| 301 | } |
| 302 | |
| 303 | status_t Camera3OutputStream::setTransformLocked(int transform) { |
| 304 | status_t res = OK; |
| 305 | if (mState == STATE_ERROR) { |
| 306 | ALOGE("%s: Stream in error state", __FUNCTION__); |
| 307 | return INVALID_OPERATION; |
| 308 | } |
| 309 | |
| 310 | mTransform = transform; |
| 311 | if (mState == STATE_CONFIGURED) { |
| 312 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 313 | transform); |
| 314 | if (res != OK) { |
| 315 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 316 | __FUNCTION__, transform, strerror(-res), res); |
| 317 | } |
| 318 | } |
| 319 | return res; |
| 320 | } |
| 321 | |
| 322 | status_t Camera3OutputStream::configureQueueLocked() { |
| 323 | status_t res; |
| 324 | |
| 325 | switch (mState) { |
| 326 | case STATE_IN_RECONFIG: |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 327 | res = disconnectLocked(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 328 | if (res != OK) { |
| 329 | return res; |
| 330 | } |
| 331 | break; |
| 332 | case STATE_IN_CONFIG: |
| 333 | // OK |
| 334 | break; |
| 335 | default: |
| 336 | ALOGE("%s: Bad state: %d", __FUNCTION__, mState); |
| 337 | return INVALID_OPERATION; |
| 338 | } |
| 339 | |
| 340 | // Configure consumer-side ANativeWindow interface |
| 341 | res = native_window_api_connect(mConsumer.get(), |
| 342 | NATIVE_WINDOW_API_CAMERA); |
| 343 | if (res != OK) { |
| 344 | ALOGE("%s: Unable to connect to native window for stream %d", |
| 345 | __FUNCTION__, mId); |
| 346 | return res; |
| 347 | } |
| 348 | |
| 349 | res = native_window_set_usage(mConsumer.get(), camera3_stream::usage); |
| 350 | if (res != OK) { |
| 351 | ALOGE("%s: Unable to configure usage %08x for stream %d", |
| 352 | __FUNCTION__, camera3_stream::usage, mId); |
| 353 | return res; |
| 354 | } |
| 355 | |
| 356 | res = native_window_set_scaling_mode(mConsumer.get(), |
| 357 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 358 | if (res != OK) { |
| 359 | ALOGE("%s: Unable to configure stream scaling: %s (%d)", |
| 360 | __FUNCTION__, strerror(-res), res); |
| 361 | return res; |
| 362 | } |
| 363 | |
| 364 | res = setTransformLocked(0); |
| 365 | if (res != OK) { |
| 366 | return res; |
| 367 | } |
| 368 | |
| 369 | if (mMaxSize == 0) { |
| 370 | // For buffers of known size |
| 371 | res = native_window_set_buffers_geometry(mConsumer.get(), |
| 372 | camera3_stream::width, camera3_stream::height, |
| 373 | camera3_stream::format); |
| 374 | } else { |
| 375 | // For buffers with bounded size |
| 376 | res = native_window_set_buffers_geometry(mConsumer.get(), |
| 377 | mMaxSize, 1, |
| 378 | camera3_stream::format); |
| 379 | } |
| 380 | if (res != OK) { |
| 381 | ALOGE("%s: Unable to configure stream buffer geometry" |
| 382 | " %d x %d, format %x for stream %d", |
| 383 | __FUNCTION__, camera3_stream::width, camera3_stream::height, |
| 384 | camera3_stream::format, mId); |
| 385 | return res; |
| 386 | } |
| 387 | |
| 388 | int maxConsumerBuffers; |
| 389 | res = mConsumer->query(mConsumer.get(), |
| 390 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); |
| 391 | if (res != OK) { |
| 392 | ALOGE("%s: Unable to query consumer undequeued" |
| 393 | " buffer count for stream %d", __FUNCTION__, mId); |
| 394 | return res; |
| 395 | } |
| 396 | |
| 397 | ALOGV("%s: Consumer wants %d buffers", __FUNCTION__, |
| 398 | maxConsumerBuffers); |
| 399 | |
| 400 | mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers; |
| 401 | mDequeuedBufferCount = 0; |
| 402 | mFrameCount = 0; |
| 403 | mLastTimestamp = 0; |
| 404 | |
| 405 | res = native_window_set_buffer_count(mConsumer.get(), |
| 406 | mTotalBufferCount); |
| 407 | if (res != OK) { |
| 408 | ALOGE("%s: Unable to set buffer count for stream %d", |
| 409 | __FUNCTION__, mId); |
| 410 | return res; |
| 411 | } |
| 412 | |
| 413 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 414 | mTransform); |
| 415 | if (res != OK) { |
| 416 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 417 | __FUNCTION__, mTransform, strerror(-res), res); |
| 418 | } |
| 419 | |
| 420 | return OK; |
| 421 | } |
| 422 | |
| 423 | size_t Camera3OutputStream::getBufferCountLocked() { |
| 424 | return mTotalBufferCount; |
| 425 | } |
| 426 | |
| 427 | status_t Camera3OutputStream::disconnectLocked() { |
| 428 | status_t res; |
| 429 | |
| 430 | switch (mState) { |
| 431 | case STATE_IN_RECONFIG: |
| 432 | case STATE_CONFIGURED: |
| 433 | // OK |
| 434 | break; |
| 435 | default: |
| 436 | // No connection, nothing to do |
| 437 | return OK; |
| 438 | } |
| 439 | |
| 440 | if (mDequeuedBufferCount > 0) { |
| 441 | ALOGE("%s: Can't disconnect with %d buffers still dequeued!", |
| 442 | __FUNCTION__, mDequeuedBufferCount); |
| 443 | return INVALID_OPERATION; |
| 444 | } |
| 445 | |
| 446 | res = native_window_api_disconnect(mConsumer.get(), NATIVE_WINDOW_API_CAMERA); |
| 447 | |
| 448 | /** |
| 449 | * This is not an error. if client calling process dies, the window will |
| 450 | * also die and all calls to it will return DEAD_OBJECT, thus it's already |
| 451 | * "disconnected" |
| 452 | */ |
| 453 | if (res == DEAD_OBJECT) { |
| 454 | ALOGW("%s: While disconnecting stream %d from native window, the" |
| 455 | " native window died from under us", __FUNCTION__, mId); |
| 456 | } |
| 457 | else if (res != OK) { |
| 458 | ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)", |
| 459 | __FUNCTION__, mId, res, strerror(-res)); |
| 460 | mState = STATE_ERROR; |
| 461 | return res; |
| 462 | } |
| 463 | |
| 464 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG : STATE_CONSTRUCTED; |
| 465 | return OK; |
| 466 | } |
| 467 | |
| 468 | }; // namespace camera3 |
| 469 | |
| 470 | }; // namespace android |