blob: b00a963f64fd85e26be031cec8e95cdaec293cbc [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
Dan Stoza549e7352015-03-12 15:21:16 -070021#include <gui/BufferItem.h>
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080022#include <utils/Log.h>
23#include <utils/Trace.h>
24#include "Camera3InputStream.h"
25
26namespace android {
27
28namespace camera3 {
29
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040030const String8 Camera3InputStream::FAKE_ID;
Shuzhen Wangc28189a2017-11-27 23:05:10 -080031
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080032Camera3InputStream::Camera3InputStream(int id,
33 uint32_t width, uint32_t height, int format) :
Emilian Peevf4816702020-04-03 15:44:51 -070034 Camera3IOStreamBase(id, CAMERA_STREAM_INPUT, width, height, /*maxSize*/0,
35 format, HAL_DATASPACE_UNKNOWN, CAMERA_STREAM_ROTATION_0,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040036 FAKE_ID) {
Igor Murashkin0776a142013-04-15 14:59:22 -070037
38 if (format == HAL_PIXEL_FORMAT_BLOB) {
39 ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__);
40 mState = STATE_ERROR;
41 }
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080042}
43
Igor Murashkin0776a142013-04-15 14:59:22 -070044Camera3InputStream::~Camera3InputStream() {
45 disconnectLocked();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080046}
47
Igor Murashkin0776a142013-04-15 14:59:22 -070048status_t Camera3InputStream::getInputBufferLocked(
Shuzhen Wang83bff122020-11-20 15:51:39 -080049 camera_stream_buffer *buffer, Size *size) {
Igor Murashkin0776a142013-04-15 14:59:22 -070050 ATRACE_CALL();
51 status_t res;
52
Shuzhen Wang83bff122020-11-20 15:51:39 -080053 if (size == nullptr) {
54 ALOGE("%s: size must not be null", __FUNCTION__);
55 return BAD_VALUE;
56 }
Igor Murashkin0776a142013-04-15 14:59:22 -070057 // FIXME: will not work in (re-)registration
58 if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) {
59 ALOGE("%s: Stream %d: Buffer registration for input streams"
60 " not implemented (state %d)",
61 __FUNCTION__, mId, mState);
62 return INVALID_OPERATION;
63 }
64
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070065 if ((res = getBufferPreconditionCheckLocked()) != OK) {
66 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -070067 }
68
69 ANativeWindowBuffer* anb;
70 int fenceFd;
71
72 assert(mConsumer != 0);
73
74 BufferItem bufferItem;
Igor Murashkin0776a142013-04-15 14:59:22 -070075
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070076 res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070077 if (res != OK) {
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -070078 // This may or may not be an error condition depending on caller.
79 ALOGV("%s: Stream %d: Can't acquire next output buffer: %s (%d)",
Igor Murashkin0776a142013-04-15 14:59:22 -070080 __FUNCTION__, mId, strerror(-res), res);
81 return res;
82 }
83
Shuzhen Wang83bff122020-11-20 15:51:39 -080084 size->width = bufferItem.mGraphicBuffer->getWidth();
85 size->height = bufferItem.mGraphicBuffer->getHeight();
86
Igor Murashkin0776a142013-04-15 14:59:22 -070087 anb = bufferItem.mGraphicBuffer->getNativeBuffer();
88 assert(anb != NULL);
89 fenceFd = bufferItem.mFence->dup();
90 /**
91 * FenceFD now owned by HAL except in case of error,
92 * in which case we reassign it to acquire_fence
93 */
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070094 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Emilian Peevf4816702020-04-03 15:44:51 -070095 /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070096 mBuffersInFlight.push_back(bufferItem);
97
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -080098 mFrameCount++;
99 mLastTimestamp = bufferItem.mTimestamp;
100
Igor Murashkin0776a142013-04-15 14:59:22 -0700101 return OK;
102}
103
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700104status_t Camera3InputStream::returnBufferCheckedLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700105 const camera_stream_buffer &buffer,
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700106 nsecs_t timestamp,
107 bool output,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700108 const std::vector<size_t>&,
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700109 /*out*/
110 sp<Fence> *releaseFenceOut) {
111
112 (void)timestamp;
113 (void)output;
114 ALOG_ASSERT(!output, "Expected output to be false");
115
Igor Murashkin0776a142013-04-15 14:59:22 -0700116 status_t res;
117
Igor Murashkin0776a142013-04-15 14:59:22 -0700118 bool bufferFound = false;
119 BufferItem bufferItem;
120 {
121 // Find the buffer we are returning
122 Vector<BufferItem>::iterator it, end;
123 for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
124 it != end;
125 ++it) {
126
127 const BufferItem& tmp = *it;
128 ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer();
129 if (anb != NULL && &(anb->handle) == buffer.buffer) {
130 bufferFound = true;
131 bufferItem = tmp;
132 mBuffersInFlight.erase(it);
Eino-Ville Talvala05a8cf52016-03-28 14:08:56 -0700133 break;
Igor Murashkin0776a142013-04-15 14:59:22 -0700134 }
135 }
136 }
137 if (!bufferFound) {
138 ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
139 __FUNCTION__, mId);
140 return INVALID_OPERATION;
141 }
142
Emilian Peevf4816702020-04-03 15:44:51 -0700143 if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) {
Igor Murashkin0776a142013-04-15 14:59:22 -0700144 if (buffer.release_fence != -1) {
145 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
146 "there is an error", __FUNCTION__, mId, buffer.release_fence);
147 close(buffer.release_fence);
148 }
149
150 /**
151 * Reassign release fence as the acquire fence incase of error
152 */
Emilian Peevf4816702020-04-03 15:44:51 -0700153 const_cast<camera_stream_buffer*>(&buffer)->release_fence =
Igor Murashkin0776a142013-04-15 14:59:22 -0700154 buffer.acquire_fence;
155 }
156
157 /**
158 * Unconditionally return buffer to the buffer queue.
159 * - Fwk takes over the release_fence ownership
160 */
161 sp<Fence> releaseFence = new Fence(buffer.release_fence);
162 res = mConsumer->releaseBuffer(bufferItem, releaseFence);
163 if (res != OK) {
164 ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:"
165 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin0776a142013-04-15 14:59:22 -0700166 }
167
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700168 *releaseFenceOut = releaseFence;
Igor Murashkin0776a142013-04-15 14:59:22 -0700169
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700170 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800171}
172
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700173status_t Camera3InputStream::returnInputBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700174 const camera_stream_buffer &buffer) {
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700175 ATRACE_CALL();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800176
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700177 return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false);
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800178}
179
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700180status_t Camera3InputStream::getInputBufferProducerLocked(
181 sp<IGraphicBufferProducer> *producer) {
182 ATRACE_CALL();
183
184 if (producer == NULL) {
185 return BAD_VALUE;
186 } else if (mProducer == NULL) {
Zhijun He125684a2015-12-26 15:07:30 -0800187 ALOGE("%s: No input stream is configured", __FUNCTION__);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700188 return INVALID_OPERATION;
189 }
190
191 *producer = mProducer;
192 return OK;
193}
194
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800195status_t Camera3InputStream::disconnectLocked() {
Igor Murashkin0776a142013-04-15 14:59:22 -0700196
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700197 status_t res;
198
199 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
200 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700201 }
202
203 assert(mBuffersInFlight.size() == 0);
204
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700205 mConsumer->abandon();
206
Igor Murashkin0776a142013-04-15 14:59:22 -0700207 /**
208 * no-op since we can't disconnect the producer from the consumer-side
209 */
210
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700211 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
212 : STATE_CONSTRUCTED;
Igor Murashkin0776a142013-04-15 14:59:22 -0700213 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800214}
215
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800216void Camera3InputStream::dump(int fd, const Vector<String16> &args) const {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800217 (void) args;
Igor Murashkin0776a142013-04-15 14:59:22 -0700218 String8 lines;
219 lines.appendFormat(" Stream[%d]: Input\n", mId);
Igor Murashkin0776a142013-04-15 14:59:22 -0700220 write(fd, lines.string(), lines.size());
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700221
222 Camera3IOStreamBase::dump(fd, args);
Igor Murashkin0776a142013-04-15 14:59:22 -0700223}
224
225status_t Camera3InputStream::configureQueueLocked() {
226 status_t res;
227
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700228 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
229 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700230 }
231
232 assert(mMaxSize == 0);
Emilian Peevf4816702020-04-03 15:44:51 -0700233 assert(camera_stream::format != HAL_PIXEL_FORMAT_BLOB);
Igor Murashkin0776a142013-04-15 14:59:22 -0700234
Zhijun He6adc9cc2014-04-15 14:09:55 -0700235 mHandoutTotalBufferCount = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700236 mFrameCount = 0;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800237 mLastTimestamp = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700238
239 if (mConsumer.get() == 0) {
Dan Stoza8aa0f062014-03-12 14:31:05 -0700240 sp<IGraphicBufferProducer> producer;
241 sp<IGraphicBufferConsumer> consumer;
242 BufferQueue::createBufferQueue(&producer, &consumer);
Igor Murashkin054aab32013-11-18 11:39:27 -0800243
244 int minUndequeuedBuffers = 0;
Dan Stoza8aa0f062014-03-12 14:31:05 -0700245 res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800246 if (res != OK || minUndequeuedBuffers < 0) {
247 ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700248 __FUNCTION__, mId, res, minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800249 return res;
250 }
251 size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700252
Emilian Peevf4816702020-04-03 15:44:51 -0700253 if (camera_stream::max_buffers == 0) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700254 ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
255 __FUNCTION__, __LINE__);
256 return INVALID_OPERATION;
257 }
258
Igor Murashkin054aab32013-11-18 11:39:27 -0800259 /*
Emilian Peevf4816702020-04-03 15:44:51 -0700260 * We promise never to 'acquire' more than camera_stream::max_buffers
Igor Murashkin054aab32013-11-18 11:39:27 -0800261 * at any one time.
262 *
263 * Boost the number up to meet the minimum required buffer count.
264 *
265 * (Note that this sets consumer-side buffer count only,
266 * and not the sum of producer+consumer side as in other camera streams).
267 */
Emilian Peevf4816702020-04-03 15:44:51 -0700268 mTotalBufferCount = camera_stream::max_buffers > minBufs ?
269 camera_stream::max_buffers : minBufs;
Igor Murashkin054aab32013-11-18 11:39:27 -0800270 // TODO: somehow set the total buffer count when producer connects?
271
Emilian Peev050f5dc2017-05-18 14:43:56 +0100272 mConsumer = new BufferItemConsumer(consumer, mUsage,
Mathias Agopian5e1f08b2013-07-16 22:54:39 -0700273 mTotalBufferCount);
Igor Murashkin0776a142013-04-15 14:59:22 -0700274 mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700275
276 mProducer = producer;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700277
278 mConsumer->setBufferFreedListener(this);
Igor Murashkin0776a142013-04-15 14:59:22 -0700279 }
280
Emilian Peevf4816702020-04-03 15:44:51 -0700281 res = mConsumer->setDefaultBufferSize(camera_stream::width,
282 camera_stream::height);
Igor Murashkin0776a142013-04-15 14:59:22 -0700283 if (res != OK) {
284 ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d",
Emilian Peevf4816702020-04-03 15:44:51 -0700285 __FUNCTION__, mId, camera_stream::width, camera_stream::height);
Igor Murashkin0776a142013-04-15 14:59:22 -0700286 return res;
287 }
Emilian Peevf4816702020-04-03 15:44:51 -0700288 res = mConsumer->setDefaultBufferFormat(camera_stream::format);
Igor Murashkin0776a142013-04-15 14:59:22 -0700289 if (res != OK) {
290 ALOGE("%s: Stream %d: Could not set buffer format %d",
Emilian Peevf4816702020-04-03 15:44:51 -0700291 __FUNCTION__, mId, camera_stream::format);
Igor Murashkin0776a142013-04-15 14:59:22 -0700292 return res;
293 }
294
295 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800296}
297
Emilian Peev050f5dc2017-05-18 14:43:56 +0100298status_t Camera3InputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700299 // Per HAL3 spec, input streams have 0 for their initial usage field.
300 *usage = 0;
301 return OK;
302}
303
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700304void Camera3InputStream::onBufferFreed(const wp<GraphicBuffer>& gb) {
305 const sp<GraphicBuffer> buffer = gb.promote();
306 if (buffer != nullptr) {
Emilian Peevf4816702020-04-03 15:44:51 -0700307 camera_stream_buffer streamBuffer =
308 {nullptr, &buffer->handle, CAMERA_BUFFER_STATUS_OK, -1, -1};
Emilian Peev889234d2017-07-18 18:21:26 -0700309 // Check if this buffer is outstanding.
310 if (isOutstandingBuffer(streamBuffer)) {
311 ALOGV("%s: Stream %d: Trying to free a buffer that is still being "
312 "processed.", __FUNCTION__, mId);
313 return;
314 }
315
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700316 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
317 if (callback != nullptr) {
318 callback->onBufferFreed(mId, buffer->handle);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700319 }
320 } else {
321 ALOGE("%s: GraphicBuffer is freed before onBufferFreed callback finishes!", __FUNCTION__);
322 }
323}
324
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800325}; // namespace camera3
326
327}; // namespace android