blob: 999f563ce85c3c526cc4855542ff24f523f7af6e [file] [log] [blame]
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -08003 *
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 Shargo2e0202f2024-07-30 19:54:43 +000021#include <camera/StringUtils.h>
22#include <com_android_graphics_libgui_flags.h>
Dan Stoza549e7352015-03-12 15:21:16 -070023#include <gui/BufferItem.h>
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080024#include <utils/Log.h>
25#include <utils/Trace.h>
Jim Shargo2e0202f2024-07-30 19:54:43 +000026
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080027#include "Camera3InputStream.h"
28
29namespace android {
30
31namespace camera3 {
32
Austin Borger1c1bee02023-06-01 16:51:35 -070033const std::string Camera3InputStream::FAKE_ID;
Shuzhen Wangc28189a2017-11-27 23:05:10 -080034
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080035Camera3InputStream::Camera3InputStream(int id,
36 uint32_t width, uint32_t height, int format) :
Emilian Peevf4816702020-04-03 15:44:51 -070037 Camera3IOStreamBase(id, CAMERA_STREAM_INPUT, width, height, /*maxSize*/0,
38 format, HAL_DATASPACE_UNKNOWN, CAMERA_STREAM_ROTATION_0,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080039 FAKE_ID,
40 std::unordered_set<int32_t>{ANDROID_SENSOR_PIXEL_MODE_DEFAULT}) {
Igor Murashkin0776a142013-04-15 14:59:22 -070041
42 if (format == HAL_PIXEL_FORMAT_BLOB) {
43 ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__);
44 mState = STATE_ERROR;
45 }
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080046}
47
Igor Murashkin0776a142013-04-15 14:59:22 -070048Camera3InputStream::~Camera3InputStream() {
49 disconnectLocked();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080050}
51
Igor Murashkin0776a142013-04-15 14:59:22 -070052status_t Camera3InputStream::getInputBufferLocked(
Shuzhen Wang83bff122020-11-20 15:51:39 -080053 camera_stream_buffer *buffer, Size *size) {
Igor Murashkin0776a142013-04-15 14:59:22 -070054 ATRACE_CALL();
55 status_t res;
56
Shuzhen Wang83bff122020-11-20 15:51:39 -080057 if (size == nullptr) {
58 ALOGE("%s: size must not be null", __FUNCTION__);
59 return BAD_VALUE;
60 }
Igor Murashkin0776a142013-04-15 14:59:22 -070061 // 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 Murashkinae3d0ba2013-05-08 18:03:15 -070069 if ((res = getBufferPreconditionCheckLocked()) != OK) {
70 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -070071 }
72
73 ANativeWindowBuffer* anb;
74 int fenceFd;
75
76 assert(mConsumer != 0);
77
78 BufferItem bufferItem;
Igor Murashkin0776a142013-04-15 14:59:22 -070079
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070080 res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070081 if (res != OK) {
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -070082 // 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 Murashkin0776a142013-04-15 14:59:22 -070084 __FUNCTION__, mId, strerror(-res), res);
85 return res;
86 }
87
Shuzhen Wang83bff122020-11-20 15:51:39 -080088 size->width = bufferItem.mGraphicBuffer->getWidth();
89 size->height = bufferItem.mGraphicBuffer->getHeight();
90
Igor Murashkin0776a142013-04-15 14:59:22 -070091 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 Murashkinae3d0ba2013-05-08 18:03:15 -070098 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Emilian Peevf4816702020-04-03 15:44:51 -070099 /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -0700100 mBuffersInFlight.push_back(bufferItem);
101
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800102 mFrameCount++;
103 mLastTimestamp = bufferItem.mTimestamp;
104
Igor Murashkin0776a142013-04-15 14:59:22 -0700105 return OK;
106}
107
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700108status_t Camera3InputStream::returnBufferCheckedLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700109 const camera_stream_buffer &buffer,
Jing Mikec7f9b132023-03-12 11:12:04 +0800110 [[maybe_unused]] nsecs_t timestamp,
111 [[maybe_unused]] nsecs_t readoutTimestamp,
112 [[maybe_unused]] bool output,
Emilian Peev5104fe92021-10-21 14:27:09 -0700113 int32_t /*transform*/,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700114 const std::vector<size_t>&,
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700115 /*out*/
116 sp<Fence> *releaseFenceOut) {
117
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700118 ALOG_ASSERT(!output, "Expected output to be false");
119
Igor Murashkin0776a142013-04-15 14:59:22 -0700120 status_t res;
121
Igor Murashkin0776a142013-04-15 14:59:22 -0700122 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 Talvala05a8cf52016-03-28 14:08:56 -0700137 break;
Igor Murashkin0776a142013-04-15 14:59:22 -0700138 }
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 Peevf4816702020-04-03 15:44:51 -0700147 if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) {
Igor Murashkin0776a142013-04-15 14:59:22 -0700148 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 Peevf4816702020-04-03 15:44:51 -0700157 const_cast<camera_stream_buffer*>(&buffer)->release_fence =
Igor Murashkin0776a142013-04-15 14:59:22 -0700158 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 Murashkin0776a142013-04-15 14:59:22 -0700170 }
171
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700172 *releaseFenceOut = releaseFence;
Igor Murashkin0776a142013-04-15 14:59:22 -0700173
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700174 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800175}
176
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700177status_t Camera3InputStream::returnInputBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700178 const camera_stream_buffer &buffer) {
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700179 ATRACE_CALL();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800180
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700181 return returnAnyBufferLocked(buffer, /*timestamp*/0, /*readoutTimestamp*/0,
182 /*output*/false, /*transform*/ -1);
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800183}
184
Carlos Martinez Romero8e776102024-08-21 12:49:10 -0700185#if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
186status_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 Chen618ff8a2015-03-13 11:27:17 -0700200status_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 He125684a2015-12-26 15:07:30 -0800207 ALOGE("%s: No input stream is configured", __FUNCTION__);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700208 return INVALID_OPERATION;
209 }
210
211 *producer = mProducer;
212 return OK;
213}
Carlos Martinez Romero8e776102024-08-21 12:49:10 -0700214#endif
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700215
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800216status_t Camera3InputStream::disconnectLocked() {
Igor Murashkin0776a142013-04-15 14:59:22 -0700217
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700218 status_t res;
219
220 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
221 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700222 }
223
224 assert(mBuffersInFlight.size() == 0);
225
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700226 mConsumer->abandon();
227
Igor Murashkin0776a142013-04-15 14:59:22 -0700228 /**
229 * no-op since we can't disconnect the producer from the consumer-side
230 */
231
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700232 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
233 : STATE_CONSTRUCTED;
Igor Murashkin0776a142013-04-15 14:59:22 -0700234 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800235}
236
Emilian Peev3b93acb2024-03-11 21:09:48 +0000237void Camera3InputStream::dump(int fd, [[maybe_unused]] const Vector<String16> &args) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700238 std::string lines;
239 lines += fmt::sprintf(" Stream[%d]: Input\n", mId);
240 write(fd, lines.c_str(), lines.size());
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700241
242 Camera3IOStreamBase::dump(fd, args);
Igor Murashkin0776a142013-04-15 14:59:22 -0700243}
244
245status_t Camera3InputStream::configureQueueLocked() {
246 status_t res;
247
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700248 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
249 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700250 }
251
252 assert(mMaxSize == 0);
Emilian Peevf4816702020-04-03 15:44:51 -0700253 assert(camera_stream::format != HAL_PIXEL_FORMAT_BLOB);
Igor Murashkin0776a142013-04-15 14:59:22 -0700254
Zhijun He6adc9cc2014-04-15 14:09:55 -0700255 mHandoutTotalBufferCount = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700256 mFrameCount = 0;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800257 mLastTimestamp = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700258
259 if (mConsumer.get() == 0) {
Jim Shargo2e0202f2024-07-30 19:54:43 +0000260#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 Stoza8aa0f062014-03-12 14:31:05 -0700265 sp<IGraphicBufferProducer> producer;
266 sp<IGraphicBufferConsumer> consumer;
267 BufferQueue::createBufferQueue(&producer, &consumer);
Jim Shargo2e0202f2024-07-30 19:54:43 +0000268#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Igor Murashkin054aab32013-11-18 11:39:27 -0800269
270 int minUndequeuedBuffers = 0;
Dan Stoza8aa0f062014-03-12 14:31:05 -0700271 res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800272 if (res != OK || minUndequeuedBuffers < 0) {
273 ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700274 __FUNCTION__, mId, res, minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800275 return res;
276 }
277 size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700278
Emilian Peevf4816702020-04-03 15:44:51 -0700279 if (camera_stream::max_buffers == 0) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700280 ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
281 __FUNCTION__, __LINE__);
282 return INVALID_OPERATION;
283 }
284
Igor Murashkin054aab32013-11-18 11:39:27 -0800285 /*
Emilian Peevf4816702020-04-03 15:44:51 -0700286 * We promise never to 'acquire' more than camera_stream::max_buffers
Igor Murashkin054aab32013-11-18 11:39:27 -0800287 * 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 Peevf4816702020-04-03 15:44:51 -0700294 mTotalBufferCount = camera_stream::max_buffers > minBufs ?
295 camera_stream::max_buffers : minBufs;
Igor Murashkin054aab32013-11-18 11:39:27 -0800296 // TODO: somehow set the total buffer count when producer connects?
297
Jim Shargo2e0202f2024-07-30 19:54:43 +0000298#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 Romero8e776102024-08-21 12:49:10 -0700303#if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
304 mSurface = mConsumer->getSurface();
305#else
Jim Shargo2e0202f2024-07-30 19:54:43 +0000306 mProducer = mConsumer->getSurface()->getIGraphicBufferProducer();
Carlos Martinez Romero8e776102024-08-21 12:49:10 -0700307#endif // WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
308
Jim Shargo2e0202f2024-07-30 19:54:43 +0000309#else
Emilian Peev050f5dc2017-05-18 14:43:56 +0100310 mConsumer = new BufferItemConsumer(consumer, mUsage,
Mathias Agopian5e1f08b2013-07-16 22:54:39 -0700311 mTotalBufferCount);
Igor Murashkin0776a142013-04-15 14:59:22 -0700312 mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700313
314 mProducer = producer;
Jim Shargo2e0202f2024-07-30 19:54:43 +0000315#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700316
317 mConsumer->setBufferFreedListener(this);
Igor Murashkin0776a142013-04-15 14:59:22 -0700318 }
319
Emilian Peevf4816702020-04-03 15:44:51 -0700320 res = mConsumer->setDefaultBufferSize(camera_stream::width,
321 camera_stream::height);
Igor Murashkin0776a142013-04-15 14:59:22 -0700322 if (res != OK) {
323 ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d",
Emilian Peevf4816702020-04-03 15:44:51 -0700324 __FUNCTION__, mId, camera_stream::width, camera_stream::height);
Igor Murashkin0776a142013-04-15 14:59:22 -0700325 return res;
326 }
Emilian Peevf4816702020-04-03 15:44:51 -0700327 res = mConsumer->setDefaultBufferFormat(camera_stream::format);
Igor Murashkin0776a142013-04-15 14:59:22 -0700328 if (res != OK) {
329 ALOGE("%s: Stream %d: Could not set buffer format %d",
Emilian Peevf4816702020-04-03 15:44:51 -0700330 __FUNCTION__, mId, camera_stream::format);
Igor Murashkin0776a142013-04-15 14:59:22 -0700331 return res;
332 }
333
334 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800335}
336
Emilian Peev3b93acb2024-03-11 21:09:48 +0000337status_t Camera3InputStream::getEndpointUsage(uint64_t *usage) {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700338 // Per HAL3 spec, input streams have 0 for their initial usage field.
339 *usage = 0;
340 return OK;
341}
342
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700343void Camera3InputStream::onBufferFreed(const wp<GraphicBuffer>& gb) {
344 const sp<GraphicBuffer> buffer = gb.promote();
345 if (buffer != nullptr) {
Emilian Peevf4816702020-04-03 15:44:51 -0700346 camera_stream_buffer streamBuffer =
347 {nullptr, &buffer->handle, CAMERA_BUFFER_STATUS_OK, -1, -1};
Emilian Peev889234d2017-07-18 18:21:26 -0700348 // 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 Yehdb1e8642017-07-14 15:19:30 -0700355 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
356 if (callback != nullptr) {
357 callback->onBufferFreed(mId, buffer->handle);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700358 }
359 } else {
360 ALOGE("%s: GraphicBuffer is freed before onBufferFreed callback finishes!", __FUNCTION__);
361 }
362}
363
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800364}; // namespace camera3
365
366}; // namespace android