blob: fc836846015fa022d2677953c0949b121af093c6 [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
Shuzhen Wangc28189a2017-11-27 23:05:10 -080030const String8 Camera3InputStream::DUMMY_ID;
31
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080032Camera3InputStream::Camera3InputStream(int id,
33 uint32_t width, uint32_t height, int format) :
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070034 Camera3IOStreamBase(id, CAMERA3_STREAM_INPUT, width, height, /*maxSize*/0,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080035 format, HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0,
36 DUMMY_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(
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 Murashkinae3d0ba2013-05-08 18:03:15 -070061 if ((res = getBufferPreconditionCheckLocked()) != OK) {
62 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -070063 }
64
65 ANativeWindowBuffer* anb;
66 int fenceFd;
67
68 assert(mConsumer != 0);
69
70 BufferItem bufferItem;
Igor Murashkin0776a142013-04-15 14:59:22 -070071
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070072 res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070073 if (res != OK) {
74 ALOGE("%s: Stream %d: Can't acquire next output buffer: %s (%d)",
75 __FUNCTION__, mId, strerror(-res), res);
76 return res;
77 }
78
79 anb = bufferItem.mGraphicBuffer->getNativeBuffer();
80 assert(anb != NULL);
81 fenceFd = bufferItem.mFence->dup();
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070082
Igor Murashkin0776a142013-04-15 14:59:22 -070083 /**
84 * FenceFD now owned by HAL except in case of error,
85 * in which case we reassign it to acquire_fence
86 */
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070087 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -070088 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070089 mBuffersInFlight.push_back(bufferItem);
90
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -080091 mFrameCount++;
92 mLastTimestamp = bufferItem.mTimestamp;
93
Igor Murashkin0776a142013-04-15 14:59:22 -070094 return OK;
95}
96
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070097status_t Camera3InputStream::returnBufferCheckedLocked(
98 const camera3_stream_buffer &buffer,
99 nsecs_t timestamp,
100 bool output,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700101 const std::vector<size_t>&,
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700102 /*out*/
103 sp<Fence> *releaseFenceOut) {
104
105 (void)timestamp;
106 (void)output;
107 ALOG_ASSERT(!output, "Expected output to be false");
108
Igor Murashkin0776a142013-04-15 14:59:22 -0700109 status_t res;
110
Igor Murashkin0776a142013-04-15 14:59:22 -0700111 bool bufferFound = false;
112 BufferItem bufferItem;
113 {
114 // Find the buffer we are returning
115 Vector<BufferItem>::iterator it, end;
116 for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
117 it != end;
118 ++it) {
119
120 const BufferItem& tmp = *it;
121 ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer();
122 if (anb != NULL && &(anb->handle) == buffer.buffer) {
123 bufferFound = true;
124 bufferItem = tmp;
125 mBuffersInFlight.erase(it);
Eino-Ville Talvala05a8cf52016-03-28 14:08:56 -0700126 break;
Igor Murashkin0776a142013-04-15 14:59:22 -0700127 }
128 }
129 }
130 if (!bufferFound) {
131 ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
132 __FUNCTION__, mId);
133 return INVALID_OPERATION;
134 }
135
136 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
137 if (buffer.release_fence != -1) {
138 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
139 "there is an error", __FUNCTION__, mId, buffer.release_fence);
140 close(buffer.release_fence);
141 }
142
143 /**
144 * Reassign release fence as the acquire fence incase of error
145 */
146 const_cast<camera3_stream_buffer*>(&buffer)->release_fence =
147 buffer.acquire_fence;
148 }
149
150 /**
151 * Unconditionally return buffer to the buffer queue.
152 * - Fwk takes over the release_fence ownership
153 */
154 sp<Fence> releaseFence = new Fence(buffer.release_fence);
155 res = mConsumer->releaseBuffer(bufferItem, releaseFence);
156 if (res != OK) {
157 ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:"
158 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin0776a142013-04-15 14:59:22 -0700159 }
160
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700161 *releaseFenceOut = releaseFence;
Igor Murashkin0776a142013-04-15 14:59:22 -0700162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700163 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800164}
165
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700166status_t Camera3InputStream::returnInputBufferLocked(
167 const camera3_stream_buffer &buffer) {
168 ATRACE_CALL();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800169
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700170 return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false);
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800171}
172
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700173status_t Camera3InputStream::getInputBufferProducerLocked(
174 sp<IGraphicBufferProducer> *producer) {
175 ATRACE_CALL();
176
177 if (producer == NULL) {
178 return BAD_VALUE;
179 } else if (mProducer == NULL) {
Zhijun He125684a2015-12-26 15:07:30 -0800180 ALOGE("%s: No input stream is configured", __FUNCTION__);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700181 return INVALID_OPERATION;
182 }
183
184 *producer = mProducer;
185 return OK;
186}
187
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800188status_t Camera3InputStream::disconnectLocked() {
Igor Murashkin0776a142013-04-15 14:59:22 -0700189
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700190 status_t res;
191
192 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
193 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700194 }
195
196 assert(mBuffersInFlight.size() == 0);
197
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700198 mConsumer->abandon();
199
Igor Murashkin0776a142013-04-15 14:59:22 -0700200 /**
201 * no-op since we can't disconnect the producer from the consumer-side
202 */
203
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700204 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
205 : STATE_CONSTRUCTED;
Igor Murashkin0776a142013-04-15 14:59:22 -0700206 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800207}
208
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800209void Camera3InputStream::dump(int fd, const Vector<String16> &args) const {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800210 (void) args;
Igor Murashkin0776a142013-04-15 14:59:22 -0700211 String8 lines;
212 lines.appendFormat(" Stream[%d]: Input\n", mId);
Igor Murashkin0776a142013-04-15 14:59:22 -0700213 write(fd, lines.string(), lines.size());
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700214
215 Camera3IOStreamBase::dump(fd, args);
Igor Murashkin0776a142013-04-15 14:59:22 -0700216}
217
218status_t Camera3InputStream::configureQueueLocked() {
219 status_t res;
220
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700221 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
222 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700223 }
224
225 assert(mMaxSize == 0);
226 assert(camera3_stream::format != HAL_PIXEL_FORMAT_BLOB);
227
Zhijun He6adc9cc2014-04-15 14:09:55 -0700228 mHandoutTotalBufferCount = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700229 mFrameCount = 0;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800230 mLastTimestamp = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700231
232 if (mConsumer.get() == 0) {
Dan Stoza8aa0f062014-03-12 14:31:05 -0700233 sp<IGraphicBufferProducer> producer;
234 sp<IGraphicBufferConsumer> consumer;
235 BufferQueue::createBufferQueue(&producer, &consumer);
Igor Murashkin054aab32013-11-18 11:39:27 -0800236
237 int minUndequeuedBuffers = 0;
Dan Stoza8aa0f062014-03-12 14:31:05 -0700238 res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800239 if (res != OK || minUndequeuedBuffers < 0) {
240 ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700241 __FUNCTION__, mId, res, minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800242 return res;
243 }
244 size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700245
246 if (camera3_stream::max_buffers == 0) {
247 ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
248 __FUNCTION__, __LINE__);
249 return INVALID_OPERATION;
250 }
251
Igor Murashkin054aab32013-11-18 11:39:27 -0800252 /*
253 * We promise never to 'acquire' more than camera3_stream::max_buffers
254 * at any one time.
255 *
256 * Boost the number up to meet the minimum required buffer count.
257 *
258 * (Note that this sets consumer-side buffer count only,
259 * and not the sum of producer+consumer side as in other camera streams).
260 */
261 mTotalBufferCount = camera3_stream::max_buffers > minBufs ?
262 camera3_stream::max_buffers : minBufs;
263 // TODO: somehow set the total buffer count when producer connects?
264
Emilian Peev050f5dc2017-05-18 14:43:56 +0100265 mConsumer = new BufferItemConsumer(consumer, mUsage,
Mathias Agopian5e1f08b2013-07-16 22:54:39 -0700266 mTotalBufferCount);
Igor Murashkin0776a142013-04-15 14:59:22 -0700267 mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700268
269 mProducer = producer;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700270
271 mConsumer->setBufferFreedListener(this);
Igor Murashkin0776a142013-04-15 14:59:22 -0700272 }
273
274 res = mConsumer->setDefaultBufferSize(camera3_stream::width,
275 camera3_stream::height);
276 if (res != OK) {
277 ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d",
278 __FUNCTION__, mId, camera3_stream::width, camera3_stream::height);
279 return res;
280 }
281 res = mConsumer->setDefaultBufferFormat(camera3_stream::format);
282 if (res != OK) {
283 ALOGE("%s: Stream %d: Could not set buffer format %d",
284 __FUNCTION__, mId, camera3_stream::format);
285 return res;
286 }
287
288 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800289}
290
Emilian Peev050f5dc2017-05-18 14:43:56 +0100291status_t Camera3InputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700292 // Per HAL3 spec, input streams have 0 for their initial usage field.
293 *usage = 0;
294 return OK;
295}
296
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700297void Camera3InputStream::onBufferFreed(const wp<GraphicBuffer>& gb) {
298 const sp<GraphicBuffer> buffer = gb.promote();
299 if (buffer != nullptr) {
Emilian Peev889234d2017-07-18 18:21:26 -0700300 camera3_stream_buffer streamBuffer =
301 {nullptr, &buffer->handle, 0, -1, -1};
302 // Check if this buffer is outstanding.
303 if (isOutstandingBuffer(streamBuffer)) {
304 ALOGV("%s: Stream %d: Trying to free a buffer that is still being "
305 "processed.", __FUNCTION__, mId);
306 return;
307 }
308
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700309 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
310 if (callback != nullptr) {
311 callback->onBufferFreed(mId, buffer->handle);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700312 }
313 } else {
314 ALOGE("%s: GraphicBuffer is freed before onBufferFreed callback finishes!", __FUNCTION__);
315 }
316}
317
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800318}; // namespace camera3
319
320}; // namespace android