blob: 51dc20a12c6f329185b6b8dedb81a7099b155b07 [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,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700127 uint32_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800128 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700129 Camera3IOStreamBase(id, type, width, height,
130 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -0800131 format, dataSpace, rotation, setId),
132 mTransform(0),
133 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800134 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700135 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700136 mTimestampOffset(timestampOffset),
137 mConsumerUsage(consumerUsage) {
Zhijun He125684a2015-12-26 15:07:30 -0800138
139 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
140 mBufferReleasedListener = new BufferReleasedListener(this);
141 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700142
143 // Subclasses expected to initialize mConsumer themselves
144}
145
146
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800147Camera3OutputStream::~Camera3OutputStream() {
148 disconnectLocked();
149}
150
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800151status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
152 const std::vector<size_t>&) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800153 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800154
155 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800156 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700157
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800158 status_t res;
159 res = getBufferLockedCommon(&anb, &fenceFd);
160 if (res != OK) {
161 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800162 }
163
Igor Murashkine3a9f962013-05-08 18:03:15 -0700164 /**
165 * FenceFD now owned by HAL except in case of error,
166 * in which case we reassign it to acquire_fence
167 */
168 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700169 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800170
171 return OK;
172}
173
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800174status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
175 ANativeWindowBuffer* buffer, int anwReleaseFence) {
176 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
177}
178
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800179status_t Camera3OutputStream::returnBufferLocked(
180 const camera3_stream_buffer &buffer,
181 nsecs_t timestamp) {
182 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700183
184 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
185
186 if (res != OK) {
187 return res;
188 }
189
190 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800191 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700192
193 return OK;
194}
195
196status_t Camera3OutputStream::returnBufferCheckedLocked(
197 const camera3_stream_buffer &buffer,
198 nsecs_t timestamp,
199 bool output,
200 /*out*/
201 sp<Fence> *releaseFenceOut) {
202
203 (void)output;
204 ALOG_ASSERT(output, "Expected output to be true");
205
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800206 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700207
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800208 // Fence management - always honor release fence from HAL
209 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700210 int anwReleaseFence = releaseFence->dup();
211
212 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700213 * Release the lock briefly to avoid deadlock with
214 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
215 * thread will go into StreamingProcessor::onFrameAvailable) during
216 * queueBuffer
217 */
218 sp<ANativeWindow> currentConsumer = mConsumer;
219 mLock.unlock();
220
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800221 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700222 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700223 * Return buffer back to ANativeWindow
224 */
225 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
226 // Cancel buffer
Zhijun He7b171a92016-06-24 16:05:52 -0700227 ALOGW("A frame is dropped for stream %d", mId);
Zhijun He124ccf42013-05-22 14:01:30 -0700228 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800229 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700230 anwReleaseFence);
231 if (res != OK) {
232 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700233 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700234 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800235
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800236 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800237 if (mUseBufferManager) {
238 // Return this buffer back to buffer manager.
239 mBufferReleasedListener->onBufferReleased();
240 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700241 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400242 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
243 {
244 char traceLog[48];
245 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
246 ATRACE_NAME(traceLog);
247 }
248 mTraceFirstBuffer = false;
249 }
250
Shuzhen Wang13a69632016-01-26 09:51:07 -0800251 /* Certain consumers (such as AudioSource or HardwareComposer) use
252 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800253 * in BOOTTIME. Do the conversion if necessary. */
254 res = native_window_set_buffers_timestamp(mConsumer.get(),
255 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
256 if (res != OK) {
257 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
258 __FUNCTION__, mId, strerror(-res), res);
259 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800260 }
261
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800262 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800263 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700264 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
265 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800266 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800267 }
Zhijun He124ccf42013-05-22 14:01:30 -0700268 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700269
270 // Once a valid buffer has been returned to the queue, can no longer
271 // dequeue all buffers for preallocation.
272 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
273 mStreamUnpreparable = true;
274 }
275
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700276 if (res != OK) {
277 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700278 }
279
Igor Murashkine3a9f962013-05-08 18:03:15 -0700280 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800283}
284
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800285void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
286 (void) args;
287 String8 lines;
288 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700289 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800290 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700291
292 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800293}
294
295status_t Camera3OutputStream::setTransform(int transform) {
296 ATRACE_CALL();
297 Mutex::Autolock l(mLock);
298 return setTransformLocked(transform);
299}
300
301status_t Camera3OutputStream::setTransformLocked(int transform) {
302 status_t res = OK;
303 if (mState == STATE_ERROR) {
304 ALOGE("%s: Stream in error state", __FUNCTION__);
305 return INVALID_OPERATION;
306 }
307
308 mTransform = transform;
309 if (mState == STATE_CONFIGURED) {
310 res = native_window_set_buffers_transform(mConsumer.get(),
311 transform);
312 if (res != OK) {
313 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
314 __FUNCTION__, transform, strerror(-res), res);
315 }
316 }
317 return res;
318}
319
320status_t Camera3OutputStream::configureQueueLocked() {
321 status_t res;
322
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400323 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700324 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
325 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 }
327
Shuzhen Wang0129d522016-10-30 22:43:41 -0700328 if ((res = configureConsumerQueueLocked()) != OK) {
329 return res;
330 }
331
332 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
333 // We need skip these cases as timeout will disable the non-blocking (async) mode.
334 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
335 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
336 }
337
338 return OK;
339}
340
341status_t Camera3OutputStream::configureConsumerQueueLocked() {
342 status_t res;
343
344 mTraceFirstBuffer = true;
345
Igor Murashkine3a9f962013-05-08 18:03:15 -0700346 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
347
Zhijun He125684a2015-12-26 15:07:30 -0800348 // Configure consumer-side ANativeWindow interface. The listener may be used
349 // to notify buffer manager (if it is used) of the returned buffers.
350 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800351 if (res != OK) {
352 ALOGE("%s: Unable to connect to native window for stream %d",
353 __FUNCTION__, mId);
354 return res;
355 }
356
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700357 mConsumerName = mConsumer->getConsumerName();
358
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800359 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
360 if (res != OK) {
361 ALOGE("%s: Unable to configure usage %08x for stream %d",
362 __FUNCTION__, camera3_stream::usage, mId);
363 return res;
364 }
365
366 res = native_window_set_scaling_mode(mConsumer.get(),
367 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
368 if (res != OK) {
369 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
370 __FUNCTION__, strerror(-res), res);
371 return res;
372 }
373
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800374 if (mMaxSize == 0) {
375 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700376 res = native_window_set_buffers_dimensions(mConsumer.get(),
377 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800378 } else {
379 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700380 res = native_window_set_buffers_dimensions(mConsumer.get(),
381 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800382 }
383 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700384 ALOGE("%s: Unable to configure stream buffer dimensions"
385 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800386 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700387 mMaxSize, mId);
388 return res;
389 }
390 res = native_window_set_buffers_format(mConsumer.get(),
391 camera3_stream::format);
392 if (res != OK) {
393 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
394 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800395 return res;
396 }
397
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800398 res = native_window_set_buffers_data_space(mConsumer.get(),
399 camera3_stream::data_space);
400 if (res != OK) {
401 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
402 __FUNCTION__, camera3_stream::data_space, mId);
403 return res;
404 }
405
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800406 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700407 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
408 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800409 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
410 if (res != OK) {
411 ALOGE("%s: Unable to query consumer undequeued"
412 " buffer count for stream %d", __FUNCTION__, mId);
413 return res;
414 }
415
Alex Ray20cb3002013-05-28 20:18:22 -0700416 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
417 maxConsumerBuffers, camera3_stream::max_buffers);
418 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700419 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700420 __FUNCTION__, camera3_stream::max_buffers);
421 return INVALID_OPERATION;
422 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800423
424 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700425 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800426 mFrameCount = 0;
427 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800428 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800429
430 res = native_window_set_buffer_count(mConsumer.get(),
431 mTotalBufferCount);
432 if (res != OK) {
433 ALOGE("%s: Unable to set buffer count for stream %d",
434 __FUNCTION__, mId);
435 return res;
436 }
437
438 res = native_window_set_buffers_transform(mConsumer.get(),
439 mTransform);
440 if (res != OK) {
441 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
442 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700443 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700444 }
445
Zhijun He125684a2015-12-26 15:07:30 -0800446 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800447 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800448 * buffers to be statically allocated for internal static buffer registration, while the
449 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800450 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
451 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
452 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800453 */
454 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700455 uint32_t consumerUsage = 0;
456 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800457 StreamInfo streamInfo(
458 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700459 camera3_stream::usage | consumerUsage, mTotalBufferCount,
460 /*isConfigured*/true);
461 wp<Camera3OutputStream> weakThis(this);
462 res = mBufferManager->registerStream(weakThis,
463 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800464 if (res == OK) {
465 // Disable buffer allocation for this BufferQueue, buffer manager will take over
466 // the buffer allocation responsibility.
467 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
468 mUseBufferManager = true;
469 } else {
470 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
471 "(error %d %s), fall back to BufferQueue for buffer management!",
472 __FUNCTION__, mId, res, strerror(-res));
473 }
474 }
475
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800476 return OK;
477}
478
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800479status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
480 ATRACE_CALL();
481 status_t res;
482
483 if ((res = getBufferPreconditionCheckLocked()) != OK) {
484 return res;
485 }
486
487 bool gotBufferFromManager = false;
488
489 if (mUseBufferManager) {
490 sp<GraphicBuffer> gb;
491 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
492 if (res == OK) {
493 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
494 // successful return.
495 *anb = gb.get();
496 res = mConsumer->attachBuffer(*anb);
497 if (res != OK) {
498 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
499 __FUNCTION__, mId, strerror(-res), res);
500 return res;
501 }
502 gotBufferFromManager = true;
503 ALOGV("Stream %d: Attached new buffer", getId());
504 } else if (res == ALREADY_EXISTS) {
505 // Have sufficient free buffers already attached, can just
506 // dequeue from buffer queue
507 ALOGV("Stream %d: Reusing attached buffer", getId());
508 gotBufferFromManager = false;
509 } else if (res != OK) {
510 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
511 __FUNCTION__, mId, strerror(-res), res);
512 return res;
513 }
514 }
515 if (!gotBufferFromManager) {
516 /**
517 * Release the lock briefly to avoid deadlock for below scenario:
518 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
519 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
520 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
521 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
522 * StreamingProcessor lock.
523 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
524 * and try to lock bufferQueue lock.
525 * Then there is circular locking dependency.
526 */
527 sp<ANativeWindow> currentConsumer = mConsumer;
528 mLock.unlock();
529
530 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
531 mLock.lock();
532 if (res != OK) {
533 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
534 __FUNCTION__, mId, strerror(-res), res);
535
536 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
537 // let prepareNextBuffer handle the error.)
538 if (res == NO_INIT && mState == STATE_CONFIGURED) {
539 mState = STATE_ABANDONED;
540 }
541
542 return res;
543 }
544 }
545
546 return res;
547}
548
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800549status_t Camera3OutputStream::disconnectLocked() {
550 status_t res;
551
Igor Murashkine3a9f962013-05-08 18:03:15 -0700552 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
553 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800554 }
555
Zhijun He5d677d12016-05-29 16:52:39 -0700556 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
557 // state), don't need change the stream state, return OK.
558 if (mConsumer == nullptr) {
559 return OK;
560 }
561
Zhijun He125684a2015-12-26 15:07:30 -0800562 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
563
Igor Murashkine3a9f962013-05-08 18:03:15 -0700564 res = native_window_api_disconnect(mConsumer.get(),
565 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800566 /**
567 * This is not an error. if client calling process dies, the window will
568 * also die and all calls to it will return DEAD_OBJECT, thus it's already
569 * "disconnected"
570 */
571 if (res == DEAD_OBJECT) {
572 ALOGW("%s: While disconnecting stream %d from native window, the"
573 " native window died from under us", __FUNCTION__, mId);
574 }
575 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700576 ALOGE("%s: Unable to disconnect stream %d from native window "
577 "(error %d %s)",
578 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800579 mState = STATE_ERROR;
580 return res;
581 }
582
Zhijun He125684a2015-12-26 15:07:30 -0800583 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
584 // stream at this point should be safe.
585 if (mUseBufferManager) {
586 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
587 if (res != OK) {
588 ALOGE("%s: Unable to unregister stream %d from buffer manager "
589 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
590 mState = STATE_ERROR;
591 return res;
592 }
593 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
594 // the stream is still in usable state after this call.
595 mUseBufferManager = false;
596 }
597
Igor Murashkine3a9f962013-05-08 18:03:15 -0700598 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
599 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800600 return OK;
601}
602
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700603status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700604
605 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700606
Zhijun He5d677d12016-05-29 16:52:39 -0700607 if (mConsumer == nullptr) {
608 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
609 *usage = mConsumerUsage;
610 return OK;
611 }
612
Shuzhen Wang0129d522016-10-30 22:43:41 -0700613 res = getEndpointUsageForSurface(usage, mConsumer);
614
615 return res;
616}
617
618status_t Camera3OutputStream::getEndpointUsageForSurface(uint32_t *usage,
619 const sp<Surface>& surface) const {
620 status_t res;
621 int32_t u = 0;
622
623 res = static_cast<ANativeWindow*>(surface.get())->query(surface.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700624 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700625
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700626 // If an opaque output stream's endpoint is ImageReader, add
627 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
628 // for the ZSL use case.
629 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
630 // 1. GRALLOC_USAGE_HW_TEXTURE
631 // 2. GRALLOC_USAGE_HW_RENDER
632 // 3. GRALLOC_USAGE_HW_COMPOSER
633 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
634 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
Shuzhen Wang0129d522016-10-30 22:43:41 -0700635 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
636 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700637 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
638 }
639
640 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700641 return res;
642}
643
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700644bool Camera3OutputStream::isVideoStream() const {
645 uint32_t usage = 0;
646 status_t res = getEndpointUsage(&usage);
647 if (res != OK) {
648 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
649 return false;
650 }
651
652 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
653}
654
Zhijun He125684a2015-12-26 15:07:30 -0800655status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
656 Mutex::Autolock l(mLock);
657 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700658 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800659 __FUNCTION__);
660 return INVALID_OPERATION;
661 }
662 mBufferManager = bufferManager;
663
664 return OK;
665}
666
667void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
668 sp<Camera3OutputStream> stream = mParent.promote();
669 if (stream == nullptr) {
670 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
671 return;
672 }
673
674 Mutex::Autolock l(stream->mLock);
675 if (!(stream->mUseBufferManager)) {
676 return;
677 }
678
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700679 ALOGV("Stream %d: Buffer released", stream->getId());
680 status_t res = stream->mBufferManager->onBufferReleased(
681 stream->getId(), stream->getStreamSetId());
682 if (res != OK) {
683 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
684 strerror(-res), res);
685 stream->mState = STATE_ERROR;
686 }
687}
688
689status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
690 Mutex::Autolock l(mLock);
691
692 ALOGV("Stream %d: detachBuffer", getId());
693 if (buffer == nullptr) {
694 return BAD_VALUE;
695 }
696
Zhijun He125684a2015-12-26 15:07:30 -0800697 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700698 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800699 if (res == NO_MEMORY) {
700 // This may rarely happen, which indicates that the released buffer was freed by other
701 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
702 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
703 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700704 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800705 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
706 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700707 // Treat other errors as abandonment
Zhijun He125684a2015-12-26 15:07:30 -0800708 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700709 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700710 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800711 }
712
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700713 if (fenceFd != nullptr) {
714 if (fence!= 0 && fence->isValid()) {
715 *fenceFd = fence->dup();
716 } else {
717 *fenceFd = -1;
718 }
Zhijun He125684a2015-12-26 15:07:30 -0800719 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700720
721 return OK;
Zhijun He125684a2015-12-26 15:07:30 -0800722}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800723
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800724status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700725 return OK;
726}
727
728bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700729 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700730
731 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800732 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700733 }
Zhijun He5d677d12016-05-29 16:52:39 -0700734 return mConsumer == nullptr;
735}
736
Shuzhen Wang758c2152017-01-10 18:26:18 -0800737status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800738 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800739 if (consumers.size() != 1) {
740 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
741 __FUNCTION__, consumers.size());
742 return INVALID_OPERATION;
743 }
744 if (consumers[0] == nullptr) {
745 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700746 return INVALID_OPERATION;
747 }
748
749 if (mConsumer != nullptr) {
750 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
751 return INVALID_OPERATION;
752 }
753
Shuzhen Wang758c2152017-01-10 18:26:18 -0800754 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700755 return OK;
756}
757
Shuzhen Wang13a69632016-01-26 09:51:07 -0800758bool Camera3OutputStream::isConsumedByHWComposer() const {
759 uint32_t usage = 0;
760 status_t res = getEndpointUsage(&usage);
761 if (res != OK) {
762 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
763 return false;
764 }
765
766 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
767}
768
Zhijun Hef0645c12016-08-02 00:58:11 -0700769bool Camera3OutputStream::isConsumedByHWTexture() const {
770 uint32_t usage = 0;
771 status_t res = getEndpointUsage(&usage);
772 if (res != OK) {
773 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
774 return false;
775 }
776
777 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
778}
779
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800780}; // namespace camera3
781
782}; // namespace android