blob: ecbcf76c68b1f0447955a56179491ee409092e5a [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,
190 ANativeWindowBuffer* buffer, int anwReleaseFence) {
191 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
192}
193
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800194status_t Camera3OutputStream::returnBufferLocked(
195 const camera3_stream_buffer &buffer,
196 nsecs_t timestamp) {
197 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700198
199 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
200
201 if (res != OK) {
202 return res;
203 }
204
205 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800206 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700207
208 return OK;
209}
210
211status_t Camera3OutputStream::returnBufferCheckedLocked(
212 const camera3_stream_buffer &buffer,
213 nsecs_t timestamp,
214 bool output,
215 /*out*/
216 sp<Fence> *releaseFenceOut) {
217
218 (void)output;
219 ALOG_ASSERT(output, "Expected output to be true");
220
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800221 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700222
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800223 // Fence management - always honor release fence from HAL
224 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700225 int anwReleaseFence = releaseFence->dup();
226
227 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700228 * Release the lock briefly to avoid deadlock with
229 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
230 * thread will go into StreamingProcessor::onFrameAvailable) during
231 * queueBuffer
232 */
233 sp<ANativeWindow> currentConsumer = mConsumer;
234 mLock.unlock();
235
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800236 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700237 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700238 * Return buffer back to ANativeWindow
239 */
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700240 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR || mDropBuffers || timestamp == 0) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700241 // Cancel buffer
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700242 if (mDropBuffers) {
243 ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700244 } else if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700245 ALOGW("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700246 } else {
247 ALOGE("%s: Stream %d: timestamp shouldn't be 0", __FUNCTION__, mId);
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700248 }
249
Zhijun He124ccf42013-05-22 14:01:30 -0700250 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800251 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700252 anwReleaseFence);
253 if (res != OK) {
254 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700255 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700256 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800257
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800258 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800259 if (mUseBufferManager) {
260 // Return this buffer back to buffer manager.
261 mBufferReleasedListener->onBufferReleased();
262 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700263 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400264 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
265 {
266 char traceLog[48];
267 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
268 ATRACE_NAME(traceLog);
269 }
270 mTraceFirstBuffer = false;
271 }
272
Shuzhen Wang13a69632016-01-26 09:51:07 -0800273 /* Certain consumers (such as AudioSource or HardwareComposer) use
274 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800275 * in BOOTTIME. Do the conversion if necessary. */
276 res = native_window_set_buffers_timestamp(mConsumer.get(),
277 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
278 if (res != OK) {
279 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
280 __FUNCTION__, mId, strerror(-res), res);
281 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800282 }
283
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800284 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800285 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700286 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
287 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800288 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800289 }
Zhijun He124ccf42013-05-22 14:01:30 -0700290 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700291
292 // Once a valid buffer has been returned to the queue, can no longer
293 // dequeue all buffers for preallocation.
294 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
295 mStreamUnpreparable = true;
296 }
297
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700298 if (res != OK) {
299 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700300 }
301
Igor Murashkine3a9f962013-05-08 18:03:15 -0700302 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800303
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800305}
306
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800307void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
308 (void) args;
309 String8 lines;
310 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700311 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800312 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700313
314 Camera3IOStreamBase::dump(fd, args);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700315
316 mDequeueBufferLatency.dump(fd,
317 " DequeueBuffer latency histogram:");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800318}
319
320status_t Camera3OutputStream::setTransform(int transform) {
321 ATRACE_CALL();
322 Mutex::Autolock l(mLock);
323 return setTransformLocked(transform);
324}
325
326status_t Camera3OutputStream::setTransformLocked(int transform) {
327 status_t res = OK;
328 if (mState == STATE_ERROR) {
329 ALOGE("%s: Stream in error state", __FUNCTION__);
330 return INVALID_OPERATION;
331 }
332
333 mTransform = transform;
334 if (mState == STATE_CONFIGURED) {
335 res = native_window_set_buffers_transform(mConsumer.get(),
336 transform);
337 if (res != OK) {
338 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
339 __FUNCTION__, transform, strerror(-res), res);
340 }
341 }
342 return res;
343}
344
345status_t Camera3OutputStream::configureQueueLocked() {
346 status_t res;
347
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400348 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700349 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
350 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800351 }
352
Shuzhen Wang0129d522016-10-30 22:43:41 -0700353 if ((res = configureConsumerQueueLocked()) != OK) {
354 return res;
355 }
356
357 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
358 // We need skip these cases as timeout will disable the non-blocking (async) mode.
359 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
360 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
361 }
362
363 return OK;
364}
365
366status_t Camera3OutputStream::configureConsumerQueueLocked() {
367 status_t res;
368
369 mTraceFirstBuffer = true;
370
Igor Murashkine3a9f962013-05-08 18:03:15 -0700371 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
372
Zhijun He125684a2015-12-26 15:07:30 -0800373 // Configure consumer-side ANativeWindow interface. The listener may be used
374 // to notify buffer manager (if it is used) of the returned buffers.
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700375 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
376 /*listener*/mBufferReleasedListener,
377 /*reportBufferRemoval*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800378 if (res != OK) {
379 ALOGE("%s: Unable to connect to native window for stream %d",
380 __FUNCTION__, mId);
381 return res;
382 }
383
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700384 mConsumerName = mConsumer->getConsumerName();
385
Emilian Peev050f5dc2017-05-18 14:43:56 +0100386 res = native_window_set_usage(mConsumer.get(), mUsage);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800387 if (res != OK) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100388 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
389 __FUNCTION__, mUsage, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800390 return res;
391 }
392
393 res = native_window_set_scaling_mode(mConsumer.get(),
394 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
395 if (res != OK) {
396 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
397 __FUNCTION__, strerror(-res), res);
398 return res;
399 }
400
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 if (mMaxSize == 0) {
402 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700403 res = native_window_set_buffers_dimensions(mConsumer.get(),
404 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800405 } else {
406 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700407 res = native_window_set_buffers_dimensions(mConsumer.get(),
408 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800409 }
410 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700411 ALOGE("%s: Unable to configure stream buffer dimensions"
412 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800413 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700414 mMaxSize, mId);
415 return res;
416 }
417 res = native_window_set_buffers_format(mConsumer.get(),
418 camera3_stream::format);
419 if (res != OK) {
420 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
421 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800422 return res;
423 }
424
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800425 res = native_window_set_buffers_data_space(mConsumer.get(),
426 camera3_stream::data_space);
427 if (res != OK) {
428 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
429 __FUNCTION__, camera3_stream::data_space, mId);
430 return res;
431 }
432
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800433 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700434 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
435 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800436 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
437 if (res != OK) {
438 ALOGE("%s: Unable to query consumer undequeued"
439 " buffer count for stream %d", __FUNCTION__, mId);
440 return res;
441 }
442
Alex Ray20cb3002013-05-28 20:18:22 -0700443 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
444 maxConsumerBuffers, camera3_stream::max_buffers);
445 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700446 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700447 __FUNCTION__, camera3_stream::max_buffers);
448 return INVALID_OPERATION;
449 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800450
451 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700452 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800453 mFrameCount = 0;
454 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800455 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800456
457 res = native_window_set_buffer_count(mConsumer.get(),
458 mTotalBufferCount);
459 if (res != OK) {
460 ALOGE("%s: Unable to set buffer count for stream %d",
461 __FUNCTION__, mId);
462 return res;
463 }
464
465 res = native_window_set_buffers_transform(mConsumer.get(),
466 mTransform);
467 if (res != OK) {
468 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
469 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700470 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700471 }
472
Zhijun He125684a2015-12-26 15:07:30 -0800473 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800474 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800475 * buffers to be statically allocated for internal static buffer registration, while the
476 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800477 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
478 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
479 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800480 */
481 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100482 uint64_t consumerUsage = 0;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700483 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800484 StreamInfo streamInfo(
485 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Emilian Peev050f5dc2017-05-18 14:43:56 +0100486 mUsage | consumerUsage, mTotalBufferCount,
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700487 /*isConfigured*/true);
488 wp<Camera3OutputStream> weakThis(this);
489 res = mBufferManager->registerStream(weakThis,
490 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800491 if (res == OK) {
492 // Disable buffer allocation for this BufferQueue, buffer manager will take over
493 // the buffer allocation responsibility.
494 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
495 mUseBufferManager = true;
496 } else {
497 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
498 "(error %d %s), fall back to BufferQueue for buffer management!",
499 __FUNCTION__, mId, res, strerror(-res));
500 }
501 }
502
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800503 return OK;
504}
505
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800506status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
507 ATRACE_CALL();
508 status_t res;
509
510 if ((res = getBufferPreconditionCheckLocked()) != OK) {
511 return res;
512 }
513
514 bool gotBufferFromManager = false;
515
516 if (mUseBufferManager) {
517 sp<GraphicBuffer> gb;
518 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
519 if (res == OK) {
520 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
521 // successful return.
522 *anb = gb.get();
523 res = mConsumer->attachBuffer(*anb);
524 if (res != OK) {
525 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
526 __FUNCTION__, mId, strerror(-res), res);
527 return res;
528 }
529 gotBufferFromManager = true;
530 ALOGV("Stream %d: Attached new buffer", getId());
531 } else if (res == ALREADY_EXISTS) {
532 // Have sufficient free buffers already attached, can just
533 // dequeue from buffer queue
534 ALOGV("Stream %d: Reusing attached buffer", getId());
535 gotBufferFromManager = false;
536 } else if (res != OK) {
537 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
538 __FUNCTION__, mId, strerror(-res), res);
539 return res;
540 }
541 }
542 if (!gotBufferFromManager) {
543 /**
544 * Release the lock briefly to avoid deadlock for below scenario:
545 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
546 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
547 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
548 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
549 * StreamingProcessor lock.
550 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
551 * and try to lock bufferQueue lock.
552 * Then there is circular locking dependency.
553 */
554 sp<ANativeWindow> currentConsumer = mConsumer;
555 mLock.unlock();
556
Shuzhen Wang686f6442017-06-20 16:16:04 -0700557 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800558 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700559 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
560 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
561
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800562 mLock.lock();
563 if (res != OK) {
564 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
565 __FUNCTION__, mId, strerror(-res), res);
566
567 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
568 // let prepareNextBuffer handle the error.)
Shuzhen Wangb14fef42018-07-16 10:22:21 -0700569 if ((res == NO_INIT || res == DEAD_OBJECT) && mState == STATE_CONFIGURED) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800570 mState = STATE_ABANDONED;
571 }
572
573 return res;
574 }
575 }
576
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700577 if (res == OK) {
578 std::vector<sp<GraphicBuffer>> removedBuffers;
579 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
580 if (res == OK) {
581 onBuffersRemovedLocked(removedBuffers);
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700582
583 if (mUseBufferManager && removedBuffers.size() > 0) {
584 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
585 }
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700586 }
587 }
588
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800589 return res;
590}
591
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800592status_t Camera3OutputStream::disconnectLocked() {
593 status_t res;
594
Igor Murashkine3a9f962013-05-08 18:03:15 -0700595 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
596 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800597 }
598
Zhijun He5d677d12016-05-29 16:52:39 -0700599 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
600 // state), don't need change the stream state, return OK.
601 if (mConsumer == nullptr) {
602 return OK;
603 }
604
Zhijun He125684a2015-12-26 15:07:30 -0800605 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
606
Igor Murashkine3a9f962013-05-08 18:03:15 -0700607 res = native_window_api_disconnect(mConsumer.get(),
608 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800609 /**
610 * This is not an error. if client calling process dies, the window will
611 * also die and all calls to it will return DEAD_OBJECT, thus it's already
612 * "disconnected"
613 */
614 if (res == DEAD_OBJECT) {
615 ALOGW("%s: While disconnecting stream %d from native window, the"
616 " native window died from under us", __FUNCTION__, mId);
617 }
618 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700619 ALOGE("%s: Unable to disconnect stream %d from native window "
620 "(error %d %s)",
621 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800622 mState = STATE_ERROR;
623 return res;
624 }
625
Zhijun He125684a2015-12-26 15:07:30 -0800626 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
627 // stream at this point should be safe.
628 if (mUseBufferManager) {
629 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
630 if (res != OK) {
631 ALOGE("%s: Unable to unregister stream %d from buffer manager "
632 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
633 mState = STATE_ERROR;
634 return res;
635 }
636 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
637 // the stream is still in usable state after this call.
638 mUseBufferManager = false;
639 }
640
Igor Murashkine3a9f962013-05-08 18:03:15 -0700641 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
642 : STATE_CONSTRUCTED;
Shuzhen Wang686f6442017-06-20 16:16:04 -0700643
644 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
645 mDequeueBufferLatency.reset();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800646 return OK;
647}
648
Emilian Peev050f5dc2017-05-18 14:43:56 +0100649status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700650
651 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700652
Zhijun He5d677d12016-05-29 16:52:39 -0700653 if (mConsumer == nullptr) {
654 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
655 *usage = mConsumerUsage;
656 return OK;
657 }
658
Shuzhen Wang0129d522016-10-30 22:43:41 -0700659 res = getEndpointUsageForSurface(usage, mConsumer);
660
661 return res;
662}
663
Emilian Peev050f5dc2017-05-18 14:43:56 +0100664status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700665 const sp<Surface>& surface) const {
666 status_t res;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100667 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700668
Emilian Peev050f5dc2017-05-18 14:43:56 +0100669 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700670 // If an opaque output stream's endpoint is ImageReader, add
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700671 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700672 // for the ZSL use case.
673 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
674 // 1. GRALLOC_USAGE_HW_TEXTURE
675 // 2. GRALLOC_USAGE_HW_RENDER
676 // 3. GRALLOC_USAGE_HW_COMPOSER
677 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
678 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
Shuzhen Wang0129d522016-10-30 22:43:41 -0700679 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
680 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700681 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700682 }
683
684 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700685 return res;
686}
687
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700688bool Camera3OutputStream::isVideoStream() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100689 uint64_t usage = 0;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700690 status_t res = getEndpointUsage(&usage);
691 if (res != OK) {
692 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
693 return false;
694 }
695
696 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
697}
698
Zhijun He125684a2015-12-26 15:07:30 -0800699status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
700 Mutex::Autolock l(mLock);
701 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700702 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800703 __FUNCTION__);
704 return INVALID_OPERATION;
705 }
706 mBufferManager = bufferManager;
707
708 return OK;
709}
710
Emilian Peev40ead602017-09-26 15:46:36 +0100711status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
712 const std::vector<OutputStreamInfo> &/*outputInfo*/,
713 const std::vector<size_t> &/*removedSurfaceIds*/,
714 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
715 ALOGE("%s: this method is not supported!", __FUNCTION__);
716 return INVALID_OPERATION;
717}
718
Zhijun He125684a2015-12-26 15:07:30 -0800719void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
720 sp<Camera3OutputStream> stream = mParent.promote();
721 if (stream == nullptr) {
722 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
723 return;
724 }
725
726 Mutex::Autolock l(stream->mLock);
727 if (!(stream->mUseBufferManager)) {
728 return;
729 }
730
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700731 ALOGV("Stream %d: Buffer released", stream->getId());
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700732 bool shouldFreeBuffer = false;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700733 status_t res = stream->mBufferManager->onBufferReleased(
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700734 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700735 if (res != OK) {
736 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
737 strerror(-res), res);
738 stream->mState = STATE_ERROR;
739 }
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700740
741 if (shouldFreeBuffer) {
742 sp<GraphicBuffer> buffer;
743 // Detach and free a buffer (when buffer goes out of scope)
744 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
745 if (buffer.get() != nullptr) {
746 stream->mBufferManager->notifyBufferRemoved(
747 stream->getId(), stream->getStreamSetId());
748 }
749 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700750}
751
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700752void Camera3OutputStream::onBuffersRemovedLocked(
753 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700754 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700755 if (callback != nullptr) {
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700756 for (const auto& gb : removedBuffers) {
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700757 callback->onBufferFreed(mId, gb->handle);
758 }
759 }
760}
761
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700762status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
763 Mutex::Autolock l(mLock);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700764 return detachBufferLocked(buffer, fenceFd);
765}
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700766
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700767status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700768 ALOGV("Stream %d: detachBuffer", getId());
769 if (buffer == nullptr) {
770 return BAD_VALUE;
771 }
772
Zhijun He125684a2015-12-26 15:07:30 -0800773 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700774 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800775 if (res == NO_MEMORY) {
776 // This may rarely happen, which indicates that the released buffer was freed by other
777 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
778 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
779 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700780 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800781 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
782 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700783 // Treat other errors as abandonment
Zhijun He125684a2015-12-26 15:07:30 -0800784 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700785 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700786 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800787 }
788
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700789 if (fenceFd != nullptr) {
790 if (fence!= 0 && fence->isValid()) {
791 *fenceFd = fence->dup();
792 } else {
793 *fenceFd = -1;
794 }
Zhijun He125684a2015-12-26 15:07:30 -0800795 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700796
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700797 std::vector<sp<GraphicBuffer>> removedBuffers;
798 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
799 if (res == OK) {
800 onBuffersRemovedLocked(removedBuffers);
801 }
802 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800803}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800804
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700805status_t Camera3OutputStream::dropBuffers(bool dropping) {
806 Mutex::Autolock l(mLock);
807 mDropBuffers = dropping;
808 return OK;
809}
810
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800811const String8& Camera3OutputStream::getPhysicalCameraId() const {
812 Mutex::Autolock l(mLock);
813 return physicalCameraId();
814}
815
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800816status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 return OK;
818}
819
820bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700821 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700822
823 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800824 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700825 }
Zhijun He5d677d12016-05-29 16:52:39 -0700826 return mConsumer == nullptr;
827}
828
Shuzhen Wang758c2152017-01-10 18:26:18 -0800829status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800830 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800831 if (consumers.size() != 1) {
832 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
833 __FUNCTION__, consumers.size());
834 return INVALID_OPERATION;
835 }
836 if (consumers[0] == nullptr) {
837 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700838 return INVALID_OPERATION;
839 }
840
841 if (mConsumer != nullptr) {
842 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
843 return INVALID_OPERATION;
844 }
845
Shuzhen Wang758c2152017-01-10 18:26:18 -0800846 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700847 return OK;
848}
849
Shuzhen Wang13a69632016-01-26 09:51:07 -0800850bool Camera3OutputStream::isConsumedByHWComposer() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100851 uint64_t usage = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800852 status_t res = getEndpointUsage(&usage);
853 if (res != OK) {
854 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
855 return false;
856 }
857
858 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
859}
860
Zhijun Hef0645c12016-08-02 00:58:11 -0700861bool Camera3OutputStream::isConsumedByHWTexture() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100862 uint64_t usage = 0;
Zhijun Hef0645c12016-08-02 00:58:11 -0700863 status_t res = getEndpointUsage(&usage);
864 if (res != OK) {
865 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
866 return false;
867 }
868
869 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
870}
871
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800872}; // namespace camera3
873
874}; // namespace android