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