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