blob: dcadf366954809a4040e90378f837a0d720c8aa3 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-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-OutputStream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080021#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "Camera3OutputStream.h"
24
25#ifndef container_of
26#define container_of(ptr, type, member) \
27 (type *)((char*)(ptr) - offsetof(type, member))
28#endif
29
30namespace android {
31
32namespace camera3 {
33
34Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070035 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080037 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
38 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070039 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Zhijun He125684a2015-12-26 15:07:30 -080040 /*maxSize*/0, format, dataSpace, rotation, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080041 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040042 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080043 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080044 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070045 mTimestampOffset(timestampOffset),
46 mConsumerUsage(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080047
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080048 if (mConsumer == NULL) {
49 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
50 mState = STATE_ERROR;
51 }
Zhijun He125684a2015-12-26 15:07:30 -080052
53 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
54 mBufferReleasedListener = new BufferReleasedListener(this);
55 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080056}
57
58Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070059 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080060 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080061 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
62 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070063 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080064 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070065 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040066 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080067 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080068 mUseMonoTimestamp(false),
69 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070070 mTimestampOffset(timestampOffset),
71 mConsumerUsage(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080072
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080073 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080074 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
75 format);
76 mState = STATE_ERROR;
77 }
78
79 if (mConsumer == NULL) {
80 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
81 mState = STATE_ERROR;
82 }
Zhijun He125684a2015-12-26 15:07:30 -080083
84 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
85 mBufferReleasedListener = new BufferReleasedListener(this);
86 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080087}
88
Zhijun He5d677d12016-05-29 16:52:39 -070089Camera3OutputStream::Camera3OutputStream(int id,
90 uint32_t width, uint32_t height, int format,
91 uint32_t consumerUsage, android_dataspace dataSpace,
92 camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) :
93 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
94 /*maxSize*/0, format, dataSpace, rotation, setId),
95 mConsumer(nullptr),
96 mTransform(0),
97 mTraceFirstBuffer(true),
98 mUseBufferManager(false),
99 mTimestampOffset(timestampOffset),
100 mConsumerUsage(consumerUsage) {
101 // Deferred consumer only support preview surface format now.
102 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
103 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
104 __FUNCTION__);
105 mState = STATE_ERROR;
106 }
107
108 // Sanity check for the consumer usage flag.
109 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
110 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
111 ALOGE("%s: Deferred consumer usage flag is illegal (0x%x)!", __FUNCTION__, consumerUsage);
112 mState = STATE_ERROR;
113 }
114
115 mConsumerName = String8("Deferred");
116 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
117 mBufferReleasedListener = new BufferReleasedListener(this);
118 }
119
120}
121
Igor Murashkine3a9f962013-05-08 18:03:15 -0700122Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
123 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800124 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700125 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800126 camera3_stream_rotation_t rotation,
127 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700128 Camera3IOStreamBase(id, type, width, height,
129 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -0800130 format, dataSpace, rotation, setId),
131 mTransform(0),
132 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800133 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700134 mUseBufferManager(false),
135 mConsumerUsage(0) {
Zhijun He125684a2015-12-26 15:07:30 -0800136
137 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
138 mBufferReleasedListener = new BufferReleasedListener(this);
139 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700140
141 // Subclasses expected to initialize mConsumer themselves
142}
143
144
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800145Camera3OutputStream::~Camera3OutputStream() {
146 disconnectLocked();
147}
148
149status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
150 ATRACE_CALL();
151 status_t res;
152
Igor Murashkine3a9f962013-05-08 18:03:15 -0700153 if ((res = getBufferPreconditionCheckLocked()) != OK) {
154 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800155 }
156
157 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800158 int fenceFd = -1;
159 if (mUseBufferManager) {
160 sp<GraphicBuffer> gb;
161 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, &fenceFd);
162 if (res != OK) {
163 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
164 __FUNCTION__, mId, strerror(-res), res);
165 return res;
166 }
167 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
168 // successful return.
169 anb = gb.get();
170 res = mConsumer->attachBuffer(anb);
171 if (res != OK) {
172 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
173 __FUNCTION__, mId, strerror(-res), res);
174 return res;
175 }
176 } else {
177 /**
178 * Release the lock briefly to avoid deadlock for below scenario:
179 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
180 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
181 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
182 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
183 * StreamingProcessor lock.
184 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
185 * and try to lock bufferQueue lock.
186 * Then there is circular locking dependency.
187 */
188 sp<ANativeWindow> currentConsumer = mConsumer;
189 mLock.unlock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800190
Zhijun He125684a2015-12-26 15:07:30 -0800191 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
192 mLock.lock();
193 if (res != OK) {
194 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
195 __FUNCTION__, mId, strerror(-res), res);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700196
197 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
198 // let prepareNextBuffer handle the error.)
199 if (res == NO_INIT && mState == STATE_CONFIGURED) {
200 mState = STATE_ABANDONED;
201 }
202
Zhijun He125684a2015-12-26 15:07:30 -0800203 return res;
204 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800205 }
206
Igor Murashkine3a9f962013-05-08 18:03:15 -0700207 /**
208 * FenceFD now owned by HAL except in case of error,
209 * in which case we reassign it to acquire_fence
210 */
211 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700212 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800213
214 return OK;
215}
216
217status_t Camera3OutputStream::returnBufferLocked(
218 const camera3_stream_buffer &buffer,
219 nsecs_t timestamp) {
220 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700221
222 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
223
224 if (res != OK) {
225 return res;
226 }
227
228 mLastTimestamp = timestamp;
229
230 return OK;
231}
232
233status_t Camera3OutputStream::returnBufferCheckedLocked(
234 const camera3_stream_buffer &buffer,
235 nsecs_t timestamp,
236 bool output,
237 /*out*/
238 sp<Fence> *releaseFenceOut) {
239
240 (void)output;
241 ALOG_ASSERT(output, "Expected output to be true");
242
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800243 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700244
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800245 // Fence management - always honor release fence from HAL
246 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700247 int anwReleaseFence = releaseFence->dup();
248
249 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700250 * Release the lock briefly to avoid deadlock with
251 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
252 * thread will go into StreamingProcessor::onFrameAvailable) during
253 * queueBuffer
254 */
255 sp<ANativeWindow> currentConsumer = mConsumer;
256 mLock.unlock();
257
258 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700259 * Return buffer back to ANativeWindow
260 */
261 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
262 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700263 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700264 container_of(buffer.buffer, ANativeWindowBuffer, handle),
265 anwReleaseFence);
266 if (res != OK) {
267 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700268 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700269 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800270
271 if (mUseBufferManager) {
272 // Return this buffer back to buffer manager.
273 mBufferReleasedListener->onBufferReleased();
274 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700275 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400276 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
277 {
278 char traceLog[48];
279 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
280 ATRACE_NAME(traceLog);
281 }
282 mTraceFirstBuffer = false;
283 }
284
Shuzhen Wang13a69632016-01-26 09:51:07 -0800285 /* Certain consumers (such as AudioSource or HardwareComposer) use
286 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800287 * in BOOTTIME. Do the conversion if necessary. */
288 res = native_window_set_buffers_timestamp(mConsumer.get(),
289 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
290 if (res != OK) {
291 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
292 __FUNCTION__, mId, strerror(-res), res);
293 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800294 }
295
Zhijun He124ccf42013-05-22 14:01:30 -0700296 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800297 container_of(buffer.buffer, ANativeWindowBuffer, handle),
298 anwReleaseFence);
299 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700300 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
301 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800302 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800303 }
Zhijun He124ccf42013-05-22 14:01:30 -0700304 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700305
306 // Once a valid buffer has been returned to the queue, can no longer
307 // dequeue all buffers for preallocation.
308 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
309 mStreamUnpreparable = true;
310 }
311
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700312 if (res != OK) {
313 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700314 }
315
Igor Murashkine3a9f962013-05-08 18:03:15 -0700316 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800317
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700318 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319}
320
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800321void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
322 (void) args;
323 String8 lines;
324 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700325 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700327
328 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800329}
330
331status_t Camera3OutputStream::setTransform(int transform) {
332 ATRACE_CALL();
333 Mutex::Autolock l(mLock);
334 return setTransformLocked(transform);
335}
336
337status_t Camera3OutputStream::setTransformLocked(int transform) {
338 status_t res = OK;
339 if (mState == STATE_ERROR) {
340 ALOGE("%s: Stream in error state", __FUNCTION__);
341 return INVALID_OPERATION;
342 }
343
344 mTransform = transform;
345 if (mState == STATE_CONFIGURED) {
346 res = native_window_set_buffers_transform(mConsumer.get(),
347 transform);
348 if (res != OK) {
349 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
350 __FUNCTION__, transform, strerror(-res), res);
351 }
352 }
353 return res;
354}
355
356status_t Camera3OutputStream::configureQueueLocked() {
357 status_t res;
358
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400359 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700360 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
361 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800362 }
363
Igor Murashkine3a9f962013-05-08 18:03:15 -0700364 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
365
Zhijun He125684a2015-12-26 15:07:30 -0800366 // Configure consumer-side ANativeWindow interface. The listener may be used
367 // to notify buffer manager (if it is used) of the returned buffers.
368 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800369 if (res != OK) {
370 ALOGE("%s: Unable to connect to native window for stream %d",
371 __FUNCTION__, mId);
372 return res;
373 }
374
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700375 mConsumerName = mConsumer->getConsumerName();
376
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
378 if (res != OK) {
379 ALOGE("%s: Unable to configure usage %08x for stream %d",
380 __FUNCTION__, camera3_stream::usage, mId);
381 return res;
382 }
383
384 res = native_window_set_scaling_mode(mConsumer.get(),
385 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
386 if (res != OK) {
387 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
388 __FUNCTION__, strerror(-res), res);
389 return res;
390 }
391
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800392 if (mMaxSize == 0) {
393 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700394 res = native_window_set_buffers_dimensions(mConsumer.get(),
395 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800396 } else {
397 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700398 res = native_window_set_buffers_dimensions(mConsumer.get(),
399 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800400 }
401 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700402 ALOGE("%s: Unable to configure stream buffer dimensions"
403 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800404 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700405 mMaxSize, mId);
406 return res;
407 }
408 res = native_window_set_buffers_format(mConsumer.get(),
409 camera3_stream::format);
410 if (res != OK) {
411 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
412 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800413 return res;
414 }
415
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800416 res = native_window_set_buffers_data_space(mConsumer.get(),
417 camera3_stream::data_space);
418 if (res != OK) {
419 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
420 __FUNCTION__, camera3_stream::data_space, mId);
421 return res;
422 }
423
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800424 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700425 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
426 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800427 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
428 if (res != OK) {
429 ALOGE("%s: Unable to query consumer undequeued"
430 " buffer count for stream %d", __FUNCTION__, mId);
431 return res;
432 }
433
Alex Ray20cb3002013-05-28 20:18:22 -0700434 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
435 maxConsumerBuffers, camera3_stream::max_buffers);
436 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700437 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700438 __FUNCTION__, camera3_stream::max_buffers);
439 return INVALID_OPERATION;
440 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800441
442 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700443 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800444 mFrameCount = 0;
445 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800446 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800447
448 res = native_window_set_buffer_count(mConsumer.get(),
449 mTotalBufferCount);
450 if (res != OK) {
451 ALOGE("%s: Unable to set buffer count for stream %d",
452 __FUNCTION__, mId);
453 return res;
454 }
455
456 res = native_window_set_buffers_transform(mConsumer.get(),
457 mTransform);
458 if (res != OK) {
459 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
460 __FUNCTION__, mTransform, strerror(-res), res);
461 }
462
Zhijun He125684a2015-12-26 15:07:30 -0800463 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800464 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800465 * buffers to be statically allocated for internal static buffer registration, while the
466 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800467 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
468 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
469 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800470 */
471 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
472 StreamInfo streamInfo(
473 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
474 camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true);
475 res = mBufferManager->registerStream(streamInfo);
476 if (res == OK) {
477 // Disable buffer allocation for this BufferQueue, buffer manager will take over
478 // the buffer allocation responsibility.
479 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
480 mUseBufferManager = true;
481 } else {
482 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
483 "(error %d %s), fall back to BufferQueue for buffer management!",
484 __FUNCTION__, mId, res, strerror(-res));
485 }
486 }
487
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800488 return OK;
489}
490
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800491status_t Camera3OutputStream::disconnectLocked() {
492 status_t res;
493
Igor Murashkine3a9f962013-05-08 18:03:15 -0700494 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
495 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800496 }
497
Zhijun He5d677d12016-05-29 16:52:39 -0700498 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
499 // state), don't need change the stream state, return OK.
500 if (mConsumer == nullptr) {
501 return OK;
502 }
503
Zhijun He125684a2015-12-26 15:07:30 -0800504 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
505
Igor Murashkine3a9f962013-05-08 18:03:15 -0700506 res = native_window_api_disconnect(mConsumer.get(),
507 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800508 /**
509 * This is not an error. if client calling process dies, the window will
510 * also die and all calls to it will return DEAD_OBJECT, thus it's already
511 * "disconnected"
512 */
513 if (res == DEAD_OBJECT) {
514 ALOGW("%s: While disconnecting stream %d from native window, the"
515 " native window died from under us", __FUNCTION__, mId);
516 }
517 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700518 ALOGE("%s: Unable to disconnect stream %d from native window "
519 "(error %d %s)",
520 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800521 mState = STATE_ERROR;
522 return res;
523 }
524
Zhijun He125684a2015-12-26 15:07:30 -0800525 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
526 // stream at this point should be safe.
527 if (mUseBufferManager) {
528 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
529 if (res != OK) {
530 ALOGE("%s: Unable to unregister stream %d from buffer manager "
531 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
532 mState = STATE_ERROR;
533 return res;
534 }
535 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
536 // the stream is still in usable state after this call.
537 mUseBufferManager = false;
538 }
539
Igor Murashkine3a9f962013-05-08 18:03:15 -0700540 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
541 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800542 return OK;
543}
544
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700545status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700546
547 status_t res;
548 int32_t u = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700549 if (mConsumer == nullptr) {
550 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
551 *usage = mConsumerUsage;
552 return OK;
553 }
554
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700555 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700556 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700557
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700558 // If an opaque output stream's endpoint is ImageReader, add
559 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
560 // for the ZSL use case.
561 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
562 // 1. GRALLOC_USAGE_HW_TEXTURE
563 // 2. GRALLOC_USAGE_HW_RENDER
564 // 3. GRALLOC_USAGE_HW_COMPOSER
565 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
566 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
567 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
568 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
569 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
570 }
571
572 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700573 return res;
574}
575
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700576bool Camera3OutputStream::isVideoStream() const {
577 uint32_t usage = 0;
578 status_t res = getEndpointUsage(&usage);
579 if (res != OK) {
580 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
581 return false;
582 }
583
584 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
585}
586
Zhijun He125684a2015-12-26 15:07:30 -0800587status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
588 Mutex::Autolock l(mLock);
589 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700590 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800591 __FUNCTION__);
592 return INVALID_OPERATION;
593 }
594 mBufferManager = bufferManager;
595
596 return OK;
597}
598
599void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
600 sp<Camera3OutputStream> stream = mParent.promote();
601 if (stream == nullptr) {
602 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
603 return;
604 }
605
606 Mutex::Autolock l(stream->mLock);
607 if (!(stream->mUseBufferManager)) {
608 return;
609 }
610
611 sp<Fence> fence;
612 sp<GraphicBuffer> buffer;
613 int fenceFd = -1;
614 status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence);
615 if (res == NO_MEMORY) {
616 // This may rarely happen, which indicates that the released buffer was freed by other
617 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
618 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
619 // therefore log a warning.
620 buffer = 0;
621 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
622 } else if (res != OK) {
623 // Other errors are fatal.
624 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
625 stream->mState = STATE_ERROR;
626 return;
627 }
628
629 if (fence!= 0 && fence->isValid()) {
630 fenceFd = fence->dup();
631 }
632 res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(),
633 buffer, fenceFd);
634 if (res != OK) {
635 ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__,
636 strerror(-res), res);
637 stream->mState = STATE_ERROR;
638 }
639}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800640
Zhijun He5d677d12016-05-29 16:52:39 -0700641bool Camera3OutputStream::isConsumerConfigurationDeferred() const {
642 Mutex::Autolock l(mLock);
643 return mConsumer == nullptr;
644}
645
646status_t Camera3OutputStream::setConsumer(sp<Surface> consumer) {
647 if (consumer == nullptr) {
648 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
649 return INVALID_OPERATION;
650 }
651
652 if (mConsumer != nullptr) {
653 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
654 return INVALID_OPERATION;
655 }
656
657 mConsumer = consumer;
658 return OK;
659}
660
Shuzhen Wang13a69632016-01-26 09:51:07 -0800661bool Camera3OutputStream::isConsumedByHWComposer() const {
662 uint32_t usage = 0;
663 status_t res = getEndpointUsage(&usage);
664 if (res != OK) {
665 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
666 return false;
667 }
668
669 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
670}
671
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800672}; // namespace camera3
673
674}; // namespace android