blob: 84c5754d45fb908eb7e8e745ce3fb12dd32dbb23 [file] [log] [blame]
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
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 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
30Camera3InputStream::Camera3InputStream(int id,
31 uint32_t width, uint32_t height, int format) :
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070032 Camera3IOStreamBase(id, CAMERA3_STREAM_INPUT, width, height, /*maxSize*/0,
33 format, HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0) {
Igor Murashkin0776a142013-04-15 14:59:22 -070034
35 if (format == HAL_PIXEL_FORMAT_BLOB) {
36 ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__);
37 mState = STATE_ERROR;
38 }
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080039}
40
Igor Murashkin0776a142013-04-15 14:59:22 -070041Camera3InputStream::~Camera3InputStream() {
42 disconnectLocked();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080043}
44
Igor Murashkin0776a142013-04-15 14:59:22 -070045status_t Camera3InputStream::getInputBufferLocked(
46 camera3_stream_buffer *buffer) {
47 ATRACE_CALL();
48 status_t res;
49
50 // FIXME: will not work in (re-)registration
51 if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) {
52 ALOGE("%s: Stream %d: Buffer registration for input streams"
53 " not implemented (state %d)",
54 __FUNCTION__, mId, mState);
55 return INVALID_OPERATION;
56 }
57
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070058 if ((res = getBufferPreconditionCheckLocked()) != OK) {
59 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -070060 }
61
62 ANativeWindowBuffer* anb;
63 int fenceFd;
64
65 assert(mConsumer != 0);
66
67 BufferItem bufferItem;
Igor Murashkin0776a142013-04-15 14:59:22 -070068
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070069 res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070070 if (res != OK) {
71 ALOGE("%s: Stream %d: Can't acquire next output buffer: %s (%d)",
72 __FUNCTION__, mId, strerror(-res), res);
73 return res;
74 }
75
76 anb = bufferItem.mGraphicBuffer->getNativeBuffer();
77 assert(anb != NULL);
78 fenceFd = bufferItem.mFence->dup();
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070079
Igor Murashkin0776a142013-04-15 14:59:22 -070080 /**
81 * FenceFD now owned by HAL except in case of error,
82 * in which case we reassign it to acquire_fence
83 */
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070084 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -070085 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/false);
Igor Murashkin0776a142013-04-15 14:59:22 -070086 mBuffersInFlight.push_back(bufferItem);
87
88 return OK;
89}
90
Igor Murashkinae3d0ba2013-05-08 18:03:15 -070091status_t Camera3InputStream::returnBufferCheckedLocked(
92 const camera3_stream_buffer &buffer,
93 nsecs_t timestamp,
94 bool output,
95 /*out*/
96 sp<Fence> *releaseFenceOut) {
97
98 (void)timestamp;
99 (void)output;
100 ALOG_ASSERT(!output, "Expected output to be false");
101
Igor Murashkin0776a142013-04-15 14:59:22 -0700102 status_t res;
103
Igor Murashkin0776a142013-04-15 14:59:22 -0700104 bool bufferFound = false;
105 BufferItem bufferItem;
106 {
107 // Find the buffer we are returning
108 Vector<BufferItem>::iterator it, end;
109 for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
110 it != end;
111 ++it) {
112
113 const BufferItem& tmp = *it;
114 ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer();
115 if (anb != NULL && &(anb->handle) == buffer.buffer) {
116 bufferFound = true;
117 bufferItem = tmp;
118 mBuffersInFlight.erase(it);
Igor Murashkin0776a142013-04-15 14:59:22 -0700119 }
120 }
121 }
122 if (!bufferFound) {
123 ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
124 __FUNCTION__, mId);
125 return INVALID_OPERATION;
126 }
127
128 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
129 if (buffer.release_fence != -1) {
130 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
131 "there is an error", __FUNCTION__, mId, buffer.release_fence);
132 close(buffer.release_fence);
133 }
134
135 /**
136 * Reassign release fence as the acquire fence incase of error
137 */
138 const_cast<camera3_stream_buffer*>(&buffer)->release_fence =
139 buffer.acquire_fence;
140 }
141
142 /**
143 * Unconditionally return buffer to the buffer queue.
144 * - Fwk takes over the release_fence ownership
145 */
146 sp<Fence> releaseFence = new Fence(buffer.release_fence);
147 res = mConsumer->releaseBuffer(bufferItem, releaseFence);
148 if (res != OK) {
149 ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:"
150 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin0776a142013-04-15 14:59:22 -0700151 }
152
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700153 *releaseFenceOut = releaseFence;
Igor Murashkin0776a142013-04-15 14:59:22 -0700154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800156}
157
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700158status_t Camera3InputStream::returnInputBufferLocked(
159 const camera3_stream_buffer &buffer) {
160 ATRACE_CALL();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800161
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700162 return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false);
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800163}
164
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700165status_t Camera3InputStream::getInputBufferProducerLocked(
166 sp<IGraphicBufferProducer> *producer) {
167 ATRACE_CALL();
168
169 if (producer == NULL) {
170 return BAD_VALUE;
171 } else if (mProducer == NULL) {
172 ALOGE("%s: No input stream is configured");
173 return INVALID_OPERATION;
174 }
175
176 *producer = mProducer;
177 return OK;
178}
179
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800180status_t Camera3InputStream::disconnectLocked() {
Igor Murashkin0776a142013-04-15 14:59:22 -0700181
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700182 status_t res;
183
184 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
185 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700186 }
187
188 assert(mBuffersInFlight.size() == 0);
189
190 /**
191 * no-op since we can't disconnect the producer from the consumer-side
192 */
193
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700194 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
195 : STATE_CONSTRUCTED;
Igor Murashkin0776a142013-04-15 14:59:22 -0700196 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800197}
198
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800199void Camera3InputStream::dump(int fd, const Vector<String16> &args) const {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800200 (void) args;
Igor Murashkin0776a142013-04-15 14:59:22 -0700201 String8 lines;
202 lines.appendFormat(" Stream[%d]: Input\n", mId);
Igor Murashkin0776a142013-04-15 14:59:22 -0700203 write(fd, lines.string(), lines.size());
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700204
205 Camera3IOStreamBase::dump(fd, args);
Igor Murashkin0776a142013-04-15 14:59:22 -0700206}
207
208status_t Camera3InputStream::configureQueueLocked() {
209 status_t res;
210
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700211 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
212 return res;
Igor Murashkin0776a142013-04-15 14:59:22 -0700213 }
214
215 assert(mMaxSize == 0);
216 assert(camera3_stream::format != HAL_PIXEL_FORMAT_BLOB);
217
Zhijun He6adc9cc2014-04-15 14:09:55 -0700218 mHandoutTotalBufferCount = 0;
Igor Murashkin0776a142013-04-15 14:59:22 -0700219 mFrameCount = 0;
220
221 if (mConsumer.get() == 0) {
Dan Stoza8aa0f062014-03-12 14:31:05 -0700222 sp<IGraphicBufferProducer> producer;
223 sp<IGraphicBufferConsumer> consumer;
224 BufferQueue::createBufferQueue(&producer, &consumer);
Igor Murashkin054aab32013-11-18 11:39:27 -0800225
226 int minUndequeuedBuffers = 0;
Dan Stoza8aa0f062014-03-12 14:31:05 -0700227 res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800228 if (res != OK || minUndequeuedBuffers < 0) {
229 ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700230 __FUNCTION__, mId, res, minUndequeuedBuffers);
Igor Murashkin054aab32013-11-18 11:39:27 -0800231 return res;
232 }
233 size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700234
235 if (camera3_stream::max_buffers == 0) {
236 ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
237 __FUNCTION__, __LINE__);
238 return INVALID_OPERATION;
239 }
240
Igor Murashkin054aab32013-11-18 11:39:27 -0800241 /*
242 * We promise never to 'acquire' more than camera3_stream::max_buffers
243 * at any one time.
244 *
245 * Boost the number up to meet the minimum required buffer count.
246 *
247 * (Note that this sets consumer-side buffer count only,
248 * and not the sum of producer+consumer side as in other camera streams).
249 */
250 mTotalBufferCount = camera3_stream::max_buffers > minBufs ?
251 camera3_stream::max_buffers : minBufs;
252 // TODO: somehow set the total buffer count when producer connects?
253
Dan Stoza8aa0f062014-03-12 14:31:05 -0700254 mConsumer = new BufferItemConsumer(consumer, camera3_stream::usage,
Mathias Agopian5e1f08b2013-07-16 22:54:39 -0700255 mTotalBufferCount);
Igor Murashkin0776a142013-04-15 14:59:22 -0700256 mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700257
258 mProducer = producer;
Igor Murashkin0776a142013-04-15 14:59:22 -0700259 }
260
261 res = mConsumer->setDefaultBufferSize(camera3_stream::width,
262 camera3_stream::height);
263 if (res != OK) {
264 ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d",
265 __FUNCTION__, mId, camera3_stream::width, camera3_stream::height);
266 return res;
267 }
268 res = mConsumer->setDefaultBufferFormat(camera3_stream::format);
269 if (res != OK) {
270 ALOGE("%s: Stream %d: Could not set buffer format %d",
271 __FUNCTION__, mId, camera3_stream::format);
272 return res;
273 }
274
275 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800276}
277
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700278status_t Camera3InputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700279 // Per HAL3 spec, input streams have 0 for their initial usage field.
280 *usage = 0;
281 return OK;
282}
283
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800284}; // namespace camera3
285
286}; // namespace android