blob: e79eecc72b0dbe17fe4033700ac033e2b4404598 [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),
Shuzhen Wang686f6442017-06-20 16:16:04 -070046 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070047 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070048 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080049
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080050 if (mConsumer == NULL) {
51 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
52 mState = STATE_ERROR;
53 }
Zhijun He125684a2015-12-26 15:07:30 -080054
55 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
56 mBufferReleasedListener = new BufferReleasedListener(this);
57 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080058}
59
60Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070061 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080062 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080063 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
64 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070065 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080066 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070067 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040068 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080069 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080070 mUseMonoTimestamp(false),
71 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070072 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070073 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070074 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070075 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080076
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080077 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080078 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
79 format);
80 mState = STATE_ERROR;
81 }
82
83 if (mConsumer == NULL) {
84 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
85 mState = STATE_ERROR;
86 }
Zhijun He125684a2015-12-26 15:07:30 -080087
88 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
89 mBufferReleasedListener = new BufferReleasedListener(this);
90 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080091}
92
Zhijun He5d677d12016-05-29 16:52:39 -070093Camera3OutputStream::Camera3OutputStream(int id,
94 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +010095 uint64_t consumerUsage, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -070096 camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) :
97 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
98 /*maxSize*/0, format, dataSpace, rotation, setId),
99 mConsumer(nullptr),
100 mTransform(0),
101 mTraceFirstBuffer(true),
102 mUseBufferManager(false),
103 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700104 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700105 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700106 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He5d677d12016-05-29 16:52:39 -0700107 // Deferred consumer only support preview surface format now.
108 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
109 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
110 __FUNCTION__);
111 mState = STATE_ERROR;
112 }
113
114 // Sanity check for the consumer usage flag.
115 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
116 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100117 ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!",
118 __FUNCTION__, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700119 mState = STATE_ERROR;
120 }
121
122 mConsumerName = String8("Deferred");
123 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
124 mBufferReleasedListener = new BufferReleasedListener(this);
125 }
126
127}
128
Igor Murashkine3a9f962013-05-08 18:03:15 -0700129Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
130 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800131 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700132 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800133 camera3_stream_rotation_t rotation,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100134 uint64_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800135 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700136 Camera3IOStreamBase(id, type, width, height,
137 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -0800138 format, dataSpace, rotation, setId),
139 mTransform(0),
140 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800141 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700142 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700143 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700144 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700145 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700146 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He125684a2015-12-26 15:07:30 -0800147
148 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
149 mBufferReleasedListener = new BufferReleasedListener(this);
150 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700151
152 // Subclasses expected to initialize mConsumer themselves
153}
154
155
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800156Camera3OutputStream::~Camera3OutputStream() {
157 disconnectLocked();
158}
159
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800160status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
161 const std::vector<size_t>&) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800162 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800163
164 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800165 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700166
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800167 status_t res;
168 res = getBufferLockedCommon(&anb, &fenceFd);
169 if (res != OK) {
170 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800171 }
172
Igor Murashkine3a9f962013-05-08 18:03:15 -0700173 /**
174 * FenceFD now owned by HAL except in case of error,
175 * in which case we reassign it to acquire_fence
176 */
177 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700178 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800179
180 return OK;
181}
182
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800183status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
184 ANativeWindowBuffer* buffer, int anwReleaseFence) {
185 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
186}
187
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800188status_t Camera3OutputStream::returnBufferLocked(
189 const camera3_stream_buffer &buffer,
190 nsecs_t timestamp) {
191 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700192
193 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
194
195 if (res != OK) {
196 return res;
197 }
198
199 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800200 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700201
202 return OK;
203}
204
205status_t Camera3OutputStream::returnBufferCheckedLocked(
206 const camera3_stream_buffer &buffer,
207 nsecs_t timestamp,
208 bool output,
209 /*out*/
210 sp<Fence> *releaseFenceOut) {
211
212 (void)output;
213 ALOG_ASSERT(output, "Expected output to be true");
214
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800215 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700216
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800217 // Fence management - always honor release fence from HAL
218 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700219 int anwReleaseFence = releaseFence->dup();
220
221 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700222 * Release the lock briefly to avoid deadlock with
223 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
224 * thread will go into StreamingProcessor::onFrameAvailable) during
225 * queueBuffer
226 */
227 sp<ANativeWindow> currentConsumer = mConsumer;
228 mLock.unlock();
229
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800230 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700231 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700232 * Return buffer back to ANativeWindow
233 */
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700234 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR || mDropBuffers) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700235 // Cancel buffer
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700236 if (mDropBuffers) {
237 ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId);
238 } else {
239 ALOGW("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId);
240 }
241
Zhijun He124ccf42013-05-22 14:01:30 -0700242 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800243 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700244 anwReleaseFence);
245 if (res != OK) {
246 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700247 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700248 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800249
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800250 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800251 if (mUseBufferManager) {
252 // Return this buffer back to buffer manager.
253 mBufferReleasedListener->onBufferReleased();
254 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700255 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400256 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
257 {
258 char traceLog[48];
259 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
260 ATRACE_NAME(traceLog);
261 }
262 mTraceFirstBuffer = false;
263 }
264
Shuzhen Wang13a69632016-01-26 09:51:07 -0800265 /* Certain consumers (such as AudioSource or HardwareComposer) use
266 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800267 * in BOOTTIME. Do the conversion if necessary. */
268 res = native_window_set_buffers_timestamp(mConsumer.get(),
269 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
270 if (res != OK) {
271 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
272 __FUNCTION__, mId, strerror(-res), res);
273 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800274 }
275
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800276 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800277 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700278 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
279 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800280 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800281 }
Zhijun He124ccf42013-05-22 14:01:30 -0700282 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700283
284 // Once a valid buffer has been returned to the queue, can no longer
285 // dequeue all buffers for preallocation.
286 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
287 mStreamUnpreparable = true;
288 }
289
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700290 if (res != OK) {
291 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700292 }
293
Igor Murashkine3a9f962013-05-08 18:03:15 -0700294 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800295
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700296 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800297}
298
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800299void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
300 (void) args;
301 String8 lines;
302 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700303 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800304 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700305
306 Camera3IOStreamBase::dump(fd, args);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700307
308 mDequeueBufferLatency.dump(fd,
309 " DequeueBuffer latency histogram:");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800310}
311
312status_t Camera3OutputStream::setTransform(int transform) {
313 ATRACE_CALL();
314 Mutex::Autolock l(mLock);
315 return setTransformLocked(transform);
316}
317
318status_t Camera3OutputStream::setTransformLocked(int transform) {
319 status_t res = OK;
320 if (mState == STATE_ERROR) {
321 ALOGE("%s: Stream in error state", __FUNCTION__);
322 return INVALID_OPERATION;
323 }
324
325 mTransform = transform;
326 if (mState == STATE_CONFIGURED) {
327 res = native_window_set_buffers_transform(mConsumer.get(),
328 transform);
329 if (res != OK) {
330 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
331 __FUNCTION__, transform, strerror(-res), res);
332 }
333 }
334 return res;
335}
336
337status_t Camera3OutputStream::configureQueueLocked() {
338 status_t res;
339
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400340 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700341 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
342 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800343 }
344
Shuzhen Wang0129d522016-10-30 22:43:41 -0700345 if ((res = configureConsumerQueueLocked()) != OK) {
346 return res;
347 }
348
349 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
350 // We need skip these cases as timeout will disable the non-blocking (async) mode.
351 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
352 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
353 }
354
355 return OK;
356}
357
358status_t Camera3OutputStream::configureConsumerQueueLocked() {
359 status_t res;
360
361 mTraceFirstBuffer = true;
362
Igor Murashkine3a9f962013-05-08 18:03:15 -0700363 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
364
Zhijun He125684a2015-12-26 15:07:30 -0800365 // Configure consumer-side ANativeWindow interface. The listener may be used
366 // to notify buffer manager (if it is used) of the returned buffers.
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700367 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
368 /*listener*/mBufferReleasedListener,
369 /*reportBufferRemoval*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800370 if (res != OK) {
371 ALOGE("%s: Unable to connect to native window for stream %d",
372 __FUNCTION__, mId);
373 return res;
374 }
375
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700376 mConsumerName = mConsumer->getConsumerName();
377
Emilian Peev050f5dc2017-05-18 14:43:56 +0100378 res = native_window_set_usage(mConsumer.get(), mUsage);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800379 if (res != OK) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100380 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
381 __FUNCTION__, mUsage, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800382 return res;
383 }
384
385 res = native_window_set_scaling_mode(mConsumer.get(),
386 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
387 if (res != OK) {
388 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
389 __FUNCTION__, strerror(-res), res);
390 return res;
391 }
392
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800393 if (mMaxSize == 0) {
394 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700395 res = native_window_set_buffers_dimensions(mConsumer.get(),
396 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800397 } else {
398 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700399 res = native_window_set_buffers_dimensions(mConsumer.get(),
400 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 }
402 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700403 ALOGE("%s: Unable to configure stream buffer dimensions"
404 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800405 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700406 mMaxSize, mId);
407 return res;
408 }
409 res = native_window_set_buffers_format(mConsumer.get(),
410 camera3_stream::format);
411 if (res != OK) {
412 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
413 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800414 return res;
415 }
416
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800417 res = native_window_set_buffers_data_space(mConsumer.get(),
418 camera3_stream::data_space);
419 if (res != OK) {
420 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
421 __FUNCTION__, camera3_stream::data_space, mId);
422 return res;
423 }
424
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800425 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700426 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
427 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800428 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
429 if (res != OK) {
430 ALOGE("%s: Unable to query consumer undequeued"
431 " buffer count for stream %d", __FUNCTION__, mId);
432 return res;
433 }
434
Alex Ray20cb3002013-05-28 20:18:22 -0700435 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
436 maxConsumerBuffers, camera3_stream::max_buffers);
437 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700438 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700439 __FUNCTION__, camera3_stream::max_buffers);
440 return INVALID_OPERATION;
441 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800442
443 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700444 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800445 mFrameCount = 0;
446 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800447 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800448
449 res = native_window_set_buffer_count(mConsumer.get(),
450 mTotalBufferCount);
451 if (res != OK) {
452 ALOGE("%s: Unable to set buffer count for stream %d",
453 __FUNCTION__, mId);
454 return res;
455 }
456
457 res = native_window_set_buffers_transform(mConsumer.get(),
458 mTransform);
459 if (res != OK) {
460 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
461 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700462 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700463 }
464
Zhijun He125684a2015-12-26 15:07:30 -0800465 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800466 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800467 * buffers to be statically allocated for internal static buffer registration, while the
468 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800469 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
470 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
471 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800472 */
473 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100474 uint64_t consumerUsage = 0;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700475 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800476 StreamInfo streamInfo(
477 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Emilian Peev050f5dc2017-05-18 14:43:56 +0100478 mUsage | consumerUsage, mTotalBufferCount,
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700479 /*isConfigured*/true);
480 wp<Camera3OutputStream> weakThis(this);
481 res = mBufferManager->registerStream(weakThis,
482 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800483 if (res == OK) {
484 // Disable buffer allocation for this BufferQueue, buffer manager will take over
485 // the buffer allocation responsibility.
486 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
487 mUseBufferManager = true;
488 } else {
489 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
490 "(error %d %s), fall back to BufferQueue for buffer management!",
491 __FUNCTION__, mId, res, strerror(-res));
492 }
493 }
494
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800495 return OK;
496}
497
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800498status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
499 ATRACE_CALL();
500 status_t res;
501
502 if ((res = getBufferPreconditionCheckLocked()) != OK) {
503 return res;
504 }
505
506 bool gotBufferFromManager = false;
507
508 if (mUseBufferManager) {
509 sp<GraphicBuffer> gb;
510 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
511 if (res == OK) {
512 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
513 // successful return.
514 *anb = gb.get();
515 res = mConsumer->attachBuffer(*anb);
516 if (res != OK) {
517 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
518 __FUNCTION__, mId, strerror(-res), res);
519 return res;
520 }
521 gotBufferFromManager = true;
522 ALOGV("Stream %d: Attached new buffer", getId());
523 } else if (res == ALREADY_EXISTS) {
524 // Have sufficient free buffers already attached, can just
525 // dequeue from buffer queue
526 ALOGV("Stream %d: Reusing attached buffer", getId());
527 gotBufferFromManager = false;
528 } else if (res != OK) {
529 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
530 __FUNCTION__, mId, strerror(-res), res);
531 return res;
532 }
533 }
534 if (!gotBufferFromManager) {
535 /**
536 * Release the lock briefly to avoid deadlock for below scenario:
537 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
538 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
539 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
540 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
541 * StreamingProcessor lock.
542 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
543 * and try to lock bufferQueue lock.
544 * Then there is circular locking dependency.
545 */
546 sp<ANativeWindow> currentConsumer = mConsumer;
547 mLock.unlock();
548
Shuzhen Wang686f6442017-06-20 16:16:04 -0700549 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800550 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700551 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
552 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
553
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800554 mLock.lock();
555 if (res != OK) {
556 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
557 __FUNCTION__, mId, strerror(-res), res);
558
559 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
560 // let prepareNextBuffer handle the error.)
561 if (res == NO_INIT && mState == STATE_CONFIGURED) {
562 mState = STATE_ABANDONED;
563 }
564
565 return res;
566 }
567 }
568
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700569 if (res == OK) {
570 std::vector<sp<GraphicBuffer>> removedBuffers;
571 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
572 if (res == OK) {
573 onBuffersRemovedLocked(removedBuffers);
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700574
575 if (mUseBufferManager && removedBuffers.size() > 0) {
576 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
577 }
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700578 }
579 }
580
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800581 return res;
582}
583
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800584status_t Camera3OutputStream::disconnectLocked() {
585 status_t res;
586
Igor Murashkine3a9f962013-05-08 18:03:15 -0700587 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
588 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800589 }
590
Zhijun He5d677d12016-05-29 16:52:39 -0700591 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
592 // state), don't need change the stream state, return OK.
593 if (mConsumer == nullptr) {
594 return OK;
595 }
596
Zhijun He125684a2015-12-26 15:07:30 -0800597 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
598
Igor Murashkine3a9f962013-05-08 18:03:15 -0700599 res = native_window_api_disconnect(mConsumer.get(),
600 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800601 /**
602 * This is not an error. if client calling process dies, the window will
603 * also die and all calls to it will return DEAD_OBJECT, thus it's already
604 * "disconnected"
605 */
606 if (res == DEAD_OBJECT) {
607 ALOGW("%s: While disconnecting stream %d from native window, the"
608 " native window died from under us", __FUNCTION__, mId);
609 }
610 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700611 ALOGE("%s: Unable to disconnect stream %d from native window "
612 "(error %d %s)",
613 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800614 mState = STATE_ERROR;
615 return res;
616 }
617
Zhijun He125684a2015-12-26 15:07:30 -0800618 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
619 // stream at this point should be safe.
620 if (mUseBufferManager) {
621 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
622 if (res != OK) {
623 ALOGE("%s: Unable to unregister stream %d from buffer manager "
624 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
625 mState = STATE_ERROR;
626 return res;
627 }
628 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
629 // the stream is still in usable state after this call.
630 mUseBufferManager = false;
631 }
632
Igor Murashkine3a9f962013-05-08 18:03:15 -0700633 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
634 : STATE_CONSTRUCTED;
Shuzhen Wang686f6442017-06-20 16:16:04 -0700635
636 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
637 mDequeueBufferLatency.reset();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800638 return OK;
639}
640
Emilian Peev050f5dc2017-05-18 14:43:56 +0100641status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700642
643 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700644
Zhijun He5d677d12016-05-29 16:52:39 -0700645 if (mConsumer == nullptr) {
646 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
647 *usage = mConsumerUsage;
648 return OK;
649 }
650
Shuzhen Wang0129d522016-10-30 22:43:41 -0700651 res = getEndpointUsageForSurface(usage, mConsumer);
652
653 return res;
654}
655
Emilian Peev050f5dc2017-05-18 14:43:56 +0100656status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700657 const sp<Surface>& surface) const {
658 status_t res;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100659 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700660
Emilian Peev050f5dc2017-05-18 14:43:56 +0100661 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700662 // If an opaque output stream's endpoint is ImageReader, add
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700663 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700664 // for the ZSL use case.
665 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
666 // 1. GRALLOC_USAGE_HW_TEXTURE
667 // 2. GRALLOC_USAGE_HW_RENDER
668 // 3. GRALLOC_USAGE_HW_COMPOSER
669 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
670 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
Shuzhen Wang0129d522016-10-30 22:43:41 -0700671 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
672 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700673 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700674 }
675
676 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700677 return res;
678}
679
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700680bool Camera3OutputStream::isVideoStream() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100681 uint64_t usage = 0;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700682 status_t res = getEndpointUsage(&usage);
683 if (res != OK) {
684 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
685 return false;
686 }
687
688 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
689}
690
Zhijun He125684a2015-12-26 15:07:30 -0800691status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
692 Mutex::Autolock l(mLock);
693 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700694 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800695 __FUNCTION__);
696 return INVALID_OPERATION;
697 }
698 mBufferManager = bufferManager;
699
700 return OK;
701}
702
Emilian Peev40ead602017-09-26 15:46:36 +0100703status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
704 const std::vector<OutputStreamInfo> &/*outputInfo*/,
705 const std::vector<size_t> &/*removedSurfaceIds*/,
706 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
707 ALOGE("%s: this method is not supported!", __FUNCTION__);
708 return INVALID_OPERATION;
709}
710
Zhijun He125684a2015-12-26 15:07:30 -0800711void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
712 sp<Camera3OutputStream> stream = mParent.promote();
713 if (stream == nullptr) {
714 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
715 return;
716 }
717
718 Mutex::Autolock l(stream->mLock);
719 if (!(stream->mUseBufferManager)) {
720 return;
721 }
722
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700723 ALOGV("Stream %d: Buffer released", stream->getId());
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700724 bool shouldFreeBuffer = false;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700725 status_t res = stream->mBufferManager->onBufferReleased(
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700726 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700727 if (res != OK) {
728 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
729 strerror(-res), res);
730 stream->mState = STATE_ERROR;
731 }
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700732
733 if (shouldFreeBuffer) {
734 sp<GraphicBuffer> buffer;
735 // Detach and free a buffer (when buffer goes out of scope)
736 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
737 if (buffer.get() != nullptr) {
738 stream->mBufferManager->notifyBufferRemoved(
739 stream->getId(), stream->getStreamSetId());
740 }
741 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700742}
743
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700744void Camera3OutputStream::onBuffersRemovedLocked(
745 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700746 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700747 if (callback != nullptr) {
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700748 for (const auto& gb : removedBuffers) {
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700749 callback->onBufferFreed(mId, gb->handle);
750 }
751 }
752}
753
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700754status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
755 Mutex::Autolock l(mLock);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700756 return detachBufferLocked(buffer, fenceFd);
757}
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700758
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700759status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700760 ALOGV("Stream %d: detachBuffer", getId());
761 if (buffer == nullptr) {
762 return BAD_VALUE;
763 }
764
Zhijun He125684a2015-12-26 15:07:30 -0800765 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700766 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800767 if (res == NO_MEMORY) {
768 // This may rarely happen, which indicates that the released buffer was freed by other
769 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
770 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
771 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700772 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800773 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
774 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700775 // Treat other errors as abandonment
Zhijun He125684a2015-12-26 15:07:30 -0800776 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700777 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700778 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800779 }
780
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700781 if (fenceFd != nullptr) {
782 if (fence!= 0 && fence->isValid()) {
783 *fenceFd = fence->dup();
784 } else {
785 *fenceFd = -1;
786 }
Zhijun He125684a2015-12-26 15:07:30 -0800787 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700788
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700789 std::vector<sp<GraphicBuffer>> removedBuffers;
790 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
791 if (res == OK) {
792 onBuffersRemovedLocked(removedBuffers);
793 }
794 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800795}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800796
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700797status_t Camera3OutputStream::dropBuffers(bool dropping) {
798 Mutex::Autolock l(mLock);
799 mDropBuffers = dropping;
800 return OK;
801}
802
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800803status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700804 return OK;
805}
806
807bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700808 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700809
810 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800811 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700812 }
Zhijun He5d677d12016-05-29 16:52:39 -0700813 return mConsumer == nullptr;
814}
815
Shuzhen Wang758c2152017-01-10 18:26:18 -0800816status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800817 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800818 if (consumers.size() != 1) {
819 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
820 __FUNCTION__, consumers.size());
821 return INVALID_OPERATION;
822 }
823 if (consumers[0] == nullptr) {
824 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700825 return INVALID_OPERATION;
826 }
827
828 if (mConsumer != nullptr) {
829 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
830 return INVALID_OPERATION;
831 }
832
Shuzhen Wang758c2152017-01-10 18:26:18 -0800833 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700834 return OK;
835}
836
Shuzhen Wang13a69632016-01-26 09:51:07 -0800837bool Camera3OutputStream::isConsumedByHWComposer() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100838 uint64_t usage = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800839 status_t res = getEndpointUsage(&usage);
840 if (res != OK) {
841 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
842 return false;
843 }
844
845 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
846}
847
Zhijun Hef0645c12016-08-02 00:58:11 -0700848bool Camera3OutputStream::isConsumedByHWTexture() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100849 uint64_t usage = 0;
Zhijun Hef0645c12016-08-02 00:58:11 -0700850 status_t res = getEndpointUsage(&usage);
851 if (res != OK) {
852 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
853 return false;
854 }
855
856 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
857}
858
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800859}; // namespace camera3
860
861}; // namespace android