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