Eino-Ville Talvala | 8be20f5 | 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 | 8be20f5 | 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-InputStream" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 21 | #include <camera/StringUtils.h> |
| 22 | #include <com_android_graphics_libgui_flags.h> |
Dan Stoza | 549e735 | 2015-03-12 15:21:16 -0700 | [diff] [blame] | 23 | #include <gui/BufferItem.h> |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/Trace.h> |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 26 | |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 27 | #include "Camera3InputStream.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | namespace camera3 { |
| 32 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 33 | const std::string Camera3InputStream::FAKE_ID; |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 34 | |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 35 | Camera3InputStream::Camera3InputStream(int id, |
| 36 | uint32_t width, uint32_t height, int format) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 37 | Camera3IOStreamBase(id, CAMERA_STREAM_INPUT, width, height, /*maxSize*/0, |
| 38 | format, HAL_DATASPACE_UNKNOWN, CAMERA_STREAM_ROTATION_0, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 39 | FAKE_ID, |
| 40 | std::unordered_set<int32_t>{ANDROID_SENSOR_PIXEL_MODE_DEFAULT}) { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 41 | |
| 42 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
| 43 | ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__); |
| 44 | mState = STATE_ERROR; |
| 45 | } |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 48 | Camera3InputStream::~Camera3InputStream() { |
| 49 | disconnectLocked(); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 52 | status_t Camera3InputStream::getInputBufferLocked( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 53 | camera_stream_buffer *buffer, Size *size) { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 54 | ATRACE_CALL(); |
| 55 | status_t res; |
| 56 | |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 57 | if (size == nullptr) { |
| 58 | ALOGE("%s: size must not be null", __FUNCTION__); |
| 59 | return BAD_VALUE; |
| 60 | } |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 61 | // FIXME: will not work in (re-)registration |
| 62 | if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) { |
| 63 | ALOGE("%s: Stream %d: Buffer registration for input streams" |
| 64 | " not implemented (state %d)", |
| 65 | __FUNCTION__, mId, mState); |
| 66 | return INVALID_OPERATION; |
| 67 | } |
| 68 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 69 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 70 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ANativeWindowBuffer* anb; |
| 74 | int fenceFd; |
| 75 | |
| 76 | assert(mConsumer != 0); |
| 77 | |
| 78 | BufferItem bufferItem; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 79 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 80 | res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 81 | if (res != OK) { |
Shuzhen Wang | 0cf01cb | 2019-09-05 13:33:06 -0700 | [diff] [blame] | 82 | // This may or may not be an error condition depending on caller. |
| 83 | ALOGV("%s: Stream %d: Can't acquire next output buffer: %s (%d)", |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 84 | __FUNCTION__, mId, strerror(-res), res); |
| 85 | return res; |
| 86 | } |
| 87 | |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 88 | size->width = bufferItem.mGraphicBuffer->getWidth(); |
| 89 | size->height = bufferItem.mGraphicBuffer->getHeight(); |
| 90 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 91 | anb = bufferItem.mGraphicBuffer->getNativeBuffer(); |
| 92 | assert(anb != NULL); |
| 93 | fenceFd = bufferItem.mFence->dup(); |
| 94 | /** |
| 95 | * FenceFD now owned by HAL except in case of error, |
| 96 | * in which case we reassign it to acquire_fence |
| 97 | */ |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 98 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 99 | /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/false); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 100 | mBuffersInFlight.push_back(bufferItem); |
| 101 | |
Eino-Ville Talvala | c31dc7e | 2017-01-31 17:35:41 -0800 | [diff] [blame] | 102 | mFrameCount++; |
| 103 | mLastTimestamp = bufferItem.mTimestamp; |
| 104 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 105 | return OK; |
| 106 | } |
| 107 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 108 | status_t Camera3InputStream::returnBufferCheckedLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 109 | const camera_stream_buffer &buffer, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 110 | [[maybe_unused]] nsecs_t timestamp, |
| 111 | [[maybe_unused]] nsecs_t readoutTimestamp, |
| 112 | [[maybe_unused]] bool output, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 113 | int32_t /*transform*/, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 114 | const std::vector<size_t>&, |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 115 | /*out*/ |
| 116 | sp<Fence> *releaseFenceOut) { |
| 117 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 118 | ALOG_ASSERT(!output, "Expected output to be false"); |
| 119 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 120 | status_t res; |
| 121 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 122 | bool bufferFound = false; |
| 123 | BufferItem bufferItem; |
| 124 | { |
| 125 | // Find the buffer we are returning |
| 126 | Vector<BufferItem>::iterator it, end; |
| 127 | for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end(); |
| 128 | it != end; |
| 129 | ++it) { |
| 130 | |
| 131 | const BufferItem& tmp = *it; |
| 132 | ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer(); |
| 133 | if (anb != NULL && &(anb->handle) == buffer.buffer) { |
| 134 | bufferFound = true; |
| 135 | bufferItem = tmp; |
| 136 | mBuffersInFlight.erase(it); |
Eino-Ville Talvala | 05a8cf5 | 2016-03-28 14:08:56 -0700 | [diff] [blame] | 137 | break; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | if (!bufferFound) { |
| 142 | ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL", |
| 143 | __FUNCTION__, mId); |
| 144 | return INVALID_OPERATION; |
| 145 | } |
| 146 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 147 | if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 148 | if (buffer.release_fence != -1) { |
| 149 | ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when " |
| 150 | "there is an error", __FUNCTION__, mId, buffer.release_fence); |
| 151 | close(buffer.release_fence); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Reassign release fence as the acquire fence incase of error |
| 156 | */ |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 157 | const_cast<camera_stream_buffer*>(&buffer)->release_fence = |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 158 | buffer.acquire_fence; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Unconditionally return buffer to the buffer queue. |
| 163 | * - Fwk takes over the release_fence ownership |
| 164 | */ |
| 165 | sp<Fence> releaseFence = new Fence(buffer.release_fence); |
| 166 | res = mConsumer->releaseBuffer(bufferItem, releaseFence); |
| 167 | if (res != OK) { |
| 168 | ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:" |
| 169 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 172 | *releaseFenceOut = releaseFence; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 173 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 174 | return res; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 177 | status_t Camera3InputStream::returnInputBufferLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 178 | const camera_stream_buffer &buffer) { |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 179 | ATRACE_CALL(); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 180 | |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 181 | return returnAnyBufferLocked(buffer, /*timestamp*/0, /*readoutTimestamp*/0, |
| 182 | /*output*/false, /*transform*/ -1); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Carlos Martinez Romero | 8e77610 | 2024-08-21 12:49:10 -0700 | [diff] [blame] | 185 | #if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES |
| 186 | status_t Camera3InputStream::getInputSurfaceLocked(sp<Surface> *surface) { |
| 187 | ATRACE_CALL(); |
| 188 | |
| 189 | if (surface == NULL) { |
| 190 | return BAD_VALUE; |
| 191 | } else if (mSurface == NULL) { |
| 192 | ALOGE("%s: No input stream is configured", __FUNCTION__); |
| 193 | return INVALID_OPERATION; |
| 194 | } |
| 195 | |
| 196 | *surface = mSurface; |
| 197 | return OK; |
| 198 | } |
| 199 | #else |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 200 | status_t Camera3InputStream::getInputBufferProducerLocked( |
| 201 | sp<IGraphicBufferProducer> *producer) { |
| 202 | ATRACE_CALL(); |
| 203 | |
| 204 | if (producer == NULL) { |
| 205 | return BAD_VALUE; |
| 206 | } else if (mProducer == NULL) { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 207 | ALOGE("%s: No input stream is configured", __FUNCTION__); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 208 | return INVALID_OPERATION; |
| 209 | } |
| 210 | |
| 211 | *producer = mProducer; |
| 212 | return OK; |
| 213 | } |
Carlos Martinez Romero | 8e77610 | 2024-08-21 12:49:10 -0700 | [diff] [blame] | 214 | #endif |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 215 | |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 216 | status_t Camera3InputStream::disconnectLocked() { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 217 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 218 | status_t res; |
| 219 | |
| 220 | if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { |
| 221 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | assert(mBuffersInFlight.size() == 0); |
| 225 | |
Chien-Yu Chen | ed0412e | 2015-04-27 15:04:22 -0700 | [diff] [blame] | 226 | mConsumer->abandon(); |
| 227 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 228 | /** |
| 229 | * no-op since we can't disconnect the producer from the consumer-side |
| 230 | */ |
| 231 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 232 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG |
| 233 | : STATE_CONSTRUCTED; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 234 | return OK; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 237 | void Camera3InputStream::dump(int fd, [[maybe_unused]] const Vector<String16> &args) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 238 | std::string lines; |
| 239 | lines += fmt::sprintf(" Stream[%d]: Input\n", mId); |
| 240 | write(fd, lines.c_str(), lines.size()); |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 241 | |
| 242 | Camera3IOStreamBase::dump(fd, args); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | status_t Camera3InputStream::configureQueueLocked() { |
| 246 | status_t res; |
| 247 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 248 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 249 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | assert(mMaxSize == 0); |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 253 | assert(camera_stream::format != HAL_PIXEL_FORMAT_BLOB); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 254 | |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 255 | mHandoutTotalBufferCount = 0; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 256 | mFrameCount = 0; |
Eino-Ville Talvala | c31dc7e | 2017-01-31 17:35:41 -0800 | [diff] [blame] | 257 | mLastTimestamp = 0; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 258 | |
| 259 | if (mConsumer.get() == 0) { |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 260 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 261 | sp<BufferItemConsumer> bufferItemConsumer = new BufferItemConsumer(mUsage); |
| 262 | sp<IGraphicBufferProducer> producer = |
| 263 | bufferItemConsumer->getSurface()->getIGraphicBufferProducer(); |
| 264 | #else |
Dan Stoza | 8aa0f06 | 2014-03-12 14:31:05 -0700 | [diff] [blame] | 265 | sp<IGraphicBufferProducer> producer; |
| 266 | sp<IGraphicBufferConsumer> consumer; |
| 267 | BufferQueue::createBufferQueue(&producer, &consumer); |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 268 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 269 | |
| 270 | int minUndequeuedBuffers = 0; |
Dan Stoza | 8aa0f06 | 2014-03-12 14:31:05 -0700 | [diff] [blame] | 271 | res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers); |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 272 | if (res != OK || minUndequeuedBuffers < 0) { |
| 273 | ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)", |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 274 | __FUNCTION__, mId, res, minUndequeuedBuffers); |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 275 | return res; |
| 276 | } |
| 277 | size_t minBufs = static_cast<size_t>(minUndequeuedBuffers); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 278 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 279 | if (camera_stream::max_buffers == 0) { |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 280 | ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.", |
| 281 | __FUNCTION__, __LINE__); |
| 282 | return INVALID_OPERATION; |
| 283 | } |
| 284 | |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 285 | /* |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 286 | * We promise never to 'acquire' more than camera_stream::max_buffers |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 287 | * at any one time. |
| 288 | * |
| 289 | * Boost the number up to meet the minimum required buffer count. |
| 290 | * |
| 291 | * (Note that this sets consumer-side buffer count only, |
| 292 | * and not the sum of producer+consumer side as in other camera streams). |
| 293 | */ |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 294 | mTotalBufferCount = camera_stream::max_buffers > minBufs ? |
| 295 | camera_stream::max_buffers : minBufs; |
Igor Murashkin | 054aab3 | 2013-11-18 11:39:27 -0800 | [diff] [blame] | 296 | // TODO: somehow set the total buffer count when producer connects? |
| 297 | |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 298 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 299 | mConsumer = bufferItemConsumer; |
| 300 | mConsumer->setName(String8::format("Camera3-InputStream-%d", mId)); |
| 301 | mConsumer->setMaxAcquiredBufferCount(mTotalBufferCount); |
| 302 | |
Carlos Martinez Romero | 8e77610 | 2024-08-21 12:49:10 -0700 | [diff] [blame] | 303 | #if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES |
| 304 | mSurface = mConsumer->getSurface(); |
| 305 | #else |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 306 | mProducer = mConsumer->getSurface()->getIGraphicBufferProducer(); |
Carlos Martinez Romero | 8e77610 | 2024-08-21 12:49:10 -0700 | [diff] [blame] | 307 | #endif // WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES |
| 308 | |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 309 | #else |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 310 | mConsumer = new BufferItemConsumer(consumer, mUsage, |
Mathias Agopian | 5e1f08b | 2013-07-16 22:54:39 -0700 | [diff] [blame] | 311 | mTotalBufferCount); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 312 | mConsumer->setName(String8::format("Camera3-InputStream-%d", mId)); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 313 | |
| 314 | mProducer = producer; |
Jim Shargo | 2e0202f | 2024-07-30 19:54:43 +0000 | [diff] [blame] | 315 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 316 | |
| 317 | mConsumer->setBufferFreedListener(this); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 320 | res = mConsumer->setDefaultBufferSize(camera_stream::width, |
| 321 | camera_stream::height); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 322 | if (res != OK) { |
| 323 | ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 324 | __FUNCTION__, mId, camera_stream::width, camera_stream::height); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 325 | return res; |
| 326 | } |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 327 | res = mConsumer->setDefaultBufferFormat(camera_stream::format); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 328 | if (res != OK) { |
| 329 | ALOGE("%s: Stream %d: Could not set buffer format %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 330 | __FUNCTION__, mId, camera_stream::format); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 331 | return res; |
| 332 | } |
| 333 | |
| 334 | return OK; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 337 | status_t Camera3InputStream::getEndpointUsage(uint64_t *usage) { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 338 | // Per HAL3 spec, input streams have 0 for their initial usage field. |
| 339 | *usage = 0; |
| 340 | return OK; |
| 341 | } |
| 342 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 343 | void Camera3InputStream::onBufferFreed(const wp<GraphicBuffer>& gb) { |
| 344 | const sp<GraphicBuffer> buffer = gb.promote(); |
| 345 | if (buffer != nullptr) { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 346 | camera_stream_buffer streamBuffer = |
| 347 | {nullptr, &buffer->handle, CAMERA_BUFFER_STATUS_OK, -1, -1}; |
Emilian Peev | 889234d | 2017-07-18 18:21:26 -0700 | [diff] [blame] | 348 | // Check if this buffer is outstanding. |
| 349 | if (isOutstandingBuffer(streamBuffer)) { |
| 350 | ALOGV("%s: Stream %d: Trying to free a buffer that is still being " |
| 351 | "processed.", __FUNCTION__, mId); |
| 352 | return; |
| 353 | } |
| 354 | |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 355 | sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote(); |
| 356 | if (callback != nullptr) { |
| 357 | callback->onBufferFreed(mId, buffer->handle); |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 358 | } |
| 359 | } else { |
| 360 | ALOGE("%s: GraphicBuffer is freed before onBufferFreed callback finishes!", __FUNCTION__); |
| 361 | } |
| 362 | } |
| 363 | |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 364 | }; // namespace camera3 |
| 365 | |
| 366 | }; // namespace android |