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