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