1 /*
2 * Copyright (C) 2013-2018 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
21 #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
30 namespace android {
31
32 namespace camera3 {
33
Camera3OutputStream(int id,sp<Surface> consumer,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation,nsecs_t timestampOffset,const String8 & physicalCameraId,int setId)34 Camera3OutputStream::Camera3OutputStream(int id,
35 sp<Surface> consumer,
36 uint32_t width, uint32_t height, int format,
37 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
38 nsecs_t timestampOffset, const String8& physicalCameraId,
39 int setId) :
40 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
41 /*maxSize*/0, format, dataSpace, rotation,
42 physicalCameraId, setId),
43 mConsumer(consumer),
44 mTransform(0),
45 mTraceFirstBuffer(true),
46 mUseBufferManager(false),
47 mTimestampOffset(timestampOffset),
48 mConsumerUsage(0),
49 mDropBuffers(false),
50 mDequeueBufferLatency(kDequeueLatencyBinSize) {
51
52 if (mConsumer == NULL) {
53 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
54 mState = STATE_ERROR;
55 }
56
57 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
58 mBufferReleasedListener = new BufferReleasedListener(this);
59 }
60 }
61
Camera3OutputStream(int id,sp<Surface> consumer,uint32_t width,uint32_t height,size_t maxSize,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation,nsecs_t timestampOffset,const String8 & physicalCameraId,int setId)62 Camera3OutputStream::Camera3OutputStream(int id,
63 sp<Surface> consumer,
64 uint32_t width, uint32_t height, size_t maxSize, int format,
65 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
66 nsecs_t timestampOffset, const String8& physicalCameraId, int setId) :
67 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
68 format, dataSpace, rotation, physicalCameraId, setId),
69 mConsumer(consumer),
70 mTransform(0),
71 mTraceFirstBuffer(true),
72 mUseMonoTimestamp(false),
73 mUseBufferManager(false),
74 mTimestampOffset(timestampOffset),
75 mConsumerUsage(0),
76 mDropBuffers(false),
77 mDequeueBufferLatency(kDequeueLatencyBinSize) {
78
79 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
80 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 }
89
90 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
91 mBufferReleasedListener = new BufferReleasedListener(this);
92 }
93 }
94
Camera3OutputStream(int id,uint32_t width,uint32_t height,int format,uint64_t consumerUsage,android_dataspace dataSpace,camera3_stream_rotation_t rotation,nsecs_t timestampOffset,const String8 & physicalCameraId,int setId)95 Camera3OutputStream::Camera3OutputStream(int id,
96 uint32_t width, uint32_t height, int format,
97 uint64_t consumerUsage, android_dataspace dataSpace,
98 camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
99 const String8& physicalCameraId, int setId) :
100 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
101 /*maxSize*/0, format, dataSpace, rotation,
102 physicalCameraId, setId),
103 mConsumer(nullptr),
104 mTransform(0),
105 mTraceFirstBuffer(true),
106 mUseBufferManager(false),
107 mTimestampOffset(timestampOffset),
108 mConsumerUsage(consumerUsage),
109 mDropBuffers(false),
110 mDequeueBufferLatency(kDequeueLatencyBinSize) {
111 // 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) {
121 ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!",
122 __FUNCTION__, consumerUsage);
123 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
Camera3OutputStream(int id,camera3_stream_type_t type,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation,const String8 & physicalCameraId,uint64_t consumerUsage,nsecs_t timestampOffset,int setId)133 Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
134 uint32_t width, uint32_t height,
135 int format,
136 android_dataspace dataSpace,
137 camera3_stream_rotation_t rotation,
138 const String8& physicalCameraId,
139 uint64_t consumerUsage, nsecs_t timestampOffset,
140 int setId) :
141 Camera3IOStreamBase(id, type, width, height,
142 /*maxSize*/0,
143 format, dataSpace, rotation,
144 physicalCameraId, setId),
145 mTransform(0),
146 mTraceFirstBuffer(true),
147 mUseMonoTimestamp(false),
148 mUseBufferManager(false),
149 mTimestampOffset(timestampOffset),
150 mConsumerUsage(consumerUsage),
151 mDropBuffers(false),
152 mDequeueBufferLatency(kDequeueLatencyBinSize) {
153
154 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
155 mBufferReleasedListener = new BufferReleasedListener(this);
156 }
157
158 // Subclasses expected to initialize mConsumer themselves
159 }
160
161
~Camera3OutputStream()162 Camera3OutputStream::~Camera3OutputStream() {
163 disconnectLocked();
164 }
165
getBufferLocked(camera3_stream_buffer * buffer,const std::vector<size_t> &)166 status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
167 const std::vector<size_t>&) {
168 ATRACE_CALL();
169
170 ANativeWindowBuffer* anb;
171 int fenceFd = -1;
172
173 status_t res;
174 res = getBufferLockedCommon(&anb, &fenceFd);
175 if (res != OK) {
176 return res;
177 }
178
179 /**
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,
184 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
185
186 return OK;
187 }
188
queueBufferToConsumer(sp<ANativeWindow> & consumer,ANativeWindowBuffer * buffer,int anwReleaseFence)189 status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
190 ANativeWindowBuffer* buffer, int anwReleaseFence) {
191 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
192 }
193
returnBufferLocked(const camera3_stream_buffer & buffer,nsecs_t timestamp)194 status_t Camera3OutputStream::returnBufferLocked(
195 const camera3_stream_buffer &buffer,
196 nsecs_t timestamp) {
197 ATRACE_CALL();
198
199 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
200
201 if (res != OK) {
202 return res;
203 }
204
205 mLastTimestamp = timestamp;
206 mFrameCount++;
207
208 return OK;
209 }
210
returnBufferCheckedLocked(const camera3_stream_buffer & buffer,nsecs_t timestamp,bool output,sp<Fence> * releaseFenceOut)211 status_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
221 status_t res;
222
223 // Fence management - always honor release fence from HAL
224 sp<Fence> releaseFence = new Fence(buffer.release_fence);
225 int anwReleaseFence = releaseFence->dup();
226
227 /**
228 * 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
236 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
237 /**
238 * Return buffer back to ANativeWindow
239 */
240 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR || mDropBuffers) {
241 // Cancel buffer
242 if (mDropBuffers) {
243 ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId);
244 } else {
245 ALOGW("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId);
246 }
247
248 res = currentConsumer->cancelBuffer(currentConsumer.get(),
249 anwBuffer,
250 anwReleaseFence);
251 if (res != OK) {
252 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
253 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
254 }
255
256 notifyBufferReleased(anwBuffer);
257 if (mUseBufferManager) {
258 // Return this buffer back to buffer manager.
259 mBufferReleasedListener->onBufferReleased();
260 }
261 } else {
262 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
263 {
264 char traceLog[48];
265 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
266 ATRACE_NAME(traceLog);
267 }
268 mTraceFirstBuffer = false;
269 }
270
271 /* Certain consumers (such as AudioSource or HardwareComposer) use
272 * MONOTONIC time, causing time misalignment if camera timestamp is
273 * in BOOTTIME. Do the conversion if necessary. */
274 res = native_window_set_buffers_timestamp(mConsumer.get(),
275 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
276 if (res != OK) {
277 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
278 __FUNCTION__, mId, strerror(-res), res);
279 return res;
280 }
281
282 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence);
283 if (res != OK) {
284 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
285 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
286 }
287 }
288 mLock.lock();
289
290 // Once a valid buffer has been returned to the queue, can no longer
291 // dequeue all buffers for preallocation.
292 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
293 mStreamUnpreparable = true;
294 }
295
296 if (res != OK) {
297 close(anwReleaseFence);
298 }
299
300 *releaseFenceOut = releaseFence;
301
302 return res;
303 }
304
dump(int fd,const Vector<String16> & args) const305 void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
306 (void) args;
307 String8 lines;
308 lines.appendFormat(" Stream[%d]: Output\n", mId);
309 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
310 write(fd, lines.string(), lines.size());
311
312 Camera3IOStreamBase::dump(fd, args);
313
314 mDequeueBufferLatency.dump(fd,
315 " DequeueBuffer latency histogram:");
316 }
317
setTransform(int transform)318 status_t Camera3OutputStream::setTransform(int transform) {
319 ATRACE_CALL();
320 Mutex::Autolock l(mLock);
321 return setTransformLocked(transform);
322 }
323
setTransformLocked(int transform)324 status_t Camera3OutputStream::setTransformLocked(int transform) {
325 status_t res = OK;
326 if (mState == STATE_ERROR) {
327 ALOGE("%s: Stream in error state", __FUNCTION__);
328 return INVALID_OPERATION;
329 }
330
331 mTransform = transform;
332 if (mState == STATE_CONFIGURED) {
333 res = native_window_set_buffers_transform(mConsumer.get(),
334 transform);
335 if (res != OK) {
336 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
337 __FUNCTION__, transform, strerror(-res), res);
338 }
339 }
340 return res;
341 }
342
configureQueueLocked()343 status_t Camera3OutputStream::configureQueueLocked() {
344 status_t res;
345
346 mTraceFirstBuffer = true;
347 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
348 return res;
349 }
350
351 if ((res = configureConsumerQueueLocked()) != OK) {
352 return res;
353 }
354
355 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
356 // We need skip these cases as timeout will disable the non-blocking (async) mode.
357 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
358 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
359 }
360
361 return OK;
362 }
363
configureConsumerQueueLocked()364 status_t Camera3OutputStream::configureConsumerQueueLocked() {
365 status_t res;
366
367 mTraceFirstBuffer = true;
368
369 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
370
371 // Configure consumer-side ANativeWindow interface. The listener may be used
372 // to notify buffer manager (if it is used) of the returned buffers.
373 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
374 /*listener*/mBufferReleasedListener,
375 /*reportBufferRemoval*/true);
376 if (res != OK) {
377 ALOGE("%s: Unable to connect to native window for stream %d",
378 __FUNCTION__, mId);
379 return res;
380 }
381
382 mConsumerName = mConsumer->getConsumerName();
383
384 res = native_window_set_usage(mConsumer.get(), mUsage);
385 if (res != OK) {
386 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
387 __FUNCTION__, mUsage, mId);
388 return res;
389 }
390
391 res = native_window_set_scaling_mode(mConsumer.get(),
392 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
393 if (res != OK) {
394 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
395 __FUNCTION__, strerror(-res), res);
396 return res;
397 }
398
399 if (mMaxSize == 0) {
400 // For buffers of known size
401 res = native_window_set_buffers_dimensions(mConsumer.get(),
402 camera3_stream::width, camera3_stream::height);
403 } else {
404 // For buffers with bounded size
405 res = native_window_set_buffers_dimensions(mConsumer.get(),
406 mMaxSize, 1);
407 }
408 if (res != OK) {
409 ALOGE("%s: Unable to configure stream buffer dimensions"
410 " %d x %d (maxSize %zu) for stream %d",
411 __FUNCTION__, camera3_stream::width, camera3_stream::height,
412 mMaxSize, mId);
413 return res;
414 }
415 res = native_window_set_buffers_format(mConsumer.get(),
416 camera3_stream::format);
417 if (res != OK) {
418 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
419 __FUNCTION__, camera3_stream::format, mId);
420 return res;
421 }
422
423 res = native_window_set_buffers_data_space(mConsumer.get(),
424 camera3_stream::data_space);
425 if (res != OK) {
426 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
427 __FUNCTION__, camera3_stream::data_space, mId);
428 return res;
429 }
430
431 int maxConsumerBuffers;
432 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
433 mConsumer.get(),
434 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
435 if (res != OK) {
436 ALOGE("%s: Unable to query consumer undequeued"
437 " buffer count for stream %d", __FUNCTION__, mId);
438 return res;
439 }
440
441 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
442 maxConsumerBuffers, camera3_stream::max_buffers);
443 if (camera3_stream::max_buffers == 0) {
444 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
445 __FUNCTION__, camera3_stream::max_buffers);
446 return INVALID_OPERATION;
447 }
448
449 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
450 mHandoutTotalBufferCount = 0;
451 mFrameCount = 0;
452 mLastTimestamp = 0;
453 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
454
455 res = native_window_set_buffer_count(mConsumer.get(),
456 mTotalBufferCount);
457 if (res != OK) {
458 ALOGE("%s: Unable to set buffer count for stream %d",
459 __FUNCTION__, mId);
460 return res;
461 }
462
463 res = native_window_set_buffers_transform(mConsumer.get(),
464 mTransform);
465 if (res != OK) {
466 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
467 __FUNCTION__, mTransform, strerror(-res), res);
468 return res;
469 }
470
471 /**
472 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
473 * buffers to be statically allocated for internal static buffer registration, while the
474 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
475 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
476 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
477 * HAL3.2 devices may not support the dynamic buffer registeration.
478 */
479 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
480 uint64_t consumerUsage = 0;
481 getEndpointUsage(&consumerUsage);
482 StreamInfo streamInfo(
483 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
484 mUsage | consumerUsage, mTotalBufferCount,
485 /*isConfigured*/true);
486 wp<Camera3OutputStream> weakThis(this);
487 res = mBufferManager->registerStream(weakThis,
488 streamInfo);
489 if (res == OK) {
490 // Disable buffer allocation for this BufferQueue, buffer manager will take over
491 // the buffer allocation responsibility.
492 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
493 mUseBufferManager = true;
494 } else {
495 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
496 "(error %d %s), fall back to BufferQueue for buffer management!",
497 __FUNCTION__, mId, res, strerror(-res));
498 }
499 }
500
501 return OK;
502 }
503
getBufferLockedCommon(ANativeWindowBuffer ** anb,int * fenceFd)504 status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
505 ATRACE_CALL();
506 status_t res;
507
508 if ((res = getBufferPreconditionCheckLocked()) != OK) {
509 return res;
510 }
511
512 bool gotBufferFromManager = false;
513
514 if (mUseBufferManager) {
515 sp<GraphicBuffer> gb;
516 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
517 if (res == OK) {
518 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
519 // successful return.
520 *anb = gb.get();
521 res = mConsumer->attachBuffer(*anb);
522 if (res != OK) {
523 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
524 __FUNCTION__, mId, strerror(-res), res);
525 return res;
526 }
527 gotBufferFromManager = true;
528 ALOGV("Stream %d: Attached new buffer", getId());
529 } else if (res == ALREADY_EXISTS) {
530 // Have sufficient free buffers already attached, can just
531 // dequeue from buffer queue
532 ALOGV("Stream %d: Reusing attached buffer", getId());
533 gotBufferFromManager = false;
534 } else if (res != OK) {
535 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
536 __FUNCTION__, mId, strerror(-res), res);
537 return res;
538 }
539 }
540 if (!gotBufferFromManager) {
541 /**
542 * Release the lock briefly to avoid deadlock for below scenario:
543 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
544 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
545 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
546 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
547 * StreamingProcessor lock.
548 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
549 * and try to lock bufferQueue lock.
550 * Then there is circular locking dependency.
551 */
552 sp<ANativeWindow> currentConsumer = mConsumer;
553 mLock.unlock();
554
555 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
556 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
557 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
558 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
559
560 mLock.lock();
561 if (res != OK) {
562 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
563 __FUNCTION__, mId, strerror(-res), res);
564
565 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
566 // let prepareNextBuffer handle the error.)
567 if (res == NO_INIT && mState == STATE_CONFIGURED) {
568 mState = STATE_ABANDONED;
569 }
570
571 return res;
572 }
573 }
574
575 if (res == OK) {
576 std::vector<sp<GraphicBuffer>> removedBuffers;
577 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
578 if (res == OK) {
579 onBuffersRemovedLocked(removedBuffers);
580
581 if (mUseBufferManager && removedBuffers.size() > 0) {
582 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
583 }
584 }
585 }
586
587 return res;
588 }
589
disconnectLocked()590 status_t Camera3OutputStream::disconnectLocked() {
591 status_t res;
592
593 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
594 return res;
595 }
596
597 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
598 // state), don't need change the stream state, return OK.
599 if (mConsumer == nullptr) {
600 return OK;
601 }
602
603 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
604
605 res = native_window_api_disconnect(mConsumer.get(),
606 NATIVE_WINDOW_API_CAMERA);
607 /**
608 * This is not an error. if client calling process dies, the window will
609 * also die and all calls to it will return DEAD_OBJECT, thus it's already
610 * "disconnected"
611 */
612 if (res == DEAD_OBJECT) {
613 ALOGW("%s: While disconnecting stream %d from native window, the"
614 " native window died from under us", __FUNCTION__, mId);
615 }
616 else if (res != OK) {
617 ALOGE("%s: Unable to disconnect stream %d from native window "
618 "(error %d %s)",
619 __FUNCTION__, mId, res, strerror(-res));
620 mState = STATE_ERROR;
621 return res;
622 }
623
624 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
625 // stream at this point should be safe.
626 if (mUseBufferManager) {
627 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
628 if (res != OK) {
629 ALOGE("%s: Unable to unregister stream %d from buffer manager "
630 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
631 mState = STATE_ERROR;
632 return res;
633 }
634 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
635 // the stream is still in usable state after this call.
636 mUseBufferManager = false;
637 }
638
639 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
640 : STATE_CONSTRUCTED;
641
642 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
643 mDequeueBufferLatency.reset();
644 return OK;
645 }
646
getEndpointUsage(uint64_t * usage) const647 status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
648
649 status_t res;
650
651 if (mConsumer == nullptr) {
652 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
653 *usage = mConsumerUsage;
654 return OK;
655 }
656
657 res = getEndpointUsageForSurface(usage, mConsumer);
658
659 return res;
660 }
661
getEndpointUsageForSurface(uint64_t * usage,const sp<Surface> & surface) const662 status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
663 const sp<Surface>& surface) const {
664 status_t res;
665 uint64_t u = 0;
666
667 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
668 // If an opaque output stream's endpoint is ImageReader, add
669 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
670 // for the ZSL use case.
671 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
672 // 1. GRALLOC_USAGE_HW_TEXTURE
673 // 2. GRALLOC_USAGE_HW_RENDER
674 // 3. GRALLOC_USAGE_HW_COMPOSER
675 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
676 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
677 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
678 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
679 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
680 }
681
682 *usage = u;
683 return res;
684 }
685
isVideoStream() const686 bool Camera3OutputStream::isVideoStream() const {
687 uint64_t usage = 0;
688 status_t res = getEndpointUsage(&usage);
689 if (res != OK) {
690 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
691 return false;
692 }
693
694 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
695 }
696
setBufferManager(sp<Camera3BufferManager> bufferManager)697 status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
698 Mutex::Autolock l(mLock);
699 if (mState != STATE_CONSTRUCTED) {
700 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
701 __FUNCTION__);
702 return INVALID_OPERATION;
703 }
704 mBufferManager = bufferManager;
705
706 return OK;
707 }
708
updateStream(const std::vector<sp<Surface>> &,const std::vector<OutputStreamInfo> &,const std::vector<size_t> &,KeyedVector<sp<Surface>,size_t> *)709 status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
710 const std::vector<OutputStreamInfo> &/*outputInfo*/,
711 const std::vector<size_t> &/*removedSurfaceIds*/,
712 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
713 ALOGE("%s: this method is not supported!", __FUNCTION__);
714 return INVALID_OPERATION;
715 }
716
onBufferReleased()717 void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
718 sp<Camera3OutputStream> stream = mParent.promote();
719 if (stream == nullptr) {
720 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
721 return;
722 }
723
724 Mutex::Autolock l(stream->mLock);
725 if (!(stream->mUseBufferManager)) {
726 return;
727 }
728
729 ALOGV("Stream %d: Buffer released", stream->getId());
730 bool shouldFreeBuffer = false;
731 status_t res = stream->mBufferManager->onBufferReleased(
732 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
733 if (res != OK) {
734 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
735 strerror(-res), res);
736 stream->mState = STATE_ERROR;
737 }
738
739 if (shouldFreeBuffer) {
740 sp<GraphicBuffer> buffer;
741 // Detach and free a buffer (when buffer goes out of scope)
742 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
743 if (buffer.get() != nullptr) {
744 stream->mBufferManager->notifyBufferRemoved(
745 stream->getId(), stream->getStreamSetId());
746 }
747 }
748 }
749
onBuffersRemovedLocked(const std::vector<sp<GraphicBuffer>> & removedBuffers)750 void Camera3OutputStream::onBuffersRemovedLocked(
751 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
752 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
753 if (callback != nullptr) {
754 for (const auto& gb : removedBuffers) {
755 callback->onBufferFreed(mId, gb->handle);
756 }
757 }
758 }
759
detachBuffer(sp<GraphicBuffer> * buffer,int * fenceFd)760 status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
761 Mutex::Autolock l(mLock);
762 return detachBufferLocked(buffer, fenceFd);
763 }
764
detachBufferLocked(sp<GraphicBuffer> * buffer,int * fenceFd)765 status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
766 ALOGV("Stream %d: detachBuffer", getId());
767 if (buffer == nullptr) {
768 return BAD_VALUE;
769 }
770
771 sp<Fence> fence;
772 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
773 if (res == NO_MEMORY) {
774 // This may rarely happen, which indicates that the released buffer was freed by other
775 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
776 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
777 // therefore log a warning.
778 *buffer = 0;
779 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
780 } else if (res != OK) {
781 // Treat other errors as abandonment
782 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
783 mState = STATE_ABANDONED;
784 return res;
785 }
786
787 if (fenceFd != nullptr) {
788 if (fence!= 0 && fence->isValid()) {
789 *fenceFd = fence->dup();
790 } else {
791 *fenceFd = -1;
792 }
793 }
794
795 std::vector<sp<GraphicBuffer>> removedBuffers;
796 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
797 if (res == OK) {
798 onBuffersRemovedLocked(removedBuffers);
799 }
800 return res;
801 }
802
dropBuffers(bool dropping)803 status_t Camera3OutputStream::dropBuffers(bool dropping) {
804 Mutex::Autolock l(mLock);
805 mDropBuffers = dropping;
806 return OK;
807 }
808
getPhysicalCameraId() const809 const String8& Camera3OutputStream::getPhysicalCameraId() const {
810 Mutex::Autolock l(mLock);
811 return physicalCameraId();
812 }
813
notifyBufferReleased(ANativeWindowBuffer *)814 status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
815 return OK;
816 }
817
isConsumerConfigurationDeferred(size_t surface_id) const818 bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
819 Mutex::Autolock l(mLock);
820
821 if (surface_id != 0) {
822 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
823 }
824 return mConsumer == nullptr;
825 }
826
setConsumers(const std::vector<sp<Surface>> & consumers)827 status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
828 Mutex::Autolock l(mLock);
829 if (consumers.size() != 1) {
830 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
831 __FUNCTION__, consumers.size());
832 return INVALID_OPERATION;
833 }
834 if (consumers[0] == nullptr) {
835 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
836 return INVALID_OPERATION;
837 }
838
839 if (mConsumer != nullptr) {
840 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
841 return INVALID_OPERATION;
842 }
843
844 mConsumer = consumers[0];
845 return OK;
846 }
847
isConsumedByHWComposer() const848 bool Camera3OutputStream::isConsumedByHWComposer() const {
849 uint64_t usage = 0;
850 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_COMPOSER) != 0;
857 }
858
isConsumedByHWTexture() const859 bool Camera3OutputStream::isConsumedByHWTexture() const {
860 uint64_t usage = 0;
861 status_t res = getEndpointUsage(&usage);
862 if (res != OK) {
863 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
864 return false;
865 }
866
867 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
868 }
869
870 }; // namespace camera3
871
872 }; // namespace android
873