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