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 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <utils/Trace.h> |
| 23 | #include "Camera3OutputStream.h" |
| 24 | |
| 25 | #ifndef container_of |
| 26 | #define container_of(ptr, type, member) \ |
| 27 | (type *)((char*)(ptr) - offsetof(type, member)) |
| 28 | #endif |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | namespace camera3 { |
| 33 | |
| 34 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 35 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 36 | uint32_t width, uint32_t height, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 37 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 38 | Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 39 | /*maxSize*/0, format, dataSpace, rotation, setId), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 40 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 41 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 42 | mTraceFirstBuffer(true), |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 43 | mTimestampBuffer(true), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 44 | mUseBufferManager(false) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 45 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 46 | if (mConsumer == NULL) { |
| 47 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 48 | mState = STATE_ERROR; |
| 49 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 50 | |
| 51 | if (setId > CAMERA3_STREAM_SET_ID_INVALID) { |
| 52 | mBufferReleasedListener = new BufferReleasedListener(this); |
| 53 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 57 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 58 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 59 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 60 | Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 61 | format, dataSpace, rotation, setId), |
Igor Murashkin | a55b545 | 2013-04-02 16:36:33 -0700 | [diff] [blame] | 62 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 63 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 64 | mTraceFirstBuffer(true), |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 65 | mTimestampBuffer(true), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 66 | mUseBufferManager(false) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 67 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 68 | if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 69 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 70 | format); |
| 71 | mState = STATE_ERROR; |
| 72 | } |
| 73 | |
| 74 | if (mConsumer == NULL) { |
| 75 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 76 | mState = STATE_ERROR; |
| 77 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 78 | |
| 79 | if (setId > CAMERA3_STREAM_SET_ID_INVALID) { |
| 80 | mBufferReleasedListener = new BufferReleasedListener(this); |
| 81 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 84 | Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type, |
| 85 | uint32_t width, uint32_t height, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 86 | int format, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 87 | android_dataspace dataSpace, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 88 | camera3_stream_rotation_t rotation, |
| 89 | int setId) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 90 | Camera3IOStreamBase(id, type, width, height, |
| 91 | /*maxSize*/0, |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 92 | format, dataSpace, rotation, setId), |
| 93 | mTransform(0), |
| 94 | mTraceFirstBuffer(true), |
| 95 | mUseBufferManager(false) { |
| 96 | |
| 97 | if (setId > CAMERA3_STREAM_SET_ID_INVALID) { |
| 98 | mBufferReleasedListener = new BufferReleasedListener(this); |
| 99 | } |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 100 | |
| 101 | // Subclasses expected to initialize mConsumer themselves |
| 102 | } |
| 103 | |
| 104 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 105 | Camera3OutputStream::~Camera3OutputStream() { |
| 106 | disconnectLocked(); |
| 107 | } |
| 108 | |
| 109 | status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) { |
| 110 | ATRACE_CALL(); |
| 111 | status_t res; |
| 112 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 113 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 114 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | ANativeWindowBuffer* anb; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 118 | int fenceFd = -1; |
| 119 | if (mUseBufferManager) { |
| 120 | sp<GraphicBuffer> gb; |
| 121 | res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, &fenceFd); |
| 122 | if (res != OK) { |
| 123 | ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)", |
| 124 | __FUNCTION__, mId, strerror(-res), res); |
| 125 | return res; |
| 126 | } |
| 127 | // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a |
| 128 | // successful return. |
| 129 | anb = gb.get(); |
| 130 | res = mConsumer->attachBuffer(anb); |
| 131 | if (res != OK) { |
| 132 | ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)", |
| 133 | __FUNCTION__, mId, strerror(-res), res); |
| 134 | return res; |
| 135 | } |
| 136 | } else { |
| 137 | /** |
| 138 | * Release the lock briefly to avoid deadlock for below scenario: |
| 139 | * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring(). |
| 140 | * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock. |
| 141 | * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable(). |
| 142 | * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock |
| 143 | * StreamingProcessor lock. |
| 144 | * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock |
| 145 | * and try to lock bufferQueue lock. |
| 146 | * Then there is circular locking dependency. |
| 147 | */ |
| 148 | sp<ANativeWindow> currentConsumer = mConsumer; |
| 149 | mLock.unlock(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 150 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 151 | res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd); |
| 152 | mLock.lock(); |
| 153 | if (res != OK) { |
| 154 | ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)", |
| 155 | __FUNCTION__, mId, strerror(-res), res); |
| 156 | return res; |
| 157 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 160 | /** |
| 161 | * FenceFD now owned by HAL except in case of error, |
| 162 | * in which case we reassign it to acquire_fence |
| 163 | */ |
| 164 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 165 | /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 166 | |
| 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | status_t Camera3OutputStream::returnBufferLocked( |
| 171 | const camera3_stream_buffer &buffer, |
| 172 | nsecs_t timestamp) { |
| 173 | ATRACE_CALL(); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 174 | |
| 175 | status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true); |
| 176 | |
| 177 | if (res != OK) { |
| 178 | return res; |
| 179 | } |
| 180 | |
| 181 | mLastTimestamp = timestamp; |
| 182 | |
| 183 | return OK; |
| 184 | } |
| 185 | |
| 186 | status_t Camera3OutputStream::returnBufferCheckedLocked( |
| 187 | const camera3_stream_buffer &buffer, |
| 188 | nsecs_t timestamp, |
| 189 | bool output, |
| 190 | /*out*/ |
| 191 | sp<Fence> *releaseFenceOut) { |
| 192 | |
| 193 | (void)output; |
| 194 | ALOG_ASSERT(output, "Expected output to be true"); |
| 195 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 196 | status_t res; |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 197 | |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 198 | // Fence management - always honor release fence from HAL |
| 199 | sp<Fence> releaseFence = new Fence(buffer.release_fence); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 200 | int anwReleaseFence = releaseFence->dup(); |
| 201 | |
| 202 | /** |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 203 | * Release the lock briefly to avoid deadlock with |
| 204 | * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this |
| 205 | * thread will go into StreamingProcessor::onFrameAvailable) during |
| 206 | * queueBuffer |
| 207 | */ |
| 208 | sp<ANativeWindow> currentConsumer = mConsumer; |
| 209 | mLock.unlock(); |
| 210 | |
| 211 | /** |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 212 | * Return buffer back to ANativeWindow |
| 213 | */ |
| 214 | if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) { |
| 215 | // Cancel buffer |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 216 | res = currentConsumer->cancelBuffer(currentConsumer.get(), |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 217 | container_of(buffer.buffer, ANativeWindowBuffer, handle), |
| 218 | anwReleaseFence); |
| 219 | if (res != OK) { |
| 220 | ALOGE("%s: Stream %d: Error cancelling buffer to native window:" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 221 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 222 | } |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 223 | |
| 224 | if (mUseBufferManager) { |
| 225 | // Return this buffer back to buffer manager. |
| 226 | mBufferReleasedListener->onBufferReleased(); |
| 227 | } |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 228 | } else { |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 229 | if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) { |
| 230 | { |
| 231 | char traceLog[48]; |
| 232 | snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId); |
| 233 | ATRACE_NAME(traceLog); |
| 234 | } |
| 235 | mTraceFirstBuffer = false; |
| 236 | } |
| 237 | |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 238 | /* Certain consumers (such as AudioSource or HardwareComposer) use |
| 239 | * MONOTONIC time, causing time misalignment if camera timestamp is |
| 240 | * in BOOTTIME. Avoid setting timestamp, and let BufferQueue generate it |
| 241 | * instead. */ |
| 242 | if (mTimestampBuffer) { |
| 243 | res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp); |
| 244 | if (res != OK) { |
| 245 | ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", |
| 246 | __FUNCTION__, mId, strerror(-res), res); |
| 247 | return res; |
| 248 | } |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 251 | res = currentConsumer->queueBuffer(currentConsumer.get(), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 252 | container_of(buffer.buffer, ANativeWindowBuffer, handle), |
| 253 | anwReleaseFence); |
| 254 | if (res != OK) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 255 | ALOGE("%s: Stream %d: Error queueing buffer to native window: " |
| 256 | "%s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 257 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 258 | } |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 259 | mLock.lock(); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 260 | |
| 261 | // Once a valid buffer has been returned to the queue, can no longer |
| 262 | // dequeue all buffers for preallocation. |
| 263 | if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) { |
| 264 | mStreamUnpreparable = true; |
| 265 | } |
| 266 | |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 267 | if (res != OK) { |
| 268 | close(anwReleaseFence); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 271 | *releaseFenceOut = releaseFence; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 272 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 273 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 276 | void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const { |
| 277 | (void) args; |
| 278 | String8 lines; |
| 279 | lines.appendFormat(" Stream[%d]: Output\n", mId); |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 280 | lines.appendFormat(" Consumer name: %s\n", mConsumerName.string()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 281 | write(fd, lines.string(), lines.size()); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 282 | |
| 283 | Camera3IOStreamBase::dump(fd, args); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | status_t Camera3OutputStream::setTransform(int transform) { |
| 287 | ATRACE_CALL(); |
| 288 | Mutex::Autolock l(mLock); |
| 289 | return setTransformLocked(transform); |
| 290 | } |
| 291 | |
| 292 | status_t Camera3OutputStream::setTransformLocked(int transform) { |
| 293 | status_t res = OK; |
| 294 | if (mState == STATE_ERROR) { |
| 295 | ALOGE("%s: Stream in error state", __FUNCTION__); |
| 296 | return INVALID_OPERATION; |
| 297 | } |
| 298 | |
| 299 | mTransform = transform; |
| 300 | if (mState == STATE_CONFIGURED) { |
| 301 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 302 | transform); |
| 303 | if (res != OK) { |
| 304 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 305 | __FUNCTION__, transform, strerror(-res), res); |
| 306 | } |
| 307 | } |
| 308 | return res; |
| 309 | } |
| 310 | |
| 311 | status_t Camera3OutputStream::configureQueueLocked() { |
| 312 | status_t res; |
| 313 | |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 314 | mTraceFirstBuffer = true; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 315 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 316 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 319 | ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL"); |
| 320 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 321 | // Configure consumer-side ANativeWindow interface. The listener may be used |
| 322 | // to notify buffer manager (if it is used) of the returned buffers. |
| 323 | res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 324 | if (res != OK) { |
| 325 | ALOGE("%s: Unable to connect to native window for stream %d", |
| 326 | __FUNCTION__, mId); |
| 327 | return res; |
| 328 | } |
| 329 | |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 330 | mConsumerName = mConsumer->getConsumerName(); |
| 331 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 332 | res = native_window_set_usage(mConsumer.get(), camera3_stream::usage); |
| 333 | if (res != OK) { |
| 334 | ALOGE("%s: Unable to configure usage %08x for stream %d", |
| 335 | __FUNCTION__, camera3_stream::usage, mId); |
| 336 | return res; |
| 337 | } |
| 338 | |
| 339 | res = native_window_set_scaling_mode(mConsumer.get(), |
| 340 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 341 | if (res != OK) { |
| 342 | ALOGE("%s: Unable to configure stream scaling: %s (%d)", |
| 343 | __FUNCTION__, strerror(-res), res); |
| 344 | return res; |
| 345 | } |
| 346 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 347 | if (mMaxSize == 0) { |
| 348 | // For buffers of known size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 349 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
| 350 | camera3_stream::width, camera3_stream::height); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 351 | } else { |
| 352 | // For buffers with bounded size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 353 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
| 354 | mMaxSize, 1); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 355 | } |
| 356 | if (res != OK) { |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 357 | ALOGE("%s: Unable to configure stream buffer dimensions" |
| 358 | " %d x %d (maxSize %zu) for stream %d", |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 359 | __FUNCTION__, camera3_stream::width, camera3_stream::height, |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 360 | mMaxSize, mId); |
| 361 | return res; |
| 362 | } |
| 363 | res = native_window_set_buffers_format(mConsumer.get(), |
| 364 | camera3_stream::format); |
| 365 | if (res != OK) { |
| 366 | ALOGE("%s: Unable to configure stream buffer format %#x for stream %d", |
| 367 | __FUNCTION__, camera3_stream::format, mId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 368 | return res; |
| 369 | } |
| 370 | |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 371 | res = native_window_set_buffers_data_space(mConsumer.get(), |
| 372 | camera3_stream::data_space); |
| 373 | if (res != OK) { |
| 374 | ALOGE("%s: Unable to configure stream dataspace %#x for stream %d", |
| 375 | __FUNCTION__, camera3_stream::data_space, mId); |
| 376 | return res; |
| 377 | } |
| 378 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 379 | int maxConsumerBuffers; |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 380 | res = static_cast<ANativeWindow*>(mConsumer.get())->query( |
| 381 | mConsumer.get(), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 382 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); |
| 383 | if (res != OK) { |
| 384 | ALOGE("%s: Unable to query consumer undequeued" |
| 385 | " buffer count for stream %d", __FUNCTION__, mId); |
| 386 | return res; |
| 387 | } |
| 388 | |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 389 | ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__, |
| 390 | maxConsumerBuffers, camera3_stream::max_buffers); |
| 391 | if (camera3_stream::max_buffers == 0) { |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 392 | ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1", |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 393 | __FUNCTION__, camera3_stream::max_buffers); |
| 394 | return INVALID_OPERATION; |
| 395 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 396 | |
| 397 | mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers; |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 398 | mHandoutTotalBufferCount = 0; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 399 | mFrameCount = 0; |
| 400 | mLastTimestamp = 0; |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 401 | mTimestampBuffer = !(isConsumedByHWComposer() | isVideoStream()); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 402 | |
| 403 | res = native_window_set_buffer_count(mConsumer.get(), |
| 404 | mTotalBufferCount); |
| 405 | if (res != OK) { |
| 406 | ALOGE("%s: Unable to set buffer count for stream %d", |
| 407 | __FUNCTION__, mId); |
| 408 | return res; |
| 409 | } |
| 410 | |
| 411 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 412 | mTransform); |
| 413 | if (res != OK) { |
| 414 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 415 | __FUNCTION__, mTransform, strerror(-res), res); |
| 416 | } |
| 417 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Camera3 Buffer manager is only supported by HAL3.2 onwards, as the older HALs requires |
| 420 | * buffers to be statically allocated for internal static buffer registration, while the |
| 421 | * buffers provided by buffer manager are really dynamically allocated. Camera3Device only |
| 422 | * sets the mBufferManager if device version is >= HAL3.2, which guarantees that the buffer |
| 423 | * manager setup is skipped in below code. |
| 424 | */ |
| 425 | if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) { |
| 426 | StreamInfo streamInfo( |
| 427 | getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(), |
| 428 | camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true); |
| 429 | res = mBufferManager->registerStream(streamInfo); |
| 430 | if (res == OK) { |
| 431 | // Disable buffer allocation for this BufferQueue, buffer manager will take over |
| 432 | // the buffer allocation responsibility. |
| 433 | mConsumer->getIGraphicBufferProducer()->allowAllocation(false); |
| 434 | mUseBufferManager = true; |
| 435 | } else { |
| 436 | ALOGE("%s: Unable to register stream %d to camera3 buffer manager, " |
| 437 | "(error %d %s), fall back to BufferQueue for buffer management!", |
| 438 | __FUNCTION__, mId, res, strerror(-res)); |
| 439 | } |
| 440 | } |
| 441 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 442 | return OK; |
| 443 | } |
| 444 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 445 | status_t Camera3OutputStream::disconnectLocked() { |
| 446 | status_t res; |
| 447 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 448 | if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { |
| 449 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 452 | ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId()); |
| 453 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 454 | res = native_window_api_disconnect(mConsumer.get(), |
| 455 | NATIVE_WINDOW_API_CAMERA); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * This is not an error. if client calling process dies, the window will |
| 459 | * also die and all calls to it will return DEAD_OBJECT, thus it's already |
| 460 | * "disconnected" |
| 461 | */ |
| 462 | if (res == DEAD_OBJECT) { |
| 463 | ALOGW("%s: While disconnecting stream %d from native window, the" |
| 464 | " native window died from under us", __FUNCTION__, mId); |
| 465 | } |
| 466 | else if (res != OK) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 467 | ALOGE("%s: Unable to disconnect stream %d from native window " |
| 468 | "(error %d %s)", |
| 469 | __FUNCTION__, mId, res, strerror(-res)); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 470 | mState = STATE_ERROR; |
| 471 | return res; |
| 472 | } |
| 473 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 474 | // Since device is already idle, there is no getBuffer call to buffer manager, unregister the |
| 475 | // stream at this point should be safe. |
| 476 | if (mUseBufferManager) { |
| 477 | res = mBufferManager->unregisterStream(getId(), getStreamSetId()); |
| 478 | if (res != OK) { |
| 479 | ALOGE("%s: Unable to unregister stream %d from buffer manager " |
| 480 | "(error %d %s)", __FUNCTION__, mId, res, strerror(-res)); |
| 481 | mState = STATE_ERROR; |
| 482 | return res; |
| 483 | } |
| 484 | // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as |
| 485 | // the stream is still in usable state after this call. |
| 486 | mUseBufferManager = false; |
| 487 | } |
| 488 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 489 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG |
| 490 | : STATE_CONSTRUCTED; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 491 | return OK; |
| 492 | } |
| 493 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 494 | status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 495 | |
| 496 | status_t res; |
| 497 | int32_t u = 0; |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 498 | res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(), |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 499 | NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u); |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 500 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 501 | // If an opaque output stream's endpoint is ImageReader, add |
| 502 | // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used |
| 503 | // for the ZSL use case. |
| 504 | // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set: |
| 505 | // 1. GRALLOC_USAGE_HW_TEXTURE |
| 506 | // 2. GRALLOC_USAGE_HW_RENDER |
| 507 | // 3. GRALLOC_USAGE_HW_COMPOSER |
| 508 | // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER |
| 509 | if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && |
| 510 | (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER | |
| 511 | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) { |
| 512 | u |= GRALLOC_USAGE_HW_CAMERA_ZSL; |
| 513 | } |
| 514 | |
| 515 | *usage = u; |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 516 | return res; |
| 517 | } |
| 518 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 519 | bool Camera3OutputStream::isVideoStream() const { |
| 520 | uint32_t usage = 0; |
| 521 | status_t res = getEndpointUsage(&usage); |
| 522 | if (res != OK) { |
| 523 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0; |
| 528 | } |
| 529 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 530 | status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) { |
| 531 | Mutex::Autolock l(mLock); |
| 532 | if (mState != STATE_CONSTRUCTED) { |
| 533 | ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.", |
| 534 | __FUNCTION__); |
| 535 | return INVALID_OPERATION; |
| 536 | } |
| 537 | mBufferManager = bufferManager; |
| 538 | |
| 539 | return OK; |
| 540 | } |
| 541 | |
| 542 | void Camera3OutputStream::BufferReleasedListener::onBufferReleased() { |
| 543 | sp<Camera3OutputStream> stream = mParent.promote(); |
| 544 | if (stream == nullptr) { |
| 545 | ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | Mutex::Autolock l(stream->mLock); |
| 550 | if (!(stream->mUseBufferManager)) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | sp<Fence> fence; |
| 555 | sp<GraphicBuffer> buffer; |
| 556 | int fenceFd = -1; |
| 557 | status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence); |
| 558 | if (res == NO_MEMORY) { |
| 559 | // This may rarely happen, which indicates that the released buffer was freed by other |
| 560 | // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the |
| 561 | // buffer manager that this buffer has been freed. It's not fatal, but should be avoided, |
| 562 | // therefore log a warning. |
| 563 | buffer = 0; |
| 564 | ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__); |
| 565 | } else if (res != OK) { |
| 566 | // Other errors are fatal. |
| 567 | ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 568 | stream->mState = STATE_ERROR; |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | if (fence!= 0 && fence->isValid()) { |
| 573 | fenceFd = fence->dup(); |
| 574 | } |
| 575 | res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(), |
| 576 | buffer, fenceFd); |
| 577 | if (res != OK) { |
| 578 | ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__, |
| 579 | strerror(-res), res); |
| 580 | stream->mState = STATE_ERROR; |
| 581 | } |
| 582 | } |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 583 | |
| 584 | bool Camera3OutputStream::isConsumedByHWComposer() const { |
| 585 | uint32_t usage = 0; |
| 586 | status_t res = getEndpointUsage(&usage); |
| 587 | if (res != OK) { |
| 588 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0; |
| 593 | } |
| 594 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 595 | }; // namespace camera3 |
| 596 | |
| 597 | }; // namespace android |