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