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-Stream"
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 "device3/Camera3Stream.h"
24 #include "device3/StatusTracker.h"
25 #include "utils/TraceHFR.h"
26
27 #include <cutils/properties.h>
28
29 namespace android {
30
31 namespace camera3 {
32
~Camera3Stream()33 Camera3Stream::~Camera3Stream() {
34 sp<StatusTracker> statusTracker = mStatusTracker.promote();
35 if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
36 statusTracker->removeComponent(mStatusId);
37 }
38 }
39
cast(camera3_stream * stream)40 Camera3Stream* Camera3Stream::cast(camera3_stream *stream) {
41 return static_cast<Camera3Stream*>(stream);
42 }
43
cast(const camera3_stream * stream)44 const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) {
45 return static_cast<const Camera3Stream*>(stream);
46 }
47
Camera3Stream(int id,camera3_stream_type type,uint32_t width,uint32_t height,size_t maxSize,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation,const String8 & physicalCameraId,int setId)48 Camera3Stream::Camera3Stream(int id,
49 camera3_stream_type type,
50 uint32_t width, uint32_t height, size_t maxSize, int format,
51 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
52 const String8& physicalCameraId, int setId) :
53 camera3_stream(),
54 mId(id),
55 mSetId(setId),
56 mName(String8::format("Camera3Stream[%d]", id)),
57 mMaxSize(maxSize),
58 mState(STATE_CONSTRUCTED),
59 mStatusId(StatusTracker::NO_STATUS_ID),
60 mStreamUnpreparable(true),
61 mUsage(0),
62 mOldUsage(0),
63 mOldMaxBuffers(0),
64 mOldFormat(-1),
65 mOldDataSpace(HAL_DATASPACE_UNKNOWN),
66 mPrepared(false),
67 mPrepareBlockRequest(true),
68 mPreparedBufferIdx(0),
69 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
70 mBufferLimitLatency(kBufferLimitLatencyBinSize),
71 mFormatOverridden(false),
72 mOriginalFormat(format),
73 mDataSpaceOverridden(false),
74 mOriginalDataSpace(dataSpace),
75 mPhysicalCameraId(physicalCameraId),
76 mLastTimestamp(0) {
77
78 camera3_stream::stream_type = type;
79 camera3_stream::width = width;
80 camera3_stream::height = height;
81 camera3_stream::format = format;
82 camera3_stream::data_space = dataSpace;
83 camera3_stream::rotation = rotation;
84 camera3_stream::max_buffers = 0;
85 camera3_stream::priv = NULL;
86 camera3_stream::physical_camera_id = mPhysicalCameraId.string();
87
88 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
89 maxSize == 0) {
90 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
91 mState = STATE_ERROR;
92 }
93 }
94
getId() const95 int Camera3Stream::getId() const {
96 return mId;
97 }
98
getStreamSetId() const99 int Camera3Stream::getStreamSetId() const {
100 return mSetId;
101 }
102
getWidth() const103 uint32_t Camera3Stream::getWidth() const {
104 return camera3_stream::width;
105 }
106
getHeight() const107 uint32_t Camera3Stream::getHeight() const {
108 return camera3_stream::height;
109 }
110
getFormat() const111 int Camera3Stream::getFormat() const {
112 return camera3_stream::format;
113 }
114
getDataSpace() const115 android_dataspace Camera3Stream::getDataSpace() const {
116 return camera3_stream::data_space;
117 }
118
getUsage() const119 uint64_t Camera3Stream::getUsage() const {
120 return mUsage;
121 }
122
setUsage(uint64_t usage)123 void Camera3Stream::setUsage(uint64_t usage) {
124 mUsage = usage;
125 }
126
setFormatOverride(bool formatOverridden)127 void Camera3Stream::setFormatOverride(bool formatOverridden) {
128 mFormatOverridden = formatOverridden;
129 }
130
isFormatOverridden() const131 bool Camera3Stream::isFormatOverridden() const {
132 return mFormatOverridden;
133 }
134
getOriginalFormat() const135 int Camera3Stream::getOriginalFormat() const {
136 return mOriginalFormat;
137 }
138
setDataSpaceOverride(bool dataSpaceOverridden)139 void Camera3Stream::setDataSpaceOverride(bool dataSpaceOverridden) {
140 mDataSpaceOverridden = dataSpaceOverridden;
141 }
142
isDataSpaceOverridden() const143 bool Camera3Stream::isDataSpaceOverridden() const {
144 return mDataSpaceOverridden;
145 }
146
getOriginalDataSpace() const147 android_dataspace Camera3Stream::getOriginalDataSpace() const {
148 return mOriginalDataSpace;
149 }
150
physicalCameraId() const151 const String8& Camera3Stream::physicalCameraId() const {
152 return mPhysicalCameraId;
153 }
154
setOfflineProcessingSupport(bool support)155 void Camera3Stream::setOfflineProcessingSupport(bool support) {
156 mSupportOfflineProcessing = support;
157 }
158
getOfflineProcessingSupport() const159 bool Camera3Stream::getOfflineProcessingSupport() const {
160 return mSupportOfflineProcessing;
161 }
162
forceToIdle()163 status_t Camera3Stream::forceToIdle() {
164 ATRACE_CALL();
165 Mutex::Autolock l(mLock);
166 status_t res;
167
168 switch (mState) {
169 case STATE_ERROR:
170 case STATE_CONSTRUCTED:
171 case STATE_IN_CONFIG:
172 case STATE_PREPARING:
173 case STATE_IN_RECONFIG:
174 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
175 res = NO_INIT;
176 break;
177 case STATE_CONFIGURED:
178 if (hasOutstandingBuffersLocked()) {
179 sp<StatusTracker> statusTracker = mStatusTracker.promote();
180 if (statusTracker != 0) {
181 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
182 }
183 }
184
185 mState = STATE_IN_IDLE;
186 res = OK;
187
188 break;
189 default:
190 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
191 res = NO_INIT;
192 }
193
194 return res;
195 }
196
restoreConfiguredState()197 status_t Camera3Stream::restoreConfiguredState() {
198 ATRACE_CALL();
199 Mutex::Autolock l(mLock);
200 status_t res;
201
202 switch (mState) {
203 case STATE_ERROR:
204 case STATE_CONSTRUCTED:
205 case STATE_IN_CONFIG:
206 case STATE_PREPARING:
207 case STATE_IN_RECONFIG:
208 case STATE_CONFIGURED:
209 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
210 res = NO_INIT;
211 break;
212 case STATE_IN_IDLE:
213 if (hasOutstandingBuffersLocked()) {
214 sp<StatusTracker> statusTracker = mStatusTracker.promote();
215 if (statusTracker != 0) {
216 statusTracker->markComponentActive(mStatusId);
217 }
218 }
219
220 mState = STATE_CONFIGURED;
221 res = OK;
222
223 break;
224 default:
225 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
226 res = NO_INIT;
227 }
228
229 return res;
230 }
231
startConfiguration()232 camera3_stream* Camera3Stream::startConfiguration() {
233 ATRACE_CALL();
234 Mutex::Autolock l(mLock);
235 status_t res;
236
237 switch (mState) {
238 case STATE_ERROR:
239 ALOGE("%s: In error state", __FUNCTION__);
240 return NULL;
241 case STATE_CONSTRUCTED:
242 case STATE_IN_IDLE:
243 // OK
244 break;
245 case STATE_IN_CONFIG:
246 case STATE_IN_RECONFIG:
247 // Can start config again with no trouble; but don't redo
248 // mOldUsage/mOldMaxBuffers
249 return this;
250 case STATE_CONFIGURED:
251 if (hasOutstandingBuffersLocked()) {
252 ALOGE("%s: Cannot configure stream; has outstanding buffers",
253 __FUNCTION__);
254 return NULL;
255 }
256 break;
257 default:
258 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
259 return NULL;
260 }
261
262 mOldUsage = mUsage;
263 mOldMaxBuffers = camera3_stream::max_buffers;
264 mOldFormat = camera3_stream::format;
265 mOldDataSpace = camera3_stream::data_space;
266
267 res = getEndpointUsage(&mUsage);
268 if (res != OK) {
269 ALOGE("%s: Cannot query consumer endpoint usage!",
270 __FUNCTION__);
271 return NULL;
272 }
273
274 if (mState == STATE_IN_IDLE) {
275 // Skip configuration.
276 return this;
277 }
278
279 // Stop tracking if currently doing so
280 if (mStatusId != StatusTracker::NO_STATUS_ID) {
281 sp<StatusTracker> statusTracker = mStatusTracker.promote();
282 if (statusTracker != 0) {
283 statusTracker->removeComponent(mStatusId);
284 }
285 mStatusId = StatusTracker::NO_STATUS_ID;
286 }
287
288 if (mState == STATE_CONSTRUCTED) {
289 mState = STATE_IN_CONFIG;
290 } else { // mState == STATE_CONFIGURED
291 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
292 mState = STATE_IN_RECONFIG;
293 }
294
295 return this;
296 }
297
isConfiguring() const298 bool Camera3Stream::isConfiguring() const {
299 Mutex::Autolock l(mLock);
300 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
301 }
302
finishConfiguration(bool * streamReconfigured)303 status_t Camera3Stream::finishConfiguration(/*out*/bool* streamReconfigured) {
304 ATRACE_CALL();
305 if (streamReconfigured != nullptr) {
306 *streamReconfigured = false;
307 }
308 Mutex::Autolock l(mLock);
309 switch (mState) {
310 case STATE_ERROR:
311 ALOGE("%s: In error state", __FUNCTION__);
312 return INVALID_OPERATION;
313 case STATE_IN_CONFIG:
314 case STATE_IN_RECONFIG:
315 // OK
316 break;
317 case STATE_CONSTRUCTED:
318 case STATE_CONFIGURED:
319 ALOGE("%s: Cannot finish configuration that hasn't been started",
320 __FUNCTION__);
321 return INVALID_OPERATION;
322 case STATE_IN_IDLE:
323 //Skip configuration in this state
324 return OK;
325 default:
326 ALOGE("%s: Unknown state", __FUNCTION__);
327 return INVALID_OPERATION;
328 }
329
330 // Register for idle tracking
331 sp<StatusTracker> statusTracker = mStatusTracker.promote();
332 if (statusTracker != 0 && mStatusId == StatusTracker::NO_STATUS_ID) {
333 mStatusId = statusTracker->addComponent();
334 }
335
336 // Check if the stream configuration is unchanged, and skip reallocation if
337 // so. As documented in hardware/camera3.h:configure_streams().
338 if (mState == STATE_IN_RECONFIG &&
339 mOldUsage == mUsage &&
340 mOldMaxBuffers == camera3_stream::max_buffers &&
341 mOldDataSpace == camera3_stream::data_space &&
342 mOldFormat == camera3_stream::format) {
343 mState = STATE_CONFIGURED;
344 return OK;
345 }
346
347 // Reset prepared state, since buffer config has changed, and existing
348 // allocations are no longer valid
349 mPrepared = false;
350 mPrepareBlockRequest = true;
351 mStreamUnpreparable = false;
352
353 bool reconfiguring = (mState == STATE_IN_RECONFIG);
354 status_t res;
355 res = configureQueueLocked();
356 // configureQueueLocked could return error in case of abandoned surface.
357 // Treat as non-fatal error.
358 if (res == NO_INIT || res == DEAD_OBJECT) {
359 ALOGE("%s: Unable to configure stream %d queue (non-fatal): %s (%d)",
360 __FUNCTION__, mId, strerror(-res), res);
361 mState = STATE_ABANDONED;
362 return res;
363 } else if (res != OK) {
364 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
365 __FUNCTION__, mId, strerror(-res), res);
366 mState = STATE_ERROR;
367 return res;
368 }
369
370 if (reconfiguring && streamReconfigured != nullptr) {
371 *streamReconfigured = true;
372 }
373 mState = STATE_CONFIGURED;
374
375 return res;
376 }
377
cancelConfiguration()378 status_t Camera3Stream::cancelConfiguration() {
379 ATRACE_CALL();
380 Mutex::Autolock l(mLock);
381 switch (mState) {
382 case STATE_ERROR:
383 ALOGE("%s: In error state", __FUNCTION__);
384 return INVALID_OPERATION;
385 case STATE_IN_CONFIG:
386 case STATE_IN_RECONFIG:
387 case STATE_IN_IDLE:
388 // OK
389 break;
390 case STATE_CONSTRUCTED:
391 case STATE_CONFIGURED:
392 ALOGE("%s: Cannot cancel configuration that hasn't been started",
393 __FUNCTION__);
394 return INVALID_OPERATION;
395 default:
396 ALOGE("%s: Unknown state", __FUNCTION__);
397 return INVALID_OPERATION;
398 }
399
400 mUsage = mOldUsage;
401 camera3_stream::max_buffers = mOldMaxBuffers;
402
403 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
404 STATE_CONSTRUCTED;
405
406 return OK;
407 }
408
isUnpreparable()409 bool Camera3Stream::isUnpreparable() {
410 ATRACE_CALL();
411
412 Mutex::Autolock l(mLock);
413 return mStreamUnpreparable;
414 }
415
startPrepare(int maxCount,bool blockRequest)416 status_t Camera3Stream::startPrepare(int maxCount, bool blockRequest) {
417 ATRACE_CALL();
418
419 Mutex::Autolock l(mLock);
420
421 if (maxCount < 0) {
422 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
423 __FUNCTION__, mId, maxCount);
424 return BAD_VALUE;
425 }
426
427 // This function should be only called when the stream is configured already.
428 if (mState != STATE_CONFIGURED) {
429 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
430 "state %d", __FUNCTION__, mId, mState);
431 return INVALID_OPERATION;
432 }
433
434 // This function can't be called if the stream has already received filled
435 // buffers
436 if (mStreamUnpreparable) {
437 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
438 __FUNCTION__, mId);
439 return INVALID_OPERATION;
440 }
441
442 if (getHandoutOutputBufferCountLocked() > 0) {
443 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
444 __FUNCTION__, mId);
445 return INVALID_OPERATION;
446 }
447
448 size_t pipelineMax = getBufferCountLocked();
449 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
450 pipelineMax : static_cast<size_t>(maxCount);
451 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
452 pipelineMax : clampedCount;
453
454 mPrepared = bufferCount <= mLastMaxCount;
455 mPrepareBlockRequest = blockRequest;
456
457 if (mPrepared) return OK;
458
459 mLastMaxCount = bufferCount;
460
461 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
462 mPreparedBufferIdx = 0;
463
464 mState = STATE_PREPARING;
465
466 return NOT_ENOUGH_DATA;
467 }
468
isBlockedByPrepare() const469 bool Camera3Stream::isBlockedByPrepare() const {
470 Mutex::Autolock l(mLock);
471 return mState == STATE_PREPARING && mPrepareBlockRequest;
472 }
473
isAbandoned() const474 bool Camera3Stream::isAbandoned() const {
475 Mutex::Autolock l(mLock);
476 return mState == STATE_ABANDONED;
477 }
478
prepareNextBuffer()479 status_t Camera3Stream::prepareNextBuffer() {
480 ATRACE_CALL();
481
482 Mutex::Autolock l(mLock);
483 status_t res = OK;
484
485 // This function should be only called when the stream is preparing
486 if (mState != STATE_PREPARING) {
487 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
488 "state %d", __FUNCTION__, mId, mState);
489 return INVALID_OPERATION;
490 }
491
492 // Get next buffer - this may allocate, and take a while for large buffers
493 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
494 if (res != OK) {
495 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
496 __FUNCTION__, mId, mPreparedBufferIdx);
497 return NO_INIT;
498 }
499
500 mPreparedBufferIdx++;
501
502 // Check if we still have buffers left to allocate
503 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
504 return NOT_ENOUGH_DATA;
505 }
506
507 // Done with prepare - mark stream as such, and return all buffers
508 // via cancelPrepare
509 mPrepared = true;
510
511 return cancelPrepareLocked();
512 }
513
cancelPrepare()514 status_t Camera3Stream::cancelPrepare() {
515 ATRACE_CALL();
516
517 Mutex::Autolock l(mLock);
518
519 return cancelPrepareLocked();
520 }
521
cancelPrepareLocked()522 status_t Camera3Stream::cancelPrepareLocked() {
523 status_t res = OK;
524
525 // This function should be only called when the stream is mid-preparing.
526 if (mState != STATE_PREPARING) {
527 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
528 "PREPARING state %d", __FUNCTION__, mId, mState);
529 return INVALID_OPERATION;
530 }
531
532 // Return all valid buffers to stream, in ERROR state to indicate
533 // they weren't filled.
534 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
535 mPreparedBuffers.editItemAt(i).release_fence = -1;
536 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
537 returnBufferLocked(mPreparedBuffers[i], 0);
538 }
539 mPreparedBuffers.clear();
540 mPreparedBufferIdx = 0;
541
542 mState = STATE_CONFIGURED;
543
544 return res;
545 }
546
tearDown()547 status_t Camera3Stream::tearDown() {
548 ATRACE_CALL();
549 Mutex::Autolock l(mLock);
550
551 status_t res = OK;
552
553 // This function should be only called when the stream is configured.
554 if (mState != STATE_CONFIGURED) {
555 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
556 "CONFIGURED state %d", __FUNCTION__, mId, mState);
557 return INVALID_OPERATION;
558 }
559
560 // If any buffers have been handed to the HAL, the stream cannot be torn down.
561 if (getHandoutOutputBufferCountLocked() > 0) {
562 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
563 __FUNCTION__, mId);
564 return INVALID_OPERATION;
565 }
566
567 // Free buffers by disconnecting and then reconnecting to the buffer queue
568 // Only unused buffers will be dropped immediately; buffers that have been filled
569 // and are waiting to be acquired by the consumer and buffers that are currently
570 // acquired will be freed once they are released by the consumer.
571
572 res = disconnectLocked();
573 if (res != OK) {
574 if (res == -ENOTCONN) {
575 // queue has been disconnected, nothing left to do, so exit with success
576 return OK;
577 }
578 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
579 __FUNCTION__, mId, strerror(-res), res);
580 return res;
581 }
582
583 mState = STATE_IN_CONFIG;
584
585 res = configureQueueLocked();
586 if (res != OK) {
587 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
588 __FUNCTION__, mId, strerror(-res), res);
589 mState = STATE_ERROR;
590 return res;
591 }
592
593 // Reset prepared state, since we've reconnected to the queue and can prepare again.
594 mPrepared = false;
595 mStreamUnpreparable = false;
596
597 mState = STATE_CONFIGURED;
598
599 return OK;
600 }
601
getBuffer(camera3_stream_buffer * buffer,nsecs_t waitBufferTimeout,const std::vector<size_t> & surface_ids)602 status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
603 nsecs_t waitBufferTimeout,
604 const std::vector<size_t>& surface_ids) {
605 ATRACE_HFR_CALL();
606 Mutex::Autolock l(mLock);
607 status_t res = OK;
608
609 // This function should be only called when the stream is configured already.
610 if (mState != STATE_CONFIGURED) {
611 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
612 __FUNCTION__, mId, mState);
613 if (mState == STATE_ABANDONED) {
614 return DEAD_OBJECT;
615 } else {
616 return INVALID_OPERATION;
617 }
618 }
619
620 // Wait for new buffer returned back if we are running into the limit.
621 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
622 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
623 __FUNCTION__, camera3_stream::max_buffers);
624 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
625 if (waitBufferTimeout < kWaitForBufferDuration) {
626 waitBufferTimeout = kWaitForBufferDuration;
627 }
628 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
629 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
630 mBufferLimitLatency.add(waitStart, waitEnd);
631 if (res != OK) {
632 if (res == TIMED_OUT) {
633 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
634 __FUNCTION__, waitBufferTimeout / 1000000LL,
635 camera3_stream::max_buffers);
636 }
637 return res;
638 }
639 }
640
641 res = getBufferLocked(buffer, surface_ids);
642 if (res == OK) {
643 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
644 if (buffer->buffer) {
645 Mutex::Autolock l(mOutstandingBuffersLock);
646 mOutstandingBuffers.push_back(*buffer->buffer);
647 }
648 }
649
650 return res;
651 }
652
isOutstandingBuffer(const camera3_stream_buffer & buffer) const653 bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
654 if (buffer.buffer == nullptr) {
655 return false;
656 }
657
658 Mutex::Autolock l(mOutstandingBuffersLock);
659
660 for (auto b : mOutstandingBuffers) {
661 if (b == *buffer.buffer) {
662 return true;
663 }
664 }
665 return false;
666 }
667
removeOutstandingBuffer(const camera3_stream_buffer & buffer)668 void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
669 if (buffer.buffer == nullptr) {
670 return;
671 }
672
673 Mutex::Autolock l(mOutstandingBuffersLock);
674
675 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
676 if (*b == *buffer.buffer) {
677 mOutstandingBuffers.erase(b);
678 return;
679 }
680 }
681 }
682
returnBuffer(const camera3_stream_buffer & buffer,nsecs_t timestamp,bool timestampIncreasing,const std::vector<size_t> & surface_ids,uint64_t frameNumber)683 status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
684 nsecs_t timestamp, bool timestampIncreasing,
685 const std::vector<size_t>& surface_ids, uint64_t frameNumber) {
686 ATRACE_HFR_CALL();
687 Mutex::Autolock l(mLock);
688
689 // Check if this buffer is outstanding.
690 if (!isOutstandingBuffer(buffer)) {
691 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
692 return BAD_VALUE;
693 }
694
695 removeOutstandingBuffer(buffer);
696
697 // Buffer status may be changed, so make a copy of the stream_buffer struct.
698 camera3_stream_buffer b = buffer;
699 if (timestampIncreasing && timestamp != 0 && timestamp <= mLastTimestamp) {
700 ALOGE("%s: Stream %d: timestamp %" PRId64 " is not increasing. Prev timestamp %" PRId64,
701 __FUNCTION__, mId, timestamp, mLastTimestamp);
702 b.status = CAMERA3_BUFFER_STATUS_ERROR;
703 }
704 mLastTimestamp = timestamp;
705
706 /**
707 * TODO: Check that the state is valid first.
708 *
709 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
710 * >= HAL3.2 CONFIGURED only
711 *
712 * Do this for getBuffer as well.
713 */
714 status_t res = returnBufferLocked(b, timestamp, surface_ids);
715 if (res == OK) {
716 fireBufferListenersLocked(b, /*acquired*/false, /*output*/true, timestamp, frameNumber);
717 }
718
719 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
720 // buffer to be returned.
721 mOutputBufferReturnedSignal.signal();
722
723 return res;
724 }
725
getInputBuffer(camera3_stream_buffer * buffer,bool respectHalLimit)726 status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
727 ATRACE_CALL();
728 Mutex::Autolock l(mLock);
729 status_t res = OK;
730
731 // This function should be only called when the stream is configured already.
732 if (mState != STATE_CONFIGURED) {
733 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
734 __FUNCTION__, mId, mState);
735 return INVALID_OPERATION;
736 }
737
738 // Wait for new buffer returned back if we are running into the limit.
739 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
740 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
741 __FUNCTION__, camera3_stream::max_buffers);
742 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
743 if (res != OK) {
744 if (res == TIMED_OUT) {
745 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
746 kWaitForBufferDuration / 1000000LL);
747 }
748 return res;
749 }
750 }
751
752 res = getInputBufferLocked(buffer);
753 if (res == OK) {
754 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
755 if (buffer->buffer) {
756 Mutex::Autolock l(mOutstandingBuffersLock);
757 mOutstandingBuffers.push_back(*buffer->buffer);
758 }
759 }
760
761 return res;
762 }
763
returnInputBuffer(const camera3_stream_buffer & buffer)764 status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
765 ATRACE_CALL();
766 Mutex::Autolock l(mLock);
767
768 // Check if this buffer is outstanding.
769 if (!isOutstandingBuffer(buffer)) {
770 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
771 return BAD_VALUE;
772 }
773
774 removeOutstandingBuffer(buffer);
775
776 status_t res = returnInputBufferLocked(buffer);
777 if (res == OK) {
778 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
779 mInputBufferReturnedSignal.signal();
780 }
781
782 return res;
783 }
784
getInputBufferProducer(sp<IGraphicBufferProducer> * producer)785 status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
786 ATRACE_CALL();
787 Mutex::Autolock l(mLock);
788
789 return getInputBufferProducerLocked(producer);
790 }
791
fireBufferRequestForFrameNumber(uint64_t frameNumber,const CameraMetadata & settings)792 void Camera3Stream::fireBufferRequestForFrameNumber(uint64_t frameNumber,
793 const CameraMetadata& settings) {
794 ATRACE_CALL();
795 Mutex::Autolock l(mLock);
796
797 for (auto &it : mBufferListenerList) {
798 sp<Camera3StreamBufferListener> listener = it.promote();
799 if (listener.get() != nullptr) {
800 listener->onBufferRequestForFrameNumber(frameNumber, getId(), settings);
801 }
802 }
803 }
804
fireBufferListenersLocked(const camera3_stream_buffer & buffer,bool acquired,bool output,nsecs_t timestamp,uint64_t frameNumber)805 void Camera3Stream::fireBufferListenersLocked(
806 const camera3_stream_buffer& buffer, bool acquired, bool output, nsecs_t timestamp,
807 uint64_t frameNumber) {
808 List<wp<Camera3StreamBufferListener> >::iterator it, end;
809
810 // TODO: finish implementing
811
812 Camera3StreamBufferListener::BufferInfo info =
813 Camera3StreamBufferListener::BufferInfo();
814 info.mOutput = output;
815 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
816 info.mFrameNumber = frameNumber;
817 info.mTimestamp = timestamp;
818 info.mStreamId = getId();
819
820 // TODO: rest of fields
821
822 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
823 it != end;
824 ++it) {
825
826 sp<Camera3StreamBufferListener> listener = it->promote();
827 if (listener != 0) {
828 if (acquired) {
829 listener->onBufferAcquired(info);
830 } else {
831 listener->onBufferReleased(info);
832 }
833 }
834 }
835 }
836
hasOutstandingBuffers() const837 bool Camera3Stream::hasOutstandingBuffers() const {
838 ATRACE_CALL();
839 Mutex::Autolock l(mLock);
840 return hasOutstandingBuffersLocked();
841 }
842
getOutstandingBuffersCount() const843 size_t Camera3Stream::getOutstandingBuffersCount() const {
844 ATRACE_CALL();
845 Mutex::Autolock l(mLock);
846 return getHandoutOutputBufferCountLocked();
847 }
848
setStatusTracker(sp<StatusTracker> statusTracker)849 status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
850 Mutex::Autolock l(mLock);
851 sp<StatusTracker> oldTracker = mStatusTracker.promote();
852 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
853 oldTracker->removeComponent(mStatusId);
854 }
855 mStatusId = StatusTracker::NO_STATUS_ID;
856 mStatusTracker = statusTracker;
857
858 return OK;
859 }
860
disconnect()861 status_t Camera3Stream::disconnect() {
862 ATRACE_CALL();
863 Mutex::Autolock l(mLock);
864 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
865 status_t res = disconnectLocked();
866
867 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
868 mBufferLimitLatency.reset();
869
870 if (res == -ENOTCONN) {
871 // "Already disconnected" -- not an error
872 return OK;
873 } else {
874 return res;
875 }
876 }
877
dump(int fd,const Vector<String16> & args) const878 void Camera3Stream::dump(int fd, const Vector<String16> &args) const
879 {
880 (void)args;
881 mBufferLimitLatency.dump(fd,
882 " Latency histogram for wait on max_buffers");
883 }
884
getBufferLocked(camera3_stream_buffer *,const std::vector<size_t> &)885 status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
886 const std::vector<size_t>&) {
887 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
888 return INVALID_OPERATION;
889 }
returnBufferLocked(const camera3_stream_buffer &,nsecs_t,const std::vector<size_t> &)890 status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
891 nsecs_t, const std::vector<size_t>&) {
892 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
893 return INVALID_OPERATION;
894 }
getInputBufferLocked(camera3_stream_buffer *)895 status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
896 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
897 return INVALID_OPERATION;
898 }
returnInputBufferLocked(const camera3_stream_buffer &)899 status_t Camera3Stream::returnInputBufferLocked(
900 const camera3_stream_buffer &) {
901 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
902 return INVALID_OPERATION;
903 }
getInputBufferProducerLocked(sp<IGraphicBufferProducer> *)904 status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
905 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
906 return INVALID_OPERATION;
907 }
908
addBufferListener(wp<Camera3StreamBufferListener> listener)909 void Camera3Stream::addBufferListener(
910 wp<Camera3StreamBufferListener> listener) {
911 Mutex::Autolock l(mLock);
912
913 List<wp<Camera3StreamBufferListener> >::iterator it, end;
914 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
915 it != end;
916 ) {
917 if (*it == listener) {
918 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
919 return;
920 }
921 it++;
922 }
923
924 mBufferListenerList.push_back(listener);
925 }
926
removeBufferListener(const sp<Camera3StreamBufferListener> & listener)927 void Camera3Stream::removeBufferListener(
928 const sp<Camera3StreamBufferListener>& listener) {
929 Mutex::Autolock l(mLock);
930
931 bool erased = true;
932 List<wp<Camera3StreamBufferListener> >::iterator it, end;
933 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
934 it != end;
935 ) {
936
937 if (*it == listener) {
938 it = mBufferListenerList.erase(it);
939 erased = true;
940 } else {
941 ++it;
942 }
943 }
944
945 if (!erased) {
946 ALOGW("%s: Could not find listener to remove, already removed",
947 __FUNCTION__);
948 }
949 }
950
setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener)951 void Camera3Stream::setBufferFreedListener(
952 wp<Camera3StreamBufferFreedListener> listener) {
953 Mutex::Autolock l(mLock);
954 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
955 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
956 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
957 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
958 return;
959 }
960 mBufferFreedListener = listener;
961 }
962
963 }; // namespace camera3
964
965 }; // namespace android
966