blob: 1c7758143ea208f52eb43bc8d9985caaf7f7cdca [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-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 Talvalafd58f1a2013-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-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,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080038 nsecs_t timestampOffset, const String8& physicalCameraId,
39 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070040 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080041 /*maxSize*/0, format, dataSpace, rotation,
42 physicalCameraId, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080043 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040044 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080045 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080046 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070047 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070048 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070049 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070050 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080051
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080052 if (mConsumer == NULL) {
53 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
54 mState = STATE_ERROR;
55 }
Zhijun He125684a2015-12-26 15:07:30 -080056
57 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
58 mBufferReleasedListener = new BufferReleasedListener(this);
59 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080060}
61
62Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070063 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080064 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080065 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080066 nsecs_t timestampOffset, const String8& physicalCameraId, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070067 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080068 format, dataSpace, rotation, physicalCameraId, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070069 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040070 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080071 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 mUseMonoTimestamp(false),
73 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070074 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070075 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070076 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070077 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080078
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080079 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080080 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
81 format);
82 mState = STATE_ERROR;
83 }
84
85 if (mConsumer == NULL) {
86 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
87 mState = STATE_ERROR;
88 }
Zhijun He125684a2015-12-26 15:07:30 -080089
90 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
91 mBufferReleasedListener = new BufferReleasedListener(this);
92 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080093}
94
Zhijun He5d677d12016-05-29 16:52:39 -070095Camera3OutputStream::Camera3OutputStream(int id,
96 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +010097 uint64_t consumerUsage, android_dataspace dataSpace,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080098 camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
99 const String8& physicalCameraId, int setId) :
Zhijun He5d677d12016-05-29 16:52:39 -0700100 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800101 /*maxSize*/0, format, dataSpace, rotation,
102 physicalCameraId, setId),
Zhijun He5d677d12016-05-29 16:52:39 -0700103 mConsumer(nullptr),
104 mTransform(0),
105 mTraceFirstBuffer(true),
106 mUseBufferManager(false),
107 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700108 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700109 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700110 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He5d677d12016-05-29 16:52:39 -0700111 // Deferred consumer only support preview surface format now.
112 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
113 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
114 __FUNCTION__);
115 mState = STATE_ERROR;
116 }
117
118 // Sanity check for the consumer usage flag.
119 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
120 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100121 ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!",
122 __FUNCTION__, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700123 mState = STATE_ERROR;
124 }
125
126 mConsumerName = String8("Deferred");
127 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
128 mBufferReleasedListener = new BufferReleasedListener(this);
129 }
130
131}
132
Igor Murashkine3a9f962013-05-08 18:03:15 -0700133Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
134 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800135 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700136 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800137 camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800138 const String8& physicalCameraId,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100139 uint64_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800140 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700141 Camera3IOStreamBase(id, type, width, height,
142 /*maxSize*/0,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800143 format, dataSpace, rotation,
144 physicalCameraId, setId),
Zhijun He125684a2015-12-26 15:07:30 -0800145 mTransform(0),
146 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800147 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700148 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700149 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700150 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700151 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700152 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He125684a2015-12-26 15:07:30 -0800153
154 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
155 mBufferReleasedListener = new BufferReleasedListener(this);
156 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700157
158 // Subclasses expected to initialize mConsumer themselves
159}
160
161
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800162Camera3OutputStream::~Camera3OutputStream() {
163 disconnectLocked();
164}
165
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800166status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
167 const std::vector<size_t>&) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800168 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800169
170 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800171 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700172
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800173 status_t res;
174 res = getBufferLockedCommon(&anb, &fenceFd);
175 if (res != OK) {
176 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800177 }
178
Igor Murashkine3a9f962013-05-08 18:03:15 -0700179 /**
180 * FenceFD now owned by HAL except in case of error,
181 * in which case we reassign it to acquire_fence
182 */
183 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700184 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185
186 return OK;
187}
188
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800189status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700190 ANativeWindowBuffer* buffer, int anwReleaseFence,
191 const std::vector<size_t>&) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800192 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
193}
194
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800195status_t Camera3OutputStream::returnBufferLocked(
196 const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700197 nsecs_t timestamp, const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800198 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700199
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700200 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true, surface_ids);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700201
202 if (res != OK) {
203 return res;
204 }
205
206 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800207 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700208
209 return OK;
210}
211
212status_t Camera3OutputStream::returnBufferCheckedLocked(
213 const camera3_stream_buffer &buffer,
214 nsecs_t timestamp,
215 bool output,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700216 const std::vector<size_t>& surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700217 /*out*/
218 sp<Fence> *releaseFenceOut) {
219
220 (void)output;
221 ALOG_ASSERT(output, "Expected output to be true");
222
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800223 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700224
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800225 // Fence management - always honor release fence from HAL
226 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700227 int anwReleaseFence = releaseFence->dup();
228
229 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700230 * Release the lock briefly to avoid deadlock with
231 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
232 * thread will go into StreamingProcessor::onFrameAvailable) during
233 * queueBuffer
234 */
235 sp<ANativeWindow> currentConsumer = mConsumer;
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700236 StreamState state = mState;
Zhijun He124ccf42013-05-22 14:01:30 -0700237 mLock.unlock();
238
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800239 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700240 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700241 * Return buffer back to ANativeWindow
242 */
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700243 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR || mDropBuffers || timestamp == 0) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700244 // Cancel buffer
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700245 if (mDropBuffers) {
246 ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700247 } else if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700248 ALOGV("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700249 } else {
250 ALOGE("%s: Stream %d: timestamp shouldn't be 0", __FUNCTION__, mId);
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700251 }
252
Zhijun He124ccf42013-05-22 14:01:30 -0700253 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800254 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700255 anwReleaseFence);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700256 if (shouldLogError(res, state)) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700257 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700258 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700259 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800260
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800261 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800262 if (mUseBufferManager) {
263 // Return this buffer back to buffer manager.
264 mBufferReleasedListener->onBufferReleased();
265 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700266 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400267 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
268 {
269 char traceLog[48];
270 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
271 ATRACE_NAME(traceLog);
272 }
273 mTraceFirstBuffer = false;
274 }
275
Shuzhen Wang13a69632016-01-26 09:51:07 -0800276 /* Certain consumers (such as AudioSource or HardwareComposer) use
277 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800278 * in BOOTTIME. Do the conversion if necessary. */
279 res = native_window_set_buffers_timestamp(mConsumer.get(),
280 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
281 if (res != OK) {
282 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
283 __FUNCTION__, mId, strerror(-res), res);
284 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800285 }
286
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700287 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence, surface_ids);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700288 if (shouldLogError(res, state)) {
289 ALOGE("%s: Stream %d: Error queueing buffer to native window:"
290 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800291 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800292 }
Zhijun He124ccf42013-05-22 14:01:30 -0700293 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700294
295 // Once a valid buffer has been returned to the queue, can no longer
296 // dequeue all buffers for preallocation.
297 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
298 mStreamUnpreparable = true;
299 }
300
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700301 if (res != OK) {
302 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700303 }
304
Igor Murashkine3a9f962013-05-08 18:03:15 -0700305 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800306
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700307 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800308}
309
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800310void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
311 (void) args;
312 String8 lines;
313 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700314 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800315 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700316
317 Camera3IOStreamBase::dump(fd, args);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700318
319 mDequeueBufferLatency.dump(fd,
320 " DequeueBuffer latency histogram:");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800321}
322
323status_t Camera3OutputStream::setTransform(int transform) {
324 ATRACE_CALL();
325 Mutex::Autolock l(mLock);
326 return setTransformLocked(transform);
327}
328
329status_t Camera3OutputStream::setTransformLocked(int transform) {
330 status_t res = OK;
331 if (mState == STATE_ERROR) {
332 ALOGE("%s: Stream in error state", __FUNCTION__);
333 return INVALID_OPERATION;
334 }
335
336 mTransform = transform;
337 if (mState == STATE_CONFIGURED) {
338 res = native_window_set_buffers_transform(mConsumer.get(),
339 transform);
340 if (res != OK) {
341 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
342 __FUNCTION__, transform, strerror(-res), res);
343 }
344 }
345 return res;
346}
347
348status_t Camera3OutputStream::configureQueueLocked() {
349 status_t res;
350
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400351 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700352 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
353 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800354 }
355
Shuzhen Wang0129d522016-10-30 22:43:41 -0700356 if ((res = configureConsumerQueueLocked()) != OK) {
357 return res;
358 }
359
360 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
361 // We need skip these cases as timeout will disable the non-blocking (async) mode.
362 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800363 if (mUseBufferManager) {
364 // When buffer manager is handling the buffer, we should have available buffers in
365 // buffer queue before we calls into dequeueBuffer because buffer manager is tracking
366 // free buffers.
367 // There are however some consumer side feature (ImageReader::discardFreeBuffers) that
368 // can discard free buffers without notifying buffer manager. We want the timeout to
369 // happen immediately here so buffer manager can try to update its internal state and
370 // try to allocate a buffer instead of waiting.
371 mConsumer->setDequeueTimeout(0);
372 } else {
373 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
374 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700375 }
376
377 return OK;
378}
379
380status_t Camera3OutputStream::configureConsumerQueueLocked() {
381 status_t res;
382
383 mTraceFirstBuffer = true;
384
Igor Murashkine3a9f962013-05-08 18:03:15 -0700385 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
386
Zhijun He125684a2015-12-26 15:07:30 -0800387 // Configure consumer-side ANativeWindow interface. The listener may be used
388 // to notify buffer manager (if it is used) of the returned buffers.
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700389 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
390 /*listener*/mBufferReleasedListener,
391 /*reportBufferRemoval*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800392 if (res != OK) {
393 ALOGE("%s: Unable to connect to native window for stream %d",
394 __FUNCTION__, mId);
395 return res;
396 }
397
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700398 mConsumerName = mConsumer->getConsumerName();
399
Emilian Peev050f5dc2017-05-18 14:43:56 +0100400 res = native_window_set_usage(mConsumer.get(), mUsage);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 if (res != OK) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100402 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
403 __FUNCTION__, mUsage, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800404 return res;
405 }
406
407 res = native_window_set_scaling_mode(mConsumer.get(),
408 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
409 if (res != OK) {
410 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
411 __FUNCTION__, strerror(-res), res);
412 return res;
413 }
414
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800415 if (mMaxSize == 0) {
416 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700417 res = native_window_set_buffers_dimensions(mConsumer.get(),
418 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800419 } else {
420 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700421 res = native_window_set_buffers_dimensions(mConsumer.get(),
422 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800423 }
424 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700425 ALOGE("%s: Unable to configure stream buffer dimensions"
426 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800427 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700428 mMaxSize, mId);
429 return res;
430 }
431 res = native_window_set_buffers_format(mConsumer.get(),
432 camera3_stream::format);
433 if (res != OK) {
434 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
435 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800436 return res;
437 }
438
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800439 res = native_window_set_buffers_data_space(mConsumer.get(),
440 camera3_stream::data_space);
441 if (res != OK) {
442 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
443 __FUNCTION__, camera3_stream::data_space, mId);
444 return res;
445 }
446
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800447 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700448 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
449 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800450 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
451 if (res != OK) {
452 ALOGE("%s: Unable to query consumer undequeued"
453 " buffer count for stream %d", __FUNCTION__, mId);
454 return res;
455 }
456
Alex Ray20cb3002013-05-28 20:18:22 -0700457 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
458 maxConsumerBuffers, camera3_stream::max_buffers);
459 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700460 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700461 __FUNCTION__, camera3_stream::max_buffers);
462 return INVALID_OPERATION;
463 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800464
465 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700466 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800467 mFrameCount = 0;
468 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800469 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800470
471 res = native_window_set_buffer_count(mConsumer.get(),
472 mTotalBufferCount);
473 if (res != OK) {
474 ALOGE("%s: Unable to set buffer count for stream %d",
475 __FUNCTION__, mId);
476 return res;
477 }
478
479 res = native_window_set_buffers_transform(mConsumer.get(),
480 mTransform);
481 if (res != OK) {
482 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
483 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700484 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700485 }
486
Zhijun He125684a2015-12-26 15:07:30 -0800487 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800488 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800489 * buffers to be statically allocated for internal static buffer registration, while the
490 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800491 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
492 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
493 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800494 */
495 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100496 uint64_t consumerUsage = 0;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700497 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800498 StreamInfo streamInfo(
499 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Emilian Peev050f5dc2017-05-18 14:43:56 +0100500 mUsage | consumerUsage, mTotalBufferCount,
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700501 /*isConfigured*/true);
502 wp<Camera3OutputStream> weakThis(this);
503 res = mBufferManager->registerStream(weakThis,
504 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800505 if (res == OK) {
506 // Disable buffer allocation for this BufferQueue, buffer manager will take over
507 // the buffer allocation responsibility.
508 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
509 mUseBufferManager = true;
510 } else {
511 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
512 "(error %d %s), fall back to BufferQueue for buffer management!",
513 __FUNCTION__, mId, res, strerror(-res));
514 }
515 }
516
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800517 return OK;
518}
519
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800520status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
521 ATRACE_CALL();
522 status_t res;
523
524 if ((res = getBufferPreconditionCheckLocked()) != OK) {
525 return res;
526 }
527
528 bool gotBufferFromManager = false;
529
530 if (mUseBufferManager) {
531 sp<GraphicBuffer> gb;
532 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
533 if (res == OK) {
534 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
535 // successful return.
536 *anb = gb.get();
537 res = mConsumer->attachBuffer(*anb);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700538 if (shouldLogError(res, mState)) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800539 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
540 __FUNCTION__, mId, strerror(-res), res);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700541 }
542 if (res != OK) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800543 checkRetAndSetAbandonedLocked(res);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800544 return res;
545 }
546 gotBufferFromManager = true;
547 ALOGV("Stream %d: Attached new buffer", getId());
548 } else if (res == ALREADY_EXISTS) {
549 // Have sufficient free buffers already attached, can just
550 // dequeue from buffer queue
551 ALOGV("Stream %d: Reusing attached buffer", getId());
552 gotBufferFromManager = false;
553 } else if (res != OK) {
554 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
555 __FUNCTION__, mId, strerror(-res), res);
556 return res;
557 }
558 }
559 if (!gotBufferFromManager) {
560 /**
561 * Release the lock briefly to avoid deadlock for below scenario:
562 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
563 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
564 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
565 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
566 * StreamingProcessor lock.
567 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
568 * and try to lock bufferQueue lock.
569 * Then there is circular locking dependency.
570 */
571 sp<ANativeWindow> currentConsumer = mConsumer;
572 mLock.unlock();
573
Shuzhen Wang686f6442017-06-20 16:16:04 -0700574 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800575 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700576 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
577 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
578
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800579 mLock.lock();
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800580
581 if (mUseBufferManager && res == TIMED_OUT) {
582 checkRemovedBuffersLocked();
583
584 sp<GraphicBuffer> gb;
585 res = mBufferManager->getBufferForStream(
586 getId(), getStreamSetId(), &gb, fenceFd, /*noFreeBuffer*/true);
587
588 if (res == OK) {
589 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after
590 // a successful return.
591 *anb = gb.get();
592 res = mConsumer->attachBuffer(*anb);
593 gotBufferFromManager = true;
594 ALOGV("Stream %d: Attached new buffer", getId());
595
596 if (res != OK) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700597 if (shouldLogError(res, mState)) {
598 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface:"
599 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
600 }
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800601 checkRetAndSetAbandonedLocked(res);
602 return res;
603 }
604 } else {
605 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager:"
606 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
607 return res;
608 }
609 } else if (res != OK) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700610 if (shouldLogError(res, mState)) {
611 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
612 __FUNCTION__, mId, strerror(-res), res);
613 }
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800614 checkRetAndSetAbandonedLocked(res);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800615 return res;
616 }
617 }
618
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700619 if (res == OK) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800620 checkRemovedBuffersLocked();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700621 }
622
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800623 return res;
624}
625
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800626void Camera3OutputStream::checkRemovedBuffersLocked(bool notifyBufferManager) {
627 std::vector<sp<GraphicBuffer>> removedBuffers;
628 status_t res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
629 if (res == OK) {
630 onBuffersRemovedLocked(removedBuffers);
631
632 if (notifyBufferManager && mUseBufferManager && removedBuffers.size() > 0) {
633 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
634 }
635 }
636}
637
638void Camera3OutputStream::checkRetAndSetAbandonedLocked(status_t res) {
639 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is
640 // STATE_PREPARING, let prepareNextBuffer handle the error.)
641 if ((res == NO_INIT || res == DEAD_OBJECT) && mState == STATE_CONFIGURED) {
642 mState = STATE_ABANDONED;
643 }
644}
645
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700646bool Camera3OutputStream::shouldLogError(status_t res, StreamState state) {
647 if (res == OK) {
648 return false;
649 }
650 if ((res == DEAD_OBJECT || res == NO_INIT) && state == STATE_ABANDONED) {
651 return false;
652 }
653 return true;
654}
655
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800656status_t Camera3OutputStream::disconnectLocked() {
657 status_t res;
658
Igor Murashkine3a9f962013-05-08 18:03:15 -0700659 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
660 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800661 }
662
Zhijun He5d677d12016-05-29 16:52:39 -0700663 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
664 // state), don't need change the stream state, return OK.
665 if (mConsumer == nullptr) {
666 return OK;
667 }
668
Zhijun He125684a2015-12-26 15:07:30 -0800669 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
670
Igor Murashkine3a9f962013-05-08 18:03:15 -0700671 res = native_window_api_disconnect(mConsumer.get(),
672 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800673 /**
674 * This is not an error. if client calling process dies, the window will
675 * also die and all calls to it will return DEAD_OBJECT, thus it's already
676 * "disconnected"
677 */
678 if (res == DEAD_OBJECT) {
679 ALOGW("%s: While disconnecting stream %d from native window, the"
680 " native window died from under us", __FUNCTION__, mId);
681 }
682 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700683 ALOGE("%s: Unable to disconnect stream %d from native window "
684 "(error %d %s)",
685 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800686 mState = STATE_ERROR;
687 return res;
688 }
689
Zhijun He125684a2015-12-26 15:07:30 -0800690 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
691 // stream at this point should be safe.
692 if (mUseBufferManager) {
693 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
694 if (res != OK) {
695 ALOGE("%s: Unable to unregister stream %d from buffer manager "
696 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
697 mState = STATE_ERROR;
698 return res;
699 }
700 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
701 // the stream is still in usable state after this call.
702 mUseBufferManager = false;
703 }
704
Igor Murashkine3a9f962013-05-08 18:03:15 -0700705 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
706 : STATE_CONSTRUCTED;
Shuzhen Wang686f6442017-06-20 16:16:04 -0700707
708 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
709 mDequeueBufferLatency.reset();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800710 return OK;
711}
712
Emilian Peev050f5dc2017-05-18 14:43:56 +0100713status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700714
715 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700716
Zhijun He5d677d12016-05-29 16:52:39 -0700717 if (mConsumer == nullptr) {
718 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
719 *usage = mConsumerUsage;
720 return OK;
721 }
722
Shuzhen Wang0129d522016-10-30 22:43:41 -0700723 res = getEndpointUsageForSurface(usage, mConsumer);
724
725 return res;
726}
727
Emilian Peev35ae8262018-11-08 13:11:32 +0000728void Camera3OutputStream::applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/) {
729 if (consumerUsage == nullptr) {
730 return;
731 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700732
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700733 // If an opaque output stream's endpoint is ImageReader, add
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700734 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700735 // for the ZSL use case.
736 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
737 // 1. GRALLOC_USAGE_HW_TEXTURE
738 // 2. GRALLOC_USAGE_HW_RENDER
739 // 3. GRALLOC_USAGE_HW_COMPOSER
740 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
Emilian Peev35ae8262018-11-08 13:11:32 +0000741 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
742 (*consumerUsage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
Shuzhen Wang0129d522016-10-30 22:43:41 -0700743 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000744 *consumerUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700745 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000746}
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700747
Emilian Peev35ae8262018-11-08 13:11:32 +0000748status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
749 const sp<Surface>& surface) const {
750 status_t res;
751 uint64_t u = 0;
752
753 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
754 applyZSLUsageQuirk(camera3_stream::format, &u);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700755 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700756 return res;
757}
758
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700759bool Camera3OutputStream::isVideoStream() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100760 uint64_t usage = 0;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700761 status_t res = getEndpointUsage(&usage);
762 if (res != OK) {
763 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
764 return false;
765 }
766
767 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
768}
769
Zhijun He125684a2015-12-26 15:07:30 -0800770status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
771 Mutex::Autolock l(mLock);
772 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700773 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800774 __FUNCTION__);
775 return INVALID_OPERATION;
776 }
777 mBufferManager = bufferManager;
778
779 return OK;
780}
781
Emilian Peev40ead602017-09-26 15:46:36 +0100782status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
783 const std::vector<OutputStreamInfo> &/*outputInfo*/,
784 const std::vector<size_t> &/*removedSurfaceIds*/,
785 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
786 ALOGE("%s: this method is not supported!", __FUNCTION__);
787 return INVALID_OPERATION;
788}
789
Zhijun He125684a2015-12-26 15:07:30 -0800790void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
791 sp<Camera3OutputStream> stream = mParent.promote();
792 if (stream == nullptr) {
793 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
794 return;
795 }
796
797 Mutex::Autolock l(stream->mLock);
798 if (!(stream->mUseBufferManager)) {
799 return;
800 }
801
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700802 ALOGV("Stream %d: Buffer released", stream->getId());
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700803 bool shouldFreeBuffer = false;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700804 status_t res = stream->mBufferManager->onBufferReleased(
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700805 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700806 if (res != OK) {
807 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
808 strerror(-res), res);
809 stream->mState = STATE_ERROR;
810 }
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700811
812 if (shouldFreeBuffer) {
813 sp<GraphicBuffer> buffer;
814 // Detach and free a buffer (when buffer goes out of scope)
815 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
816 if (buffer.get() != nullptr) {
817 stream->mBufferManager->notifyBufferRemoved(
818 stream->getId(), stream->getStreamSetId());
819 }
820 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700821}
822
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700823void Camera3OutputStream::onBuffersRemovedLocked(
824 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700825 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700826 if (callback != nullptr) {
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700827 for (const auto& gb : removedBuffers) {
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700828 callback->onBufferFreed(mId, gb->handle);
829 }
830 }
831}
832
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700833status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
834 Mutex::Autolock l(mLock);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700835 return detachBufferLocked(buffer, fenceFd);
836}
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700837
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700838status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700839 ALOGV("Stream %d: detachBuffer", getId());
840 if (buffer == nullptr) {
841 return BAD_VALUE;
842 }
843
Zhijun He125684a2015-12-26 15:07:30 -0800844 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700845 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800846 if (res == NO_MEMORY) {
847 // This may rarely happen, which indicates that the released buffer was freed by other
848 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
849 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
850 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700851 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800852 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
853 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700854 // Treat other errors as abandonment
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700855 if (shouldLogError(res, mState)) {
856 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
857 }
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700858 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700859 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800860 }
861
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700862 if (fenceFd != nullptr) {
863 if (fence!= 0 && fence->isValid()) {
864 *fenceFd = fence->dup();
865 } else {
866 *fenceFd = -1;
867 }
Zhijun He125684a2015-12-26 15:07:30 -0800868 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700869
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800870 // Here we assume detachBuffer is called by buffer manager so it doesn't need to be notified
871 checkRemovedBuffersLocked(/*notifyBufferManager*/false);
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700872 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800873}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800874
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700875status_t Camera3OutputStream::dropBuffers(bool dropping) {
876 Mutex::Autolock l(mLock);
877 mDropBuffers = dropping;
878 return OK;
879}
880
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800881const String8& Camera3OutputStream::getPhysicalCameraId() const {
882 Mutex::Autolock l(mLock);
883 return physicalCameraId();
884}
885
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800886status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700887 return OK;
888}
889
890bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700891 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892
893 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800894 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895 }
Zhijun He5d677d12016-05-29 16:52:39 -0700896 return mConsumer == nullptr;
897}
898
Shuzhen Wang758c2152017-01-10 18:26:18 -0800899status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800900 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800901 if (consumers.size() != 1) {
902 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
903 __FUNCTION__, consumers.size());
904 return INVALID_OPERATION;
905 }
906 if (consumers[0] == nullptr) {
907 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700908 return INVALID_OPERATION;
909 }
910
911 if (mConsumer != nullptr) {
912 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
913 return INVALID_OPERATION;
914 }
915
Shuzhen Wang758c2152017-01-10 18:26:18 -0800916 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700917 return OK;
918}
919
Shuzhen Wang13a69632016-01-26 09:51:07 -0800920bool Camera3OutputStream::isConsumedByHWComposer() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100921 uint64_t usage = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800922 status_t res = getEndpointUsage(&usage);
923 if (res != OK) {
924 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
925 return false;
926 }
927
928 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
929}
930
Zhijun Hef0645c12016-08-02 00:58:11 -0700931bool Camera3OutputStream::isConsumedByHWTexture() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100932 uint64_t usage = 0;
Zhijun Hef0645c12016-08-02 00:58:11 -0700933 status_t res = getEndpointUsage(&usage);
934 if (res != OK) {
935 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
936 return false;
937 }
938
939 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
940}
941
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800942}; // namespace camera3
943
944}; // namespace android