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