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 #include <vector>
18 #include "system/window.h"
19 #define LOG_TAG "Camera3-Stream"
20 #define ATRACE_TAG ATRACE_TAG_CAMERA
21 //#define LOG_NDEBUG 0
22
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25 #include "device3/Camera3Stream.h"
26 #include "device3/StatusTracker.h"
27 #include "utils/TraceHFR.h"
28 #include "ui/GraphicBufferMapper.h"
29
30 #include <cutils/properties.h>
31
32 namespace android {
33
34 namespace camera3 {
35
~Camera3Stream()36 Camera3Stream::~Camera3Stream() {
37 sp<StatusTracker> statusTracker = mStatusTracker.promote();
38 if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
39 statusTracker->removeComponent(mStatusId);
40 }
41 }
42
cast(camera_stream * stream)43 Camera3Stream* Camera3Stream::cast(camera_stream *stream) {
44 return static_cast<Camera3Stream*>(stream);
45 }
46
cast(const camera_stream * stream)47 const Camera3Stream* Camera3Stream::cast(const camera_stream *stream) {
48 return static_cast<const Camera3Stream*>(stream);
49 }
50
Camera3Stream(int id,camera_stream_type type,uint32_t width,uint32_t height,size_t maxSize,int format,android_dataspace dataSpace,camera_stream_rotation_t rotation,const String8 & physicalCameraId,const std::unordered_set<int32_t> & sensorPixelModesUsed,int setId,bool isMultiResolution,int64_t dynamicRangeProfile,int64_t streamUseCase,bool deviceTimeBaseIsRealtime,int timestampBase)51 Camera3Stream::Camera3Stream(int id,
52 camera_stream_type type,
53 uint32_t width, uint32_t height, size_t maxSize, int format,
54 android_dataspace dataSpace, camera_stream_rotation_t rotation,
55 const String8& physicalCameraId,
56 const std::unordered_set<int32_t> &sensorPixelModesUsed,
57 int setId, bool isMultiResolution, int64_t dynamicRangeProfile,
58 int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase) :
59 camera_stream(),
60 mId(id),
61 mSetId(setId),
62 mName(String8::format("Camera3Stream[%d]", id)),
63 mMaxSize(maxSize),
64 mState(STATE_CONSTRUCTED),
65 mStatusId(StatusTracker::NO_STATUS_ID),
66 mStreamUnpreparable(true),
67 mUsage(0),
68 mOldUsage(0),
69 mOldMaxBuffers(0),
70 mOldFormat(-1),
71 mOldDataSpace(HAL_DATASPACE_UNKNOWN),
72 mPrepared(false),
73 mPrepareBlockRequest(true),
74 mPreparedBufferIdx(0),
75 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
76 mBufferLimitLatency(kBufferLimitLatencyBinSize),
77 mFormatOverridden(false),
78 mOriginalFormat(format),
79 mDataSpaceOverridden(false),
80 mOriginalDataSpace(dataSpace),
81 mPhysicalCameraId(physicalCameraId),
82 mLastTimestamp(0),
83 mIsMultiResolution(isMultiResolution),
84 mDeviceTimeBaseIsRealtime(deviceTimeBaseIsRealtime),
85 mTimestampBase(timestampBase) {
86
87 camera_stream::stream_type = type;
88 camera_stream::width = width;
89 camera_stream::height = height;
90 camera_stream::format = format;
91 camera_stream::data_space = dataSpace;
92 camera_stream::rotation = rotation;
93 camera_stream::max_buffers = 0;
94 camera_stream::physical_camera_id = mPhysicalCameraId.string();
95 camera_stream::sensor_pixel_modes_used = sensorPixelModesUsed;
96 camera_stream::dynamic_range_profile = dynamicRangeProfile;
97 camera_stream::use_case = streamUseCase;
98
99 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
100 maxSize == 0) {
101 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
102 mState = STATE_ERROR;
103 }
104 }
105
getId() const106 int Camera3Stream::getId() const {
107 return mId;
108 }
109
getStreamSetId() const110 int Camera3Stream::getStreamSetId() const {
111 return mSetId;
112 }
113
getHalStreamGroupId() const114 int Camera3Stream::getHalStreamGroupId() const {
115 return mIsMultiResolution ? mSetId : -1;
116 }
117
isMultiResolution() const118 bool Camera3Stream::isMultiResolution() const {
119 return mIsMultiResolution;
120 }
121
getWidth() const122 uint32_t Camera3Stream::getWidth() const {
123 return camera_stream::width;
124 }
125
getHeight() const126 uint32_t Camera3Stream::getHeight() const {
127 return camera_stream::height;
128 }
129
getFormat() const130 int Camera3Stream::getFormat() const {
131 return camera_stream::format;
132 }
133
getDataSpace() const134 android_dataspace Camera3Stream::getDataSpace() const {
135 return camera_stream::data_space;
136 }
137
getUsage() const138 uint64_t Camera3Stream::getUsage() const {
139 return mUsage;
140 }
141
setUsage(uint64_t usage)142 void Camera3Stream::setUsage(uint64_t usage) {
143 mUsage = usage;
144 }
145
setFormatOverride(bool formatOverridden)146 void Camera3Stream::setFormatOverride(bool formatOverridden) {
147 mFormatOverridden = formatOverridden;
148 }
149
isFormatOverridden() const150 bool Camera3Stream::isFormatOverridden() const {
151 return mFormatOverridden;
152 }
153
getOriginalFormat() const154 int Camera3Stream::getOriginalFormat() const {
155 return mOriginalFormat;
156 }
157
getDynamicRangeProfile() const158 int64_t Camera3Stream::getDynamicRangeProfile() const {
159 return camera_stream::dynamic_range_profile;
160 }
161
setDataSpaceOverride(bool dataSpaceOverridden)162 void Camera3Stream::setDataSpaceOverride(bool dataSpaceOverridden) {
163 mDataSpaceOverridden = dataSpaceOverridden;
164 }
165
isDataSpaceOverridden() const166 bool Camera3Stream::isDataSpaceOverridden() const {
167 return mDataSpaceOverridden;
168 }
169
getOriginalDataSpace() const170 android_dataspace Camera3Stream::getOriginalDataSpace() const {
171 return mOriginalDataSpace;
172 }
173
physicalCameraId() const174 const String8& Camera3Stream::physicalCameraId() const {
175 return mPhysicalCameraId;
176 }
177
getMaxHalBuffers() const178 int Camera3Stream::getMaxHalBuffers() const {
179 return camera_stream::max_buffers;
180 }
181
getStreamUseCase() const182 int64_t Camera3Stream::getStreamUseCase() const {
183 return camera_stream::use_case;
184 }
185
getTimestampBase() const186 int Camera3Stream::getTimestampBase() const {
187 return mTimestampBase;
188 }
189
isDeviceTimeBaseRealtime() const190 bool Camera3Stream::isDeviceTimeBaseRealtime() const {
191 return mDeviceTimeBaseIsRealtime;
192 }
193
setOfflineProcessingSupport(bool support)194 void Camera3Stream::setOfflineProcessingSupport(bool support) {
195 mSupportOfflineProcessing = support;
196 }
197
getOfflineProcessingSupport() const198 bool Camera3Stream::getOfflineProcessingSupport() const {
199 return mSupportOfflineProcessing;
200 }
201
forceToIdle()202 status_t Camera3Stream::forceToIdle() {
203 ATRACE_CALL();
204 Mutex::Autolock l(mLock);
205 status_t res;
206
207 switch (mState) {
208 case STATE_ERROR:
209 case STATE_CONSTRUCTED:
210 case STATE_IN_CONFIG:
211 case STATE_PREPARING:
212 case STATE_IN_RECONFIG:
213 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
214 res = NO_INIT;
215 break;
216 case STATE_CONFIGURED:
217 if (hasOutstandingBuffersLocked()) {
218 sp<StatusTracker> statusTracker = mStatusTracker.promote();
219 if (statusTracker != 0) {
220 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
221 }
222 }
223
224 mState = STATE_IN_IDLE;
225 res = OK;
226
227 break;
228 default:
229 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
230 res = NO_INIT;
231 }
232
233 return res;
234 }
235
restoreConfiguredState()236 status_t Camera3Stream::restoreConfiguredState() {
237 ATRACE_CALL();
238 Mutex::Autolock l(mLock);
239 status_t res;
240
241 switch (mState) {
242 case STATE_ERROR:
243 case STATE_CONSTRUCTED:
244 case STATE_IN_CONFIG:
245 case STATE_PREPARING:
246 case STATE_IN_RECONFIG:
247 case STATE_CONFIGURED:
248 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
249 res = NO_INIT;
250 break;
251 case STATE_IN_IDLE:
252 if (hasOutstandingBuffersLocked()) {
253 sp<StatusTracker> statusTracker = mStatusTracker.promote();
254 if (statusTracker != 0) {
255 statusTracker->markComponentActive(mStatusId);
256 }
257 }
258
259 mState = STATE_CONFIGURED;
260 res = OK;
261
262 break;
263 default:
264 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
265 res = NO_INIT;
266 }
267
268 return res;
269 }
270
startConfiguration()271 camera_stream* Camera3Stream::startConfiguration() {
272 ATRACE_CALL();
273 Mutex::Autolock l(mLock);
274 status_t res;
275
276 switch (mState) {
277 case STATE_ERROR:
278 ALOGE("%s: In error state", __FUNCTION__);
279 return NULL;
280 case STATE_CONSTRUCTED:
281 case STATE_IN_IDLE:
282 // OK
283 break;
284 case STATE_IN_CONFIG:
285 case STATE_IN_RECONFIG:
286 // Can start config again with no trouble; but don't redo
287 // mOldUsage/mOldMaxBuffers
288 return this;
289 case STATE_CONFIGURED:
290 if (hasOutstandingBuffersLocked()) {
291 ALOGE("%s: Cannot configure stream; has outstanding buffers",
292 __FUNCTION__);
293 return NULL;
294 }
295 break;
296 default:
297 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
298 return NULL;
299 }
300
301 mOldUsage = mUsage;
302 mOldMaxBuffers = camera_stream::max_buffers;
303 mOldFormat = camera_stream::format;
304 mOldDataSpace = camera_stream::data_space;
305
306 res = getEndpointUsage(&mUsage);
307 if (res != OK) {
308 ALOGE("%s: Cannot query consumer endpoint usage!",
309 __FUNCTION__);
310 return NULL;
311 }
312
313 if (mState == STATE_IN_IDLE) {
314 // Skip configuration.
315 return this;
316 }
317
318 // Stop tracking if currently doing so
319 if (mStatusId != StatusTracker::NO_STATUS_ID) {
320 sp<StatusTracker> statusTracker = mStatusTracker.promote();
321 if (statusTracker != 0) {
322 statusTracker->removeComponent(mStatusId);
323 }
324 mStatusId = StatusTracker::NO_STATUS_ID;
325 }
326
327 if (mState == STATE_CONSTRUCTED) {
328 mState = STATE_IN_CONFIG;
329 } else { // mState == STATE_CONFIGURED
330 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
331 mState = STATE_IN_RECONFIG;
332 }
333
334 return this;
335 }
336
isConfiguring() const337 bool Camera3Stream::isConfiguring() const {
338 Mutex::Autolock l(mLock);
339 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
340 }
341
finishConfiguration(bool * streamReconfigured)342 status_t Camera3Stream::finishConfiguration(/*out*/bool* streamReconfigured) {
343 ATRACE_CALL();
344 if (streamReconfigured != nullptr) {
345 *streamReconfigured = false;
346 }
347 Mutex::Autolock l(mLock);
348 switch (mState) {
349 case STATE_ERROR:
350 ALOGE("%s: In error state", __FUNCTION__);
351 return INVALID_OPERATION;
352 case STATE_IN_CONFIG:
353 case STATE_IN_RECONFIG:
354 // OK
355 break;
356 case STATE_CONSTRUCTED:
357 case STATE_CONFIGURED:
358 ALOGE("%s: Cannot finish configuration that hasn't been started",
359 __FUNCTION__);
360 return INVALID_OPERATION;
361 case STATE_IN_IDLE:
362 //Skip configuration in this state
363 return OK;
364 default:
365 ALOGE("%s: Unknown state", __FUNCTION__);
366 return INVALID_OPERATION;
367 }
368
369 // Register for idle tracking
370 sp<StatusTracker> statusTracker = mStatusTracker.promote();
371 if (statusTracker != 0 && mStatusId == StatusTracker::NO_STATUS_ID) {
372 std::string name = std::string("Stream ") + std::to_string(mId);
373 mStatusId = statusTracker->addComponent(name.c_str());
374 }
375
376 // Check if the stream configuration is unchanged, and skip reallocation if
377 // so.
378 if (mState == STATE_IN_RECONFIG &&
379 mOldUsage == mUsage &&
380 mOldMaxBuffers == camera_stream::max_buffers &&
381 mOldDataSpace == camera_stream::data_space &&
382 mOldFormat == camera_stream::format) {
383 mState = STATE_CONFIGURED;
384 return OK;
385 }
386
387 // Reset prepared state, since buffer config has changed, and existing
388 // allocations are no longer valid
389 mPrepared = false;
390 mPrepareBlockRequest = true;
391 mStreamUnpreparable = false;
392
393 bool reconfiguring = (mState == STATE_IN_RECONFIG);
394 status_t res;
395 res = configureQueueLocked();
396 // configureQueueLocked could return error in case of abandoned surface.
397 // Treat as non-fatal error.
398 if (res == NO_INIT || res == DEAD_OBJECT) {
399 ALOGE("%s: Unable to configure stream %d queue (non-fatal): %s (%d)",
400 __FUNCTION__, mId, strerror(-res), res);
401 mState = STATE_ABANDONED;
402 return res;
403 } else if (res != OK) {
404 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
405 __FUNCTION__, mId, strerror(-res), res);
406 mState = STATE_ERROR;
407 return res;
408 }
409
410 if (reconfiguring && streamReconfigured != nullptr) {
411 *streamReconfigured = true;
412 }
413 mState = STATE_CONFIGURED;
414
415 return res;
416 }
417
cancelConfiguration()418 status_t Camera3Stream::cancelConfiguration() {
419 ATRACE_CALL();
420 Mutex::Autolock l(mLock);
421 switch (mState) {
422 case STATE_ERROR:
423 ALOGE("%s: In error state", __FUNCTION__);
424 return INVALID_OPERATION;
425 case STATE_IN_CONFIG:
426 case STATE_IN_RECONFIG:
427 case STATE_IN_IDLE:
428 // OK
429 break;
430 case STATE_CONSTRUCTED:
431 case STATE_CONFIGURED:
432 ALOGE("%s: Cannot cancel configuration that hasn't been started",
433 __FUNCTION__);
434 return INVALID_OPERATION;
435 default:
436 ALOGE("%s: Unknown state", __FUNCTION__);
437 return INVALID_OPERATION;
438 }
439
440 mUsage = mOldUsage;
441 camera_stream::max_buffers = mOldMaxBuffers;
442
443 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
444 STATE_CONSTRUCTED;
445
446 return OK;
447 }
448
isUnpreparable()449 bool Camera3Stream::isUnpreparable() {
450 ATRACE_CALL();
451
452 Mutex::Autolock l(mLock);
453 return mStreamUnpreparable;
454 }
455
markUnpreparable()456 void Camera3Stream::markUnpreparable() {
457 ATRACE_CALL();
458
459 Mutex::Autolock l(mLock);
460 mStreamUnpreparable = true;
461 }
462
startPrepare(int maxCount,bool blockRequest)463 status_t Camera3Stream::startPrepare(int maxCount, bool blockRequest) {
464 ATRACE_CALL();
465
466 Mutex::Autolock l(mLock);
467
468 if (maxCount < 0) {
469 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
470 __FUNCTION__, mId, maxCount);
471 return BAD_VALUE;
472 }
473
474 // This function should be only called when the stream is configured already.
475 if (mState != STATE_CONFIGURED) {
476 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
477 "state %d", __FUNCTION__, mId, mState);
478 return INVALID_OPERATION;
479 }
480
481 // This function can't be called if the stream has already received filled
482 // buffers
483 if (mStreamUnpreparable) {
484 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
485 __FUNCTION__, mId);
486 return INVALID_OPERATION;
487 }
488
489 if (getHandoutOutputBufferCountLocked() > 0) {
490 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
491 __FUNCTION__, mId);
492 return INVALID_OPERATION;
493 }
494
495 size_t pipelineMax = getBufferCountLocked();
496 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
497 pipelineMax : static_cast<size_t>(maxCount);
498 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
499 pipelineMax : clampedCount;
500
501 mPrepared = bufferCount <= mLastMaxCount;
502 mPrepareBlockRequest = blockRequest;
503
504 if (mPrepared) return OK;
505
506 mLastMaxCount = bufferCount;
507
508 mPreparedBuffers.insertAt(camera_stream_buffer_t(), /*index*/0, bufferCount);
509 mPreparedBufferIdx = 0;
510
511 mState = STATE_PREPARING;
512
513 return NOT_ENOUGH_DATA;
514 }
515
isBlockedByPrepare() const516 bool Camera3Stream::isBlockedByPrepare() const {
517 Mutex::Autolock l(mLock);
518 return mState == STATE_PREPARING && mPrepareBlockRequest;
519 }
520
isAbandoned() const521 bool Camera3Stream::isAbandoned() const {
522 Mutex::Autolock l(mLock);
523 return mState == STATE_ABANDONED;
524 }
525
prepareNextBuffer()526 status_t Camera3Stream::prepareNextBuffer() {
527 ATRACE_CALL();
528
529 Mutex::Autolock l(mLock);
530 status_t res = OK;
531
532 // This function should be only called when the stream is preparing
533 if (mState != STATE_PREPARING) {
534 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
535 "state %d", __FUNCTION__, mId, mState);
536 return INVALID_OPERATION;
537 }
538
539 // Get next buffer - this may allocate, and take a while for large buffers
540 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
541 if (res != OK) {
542 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
543 __FUNCTION__, mId, mPreparedBufferIdx);
544 return NO_INIT;
545 }
546
547 mPreparedBufferIdx++;
548
549 // Check if we still have buffers left to allocate
550 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
551 return NOT_ENOUGH_DATA;
552 }
553
554 // Done with prepare - mark stream as such, and return all buffers
555 // via cancelPrepare
556 mPrepared = true;
557
558 return cancelPrepareLocked();
559 }
560
cancelPrepare()561 status_t Camera3Stream::cancelPrepare() {
562 ATRACE_CALL();
563
564 Mutex::Autolock l(mLock);
565
566 return cancelPrepareLocked();
567 }
568
cancelPrepareLocked()569 status_t Camera3Stream::cancelPrepareLocked() {
570 status_t res = OK;
571
572 // This function should be only called when the stream is mid-preparing.
573 if (mState != STATE_PREPARING) {
574 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
575 "PREPARING state %d", __FUNCTION__, mId, mState);
576 return INVALID_OPERATION;
577 }
578
579 // Return all valid buffers to stream, in ERROR state to indicate
580 // they weren't filled.
581 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
582 mPreparedBuffers.editItemAt(i).release_fence = -1;
583 mPreparedBuffers.editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
584 returnBufferLocked(mPreparedBuffers[i], /*timestamp*/0, /*readoutTimestamp*/0,
585 /*transform*/ -1);
586 }
587 mPreparedBuffers.clear();
588 mPreparedBufferIdx = 0;
589
590 mState = STATE_CONFIGURED;
591
592 return res;
593 }
594
tearDown()595 status_t Camera3Stream::tearDown() {
596 ATRACE_CALL();
597 Mutex::Autolock l(mLock);
598
599 status_t res = OK;
600
601 // This function should be only called when the stream is configured.
602 if (mState != STATE_CONFIGURED) {
603 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
604 "CONFIGURED state %d", __FUNCTION__, mId, mState);
605 return INVALID_OPERATION;
606 }
607
608 // If any buffers have been handed to the HAL, the stream cannot be torn down.
609 if (getHandoutOutputBufferCountLocked() > 0) {
610 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
611 __FUNCTION__, mId);
612 return INVALID_OPERATION;
613 }
614
615 // Free buffers by disconnecting and then reconnecting to the buffer queue
616 // Only unused buffers will be dropped immediately; buffers that have been filled
617 // and are waiting to be acquired by the consumer and buffers that are currently
618 // acquired will be freed once they are released by the consumer.
619
620 res = disconnectLocked();
621 if (res != OK) {
622 if (res == -ENOTCONN) {
623 // queue has been disconnected, nothing left to do, so exit with success
624 return OK;
625 }
626 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
627 __FUNCTION__, mId, strerror(-res), res);
628 return res;
629 }
630
631 mState = STATE_IN_CONFIG;
632
633 res = configureQueueLocked();
634 if (res != OK) {
635 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
636 __FUNCTION__, mId, strerror(-res), res);
637 mState = STATE_ERROR;
638 return res;
639 }
640
641 // Reset prepared state, since we've reconnected to the queue and can prepare again.
642 mPrepared = false;
643 mStreamUnpreparable = false;
644
645 mState = STATE_CONFIGURED;
646
647 return OK;
648 }
649
getBuffer(camera_stream_buffer * buffer,nsecs_t waitBufferTimeout,const std::vector<size_t> & surface_ids)650 status_t Camera3Stream::getBuffer(camera_stream_buffer *buffer,
651 nsecs_t waitBufferTimeout,
652 const std::vector<size_t>& surface_ids) {
653 ATRACE_HFR_CALL();
654 Mutex::Autolock l(mLock);
655 status_t res = OK;
656
657 // This function should be only called when the stream is configured already.
658 if (mState != STATE_CONFIGURED) {
659 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
660 __FUNCTION__, mId, mState);
661 if (mState == STATE_ABANDONED) {
662 return DEAD_OBJECT;
663 } else {
664 return INVALID_OPERATION;
665 }
666 }
667
668 // Wait for new buffer returned back if we are running into the limit. There
669 // are 2 limits:
670 // 1. The number of HAL buffers is greater than max_buffers
671 // 2. The number of HAL buffers + cached buffers is greater than max_buffers
672 // + maxCachedBuffers
673 size_t numOutstandingBuffers = getHandoutOutputBufferCountLocked();
674 size_t numCachedBuffers = getCachedOutputBufferCountLocked();
675 size_t maxNumCachedBuffers = getMaxCachedOutputBuffersLocked();
676 while (numOutstandingBuffers == camera_stream::max_buffers ||
677 numOutstandingBuffers + numCachedBuffers ==
678 camera_stream::max_buffers + maxNumCachedBuffers) {
679 ALOGV("%s: Already dequeued max output buffers (%d(+%zu)), wait for next returned one.",
680 __FUNCTION__, camera_stream::max_buffers, maxNumCachedBuffers);
681 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
682 if (waitBufferTimeout < kWaitForBufferDuration) {
683 waitBufferTimeout = kWaitForBufferDuration;
684 }
685 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
686 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
687 mBufferLimitLatency.add(waitStart, waitEnd);
688 if (res != OK) {
689 if (res == TIMED_OUT) {
690 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
691 __FUNCTION__, waitBufferTimeout / 1000000LL,
692 camera_stream::max_buffers);
693 }
694 return res;
695 }
696
697 size_t updatedNumOutstandingBuffers = getHandoutOutputBufferCountLocked();
698 size_t updatedNumCachedBuffers = getCachedOutputBufferCountLocked();
699 if (updatedNumOutstandingBuffers >= numOutstandingBuffers &&
700 updatedNumCachedBuffers == numCachedBuffers) {
701 ALOGE("%s: outstanding buffer count goes from %zu to %zu, "
702 "getBuffer(s) call must not run in parallel!", __FUNCTION__,
703 numOutstandingBuffers, updatedNumOutstandingBuffers);
704 return INVALID_OPERATION;
705 }
706 numOutstandingBuffers = updatedNumOutstandingBuffers;
707 numCachedBuffers = updatedNumCachedBuffers;
708 }
709
710 res = getBufferLocked(buffer, surface_ids);
711 if (res == OK) {
712 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
713 if (buffer->buffer) {
714 Mutex::Autolock l(mOutstandingBuffersLock);
715 mOutstandingBuffers.push_back(*buffer->buffer);
716 }
717 }
718
719 return res;
720 }
721
isOutstandingBuffer(const camera_stream_buffer & buffer) const722 bool Camera3Stream::isOutstandingBuffer(const camera_stream_buffer &buffer) const{
723 if (buffer.buffer == nullptr) {
724 return false;
725 }
726
727 Mutex::Autolock l(mOutstandingBuffersLock);
728
729 for (auto b : mOutstandingBuffers) {
730 if (b == *buffer.buffer) {
731 return true;
732 }
733 }
734 return false;
735 }
736
removeOutstandingBuffer(const camera_stream_buffer & buffer)737 void Camera3Stream::removeOutstandingBuffer(const camera_stream_buffer &buffer) {
738 if (buffer.buffer == nullptr) {
739 return;
740 }
741
742 Mutex::Autolock l(mOutstandingBuffersLock);
743
744 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
745 if (*b == *buffer.buffer) {
746 mOutstandingBuffers.erase(b);
747 return;
748 }
749 }
750 }
751
returnBuffer(const camera_stream_buffer & buffer,nsecs_t timestamp,nsecs_t readoutTimestamp,bool timestampIncreasing,const std::vector<size_t> & surface_ids,uint64_t frameNumber,int32_t transform)752 status_t Camera3Stream::returnBuffer(const camera_stream_buffer &buffer,
753 nsecs_t timestamp, nsecs_t readoutTimestamp, bool timestampIncreasing,
754 const std::vector<size_t>& surface_ids, uint64_t frameNumber, int32_t transform) {
755 ATRACE_HFR_CALL();
756 Mutex::Autolock l(mLock);
757
758 // Check if this buffer is outstanding.
759 if (!isOutstandingBuffer(buffer)) {
760 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
761 return BAD_VALUE;
762 }
763
764 removeOutstandingBuffer(buffer);
765
766 // Buffer status may be changed, so make a copy of the stream_buffer struct.
767 camera_stream_buffer b = buffer;
768 if (timestampIncreasing && timestamp != 0 && timestamp <= mLastTimestamp) {
769 ALOGE("%s: Stream %d: timestamp %" PRId64 " is not increasing. Prev timestamp %" PRId64,
770 __FUNCTION__, mId, timestamp, mLastTimestamp);
771 b.status = CAMERA_BUFFER_STATUS_ERROR;
772 }
773 mLastTimestamp = timestamp;
774
775 /**
776 * TODO: Check that the state is valid first.
777 *
778 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
779 * >= HAL3.2 CONFIGURED only
780 *
781 * Do this for getBuffer as well.
782 */
783 status_t res = returnBufferLocked(b, timestamp, readoutTimestamp, transform, surface_ids);
784 if (res == OK) {
785 fireBufferListenersLocked(b, /*acquired*/false, /*output*/true, timestamp, frameNumber);
786 }
787
788 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
789 // buffer to be returned.
790 mOutputBufferReturnedSignal.signal();
791
792 return res;
793 }
794
getInputBuffer(camera_stream_buffer * buffer,Size * size,bool respectHalLimit)795 status_t Camera3Stream::getInputBuffer(camera_stream_buffer *buffer,
796 Size* size, bool respectHalLimit) {
797 ATRACE_CALL();
798 Mutex::Autolock l(mLock);
799 status_t res = OK;
800
801 if (size == nullptr) {
802 ALOGE("%s: size must not be null", __FUNCTION__);
803 return BAD_VALUE;
804 }
805 // This function should be only called when the stream is configured already.
806 if (mState != STATE_CONFIGURED) {
807 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
808 __FUNCTION__, mId, mState);
809 return INVALID_OPERATION;
810 }
811
812 // Wait for new buffer returned back if we are running into the limit.
813 if (getHandoutInputBufferCountLocked() == camera_stream::max_buffers && respectHalLimit) {
814 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
815 __FUNCTION__, camera_stream::max_buffers);
816 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
817 if (res != OK) {
818 if (res == TIMED_OUT) {
819 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
820 kWaitForBufferDuration / 1000000LL);
821 }
822 return res;
823 }
824 }
825
826 res = getInputBufferLocked(buffer, size);
827 if (res == OK) {
828 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
829 if (buffer->buffer) {
830 Mutex::Autolock l(mOutstandingBuffersLock);
831 mOutstandingBuffers.push_back(*buffer->buffer);
832 }
833 }
834
835 return res;
836 }
837
returnInputBuffer(const camera_stream_buffer & buffer)838 status_t Camera3Stream::returnInputBuffer(const camera_stream_buffer &buffer) {
839 ATRACE_CALL();
840 Mutex::Autolock l(mLock);
841
842 // Check if this buffer is outstanding.
843 if (!isOutstandingBuffer(buffer)) {
844 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
845 return BAD_VALUE;
846 }
847
848 removeOutstandingBuffer(buffer);
849
850 status_t res = returnInputBufferLocked(buffer);
851 if (res == OK) {
852 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
853 mInputBufferReturnedSignal.signal();
854 }
855
856 return res;
857 }
858
getInputBufferProducer(sp<IGraphicBufferProducer> * producer)859 status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
860 ATRACE_CALL();
861 Mutex::Autolock l(mLock);
862
863 return getInputBufferProducerLocked(producer);
864 }
865
fireBufferRequestForFrameNumber(uint64_t frameNumber,const CameraMetadata & settings)866 void Camera3Stream::fireBufferRequestForFrameNumber(uint64_t frameNumber,
867 const CameraMetadata& settings) {
868 ATRACE_CALL();
869 Mutex::Autolock l(mLock);
870
871 for (auto &it : mBufferListenerList) {
872 sp<Camera3StreamBufferListener> listener = it.promote();
873 if (listener.get() != nullptr) {
874 listener->onBufferRequestForFrameNumber(frameNumber, getId(), settings);
875 }
876 }
877 }
878
fireBufferListenersLocked(const camera_stream_buffer & buffer,bool acquired,bool output,nsecs_t timestamp,uint64_t frameNumber)879 void Camera3Stream::fireBufferListenersLocked(
880 const camera_stream_buffer& buffer, bool acquired, bool output, nsecs_t timestamp,
881 uint64_t frameNumber) {
882 List<wp<Camera3StreamBufferListener> >::iterator it, end;
883
884 // TODO: finish implementing
885
886 Camera3StreamBufferListener::BufferInfo info =
887 Camera3StreamBufferListener::BufferInfo();
888 info.mOutput = output;
889 info.mError = (buffer.status == CAMERA_BUFFER_STATUS_ERROR);
890 info.mFrameNumber = frameNumber;
891 info.mTimestamp = timestamp;
892 info.mStreamId = getId();
893
894 // TODO: rest of fields
895
896 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
897 it != end;
898 ++it) {
899
900 sp<Camera3StreamBufferListener> listener = it->promote();
901 if (listener != 0) {
902 if (acquired) {
903 listener->onBufferAcquired(info);
904 } else {
905 listener->onBufferReleased(info);
906 }
907 }
908 }
909 }
910
hasOutstandingBuffers() const911 bool Camera3Stream::hasOutstandingBuffers() const {
912 ATRACE_CALL();
913 Mutex::Autolock l(mLock);
914 return hasOutstandingBuffersLocked();
915 }
916
getOutstandingBuffersCount() const917 size_t Camera3Stream::getOutstandingBuffersCount() const {
918 ATRACE_CALL();
919 Mutex::Autolock l(mLock);
920 return getHandoutOutputBufferCountLocked();
921 }
922
setStatusTracker(sp<StatusTracker> statusTracker)923 status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
924 Mutex::Autolock l(mLock);
925 sp<StatusTracker> oldTracker = mStatusTracker.promote();
926 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
927 oldTracker->removeComponent(mStatusId);
928 }
929 mStatusId = StatusTracker::NO_STATUS_ID;
930 mStatusTracker = statusTracker;
931
932 return OK;
933 }
934
disconnect()935 status_t Camera3Stream::disconnect() {
936 ATRACE_CALL();
937 Mutex::Autolock l(mLock);
938 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
939 status_t res = disconnectLocked();
940
941 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
942 mBufferLimitLatency.reset();
943
944 if (res == -ENOTCONN) {
945 // "Already disconnected" -- not an error
946 return OK;
947 } else {
948 return res;
949 }
950 }
951
dump(int fd,const Vector<String16> & args) const952 void Camera3Stream::dump(int fd, const Vector<String16> &args) const
953 {
954 (void)args;
955 mBufferLimitLatency.dump(fd,
956 " Latency histogram for wait on max_buffers");
957 }
958
getBufferLocked(camera_stream_buffer *,const std::vector<size_t> &)959 status_t Camera3Stream::getBufferLocked(camera_stream_buffer *,
960 const std::vector<size_t>&) {
961 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
962 return INVALID_OPERATION;
963 }
964
getBuffersLocked(std::vector<OutstandingBuffer> *)965 status_t Camera3Stream::getBuffersLocked(std::vector<OutstandingBuffer>*) {
966 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
967 return INVALID_OPERATION;
968 }
969
returnBufferLocked(const camera_stream_buffer &,nsecs_t,nsecs_t,int32_t,const std::vector<size_t> &)970 status_t Camera3Stream::returnBufferLocked(const camera_stream_buffer &,
971 nsecs_t, nsecs_t, int32_t, const std::vector<size_t>&) {
972 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
973 return INVALID_OPERATION;
974 }
getInputBufferLocked(camera_stream_buffer *,Size *)975 status_t Camera3Stream::getInputBufferLocked(camera_stream_buffer *, Size *) {
976 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
977 return INVALID_OPERATION;
978 }
returnInputBufferLocked(const camera_stream_buffer &)979 status_t Camera3Stream::returnInputBufferLocked(
980 const camera_stream_buffer &) {
981 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
982 return INVALID_OPERATION;
983 }
getInputBufferProducerLocked(sp<IGraphicBufferProducer> *)984 status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
985 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
986 return INVALID_OPERATION;
987 }
988
addBufferListener(wp<Camera3StreamBufferListener> listener)989 void Camera3Stream::addBufferListener(
990 wp<Camera3StreamBufferListener> listener) {
991 Mutex::Autolock l(mLock);
992
993 List<wp<Camera3StreamBufferListener> >::iterator it, end;
994 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
995 it != end;
996 ) {
997 if (*it == listener) {
998 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
999 return;
1000 }
1001 it++;
1002 }
1003
1004 mBufferListenerList.push_back(listener);
1005 }
1006
removeBufferListener(const sp<Camera3StreamBufferListener> & listener)1007 void Camera3Stream::removeBufferListener(
1008 const sp<Camera3StreamBufferListener>& listener) {
1009 Mutex::Autolock l(mLock);
1010
1011 bool erased = true;
1012 List<wp<Camera3StreamBufferListener> >::iterator it, end;
1013 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
1014 it != end;
1015 ) {
1016
1017 if (*it == listener) {
1018 it = mBufferListenerList.erase(it);
1019 erased = true;
1020 } else {
1021 ++it;
1022 }
1023 }
1024
1025 if (!erased) {
1026 ALOGW("%s: Could not find listener to remove, already removed",
1027 __FUNCTION__);
1028 }
1029 }
1030
setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener)1031 void Camera3Stream::setBufferFreedListener(
1032 wp<Camera3StreamBufferFreedListener> listener) {
1033 Mutex::Autolock l(mLock);
1034 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
1035 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
1036 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
1037 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
1038 return;
1039 }
1040 mBufferFreedListener = listener;
1041 }
1042
getBuffers(std::vector<OutstandingBuffer> * buffers,nsecs_t waitBufferTimeout)1043 status_t Camera3Stream::getBuffers(std::vector<OutstandingBuffer>* buffers,
1044 nsecs_t waitBufferTimeout) {
1045 ATRACE_CALL();
1046 Mutex::Autolock l(mLock);
1047 status_t res = OK;
1048
1049 if (buffers == nullptr) {
1050 ALOGI("%s: buffers must not be null!", __FUNCTION__);
1051 return BAD_VALUE;
1052 }
1053
1054 size_t numBuffersRequested = buffers->size();
1055 if (numBuffersRequested == 0) {
1056 ALOGE("%s: 0 buffers are requested!", __FUNCTION__);
1057 return BAD_VALUE;
1058 }
1059
1060 // This function should be only called when the stream is configured already.
1061 if (mState != STATE_CONFIGURED) {
1062 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
1063 __FUNCTION__, mId, mState);
1064 if (mState == STATE_ABANDONED) {
1065 return DEAD_OBJECT;
1066 } else {
1067 return INVALID_OPERATION;
1068 }
1069 }
1070
1071 size_t numOutstandingBuffers = getHandoutOutputBufferCountLocked();
1072 size_t numCachedBuffers = getCachedOutputBufferCountLocked();
1073 size_t maxNumCachedBuffers = getMaxCachedOutputBuffersLocked();
1074 // Wait for new buffer returned back if we are running into the limit. There
1075 // are 2 limits:
1076 // 1. The number of HAL buffers is greater than max_buffers
1077 // 2. The number of HAL buffers + cached buffers is greater than max_buffers
1078 // + maxCachedBuffers
1079 while (numOutstandingBuffers + numBuffersRequested > camera_stream::max_buffers ||
1080 numOutstandingBuffers + numCachedBuffers + numBuffersRequested >
1081 camera_stream::max_buffers + maxNumCachedBuffers) {
1082 ALOGV("%s: Already dequeued %zu(+%zu) output buffers and requesting %zu "
1083 "(max is %d(+%zu)), waiting.", __FUNCTION__, numOutstandingBuffers,
1084 numCachedBuffers, numBuffersRequested, camera_stream::max_buffers,
1085 maxNumCachedBuffers);
1086 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
1087 if (waitBufferTimeout < kWaitForBufferDuration) {
1088 waitBufferTimeout = kWaitForBufferDuration;
1089 }
1090 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
1091 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
1092 mBufferLimitLatency.add(waitStart, waitEnd);
1093 if (res != OK) {
1094 if (res == TIMED_OUT) {
1095 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
1096 __FUNCTION__, waitBufferTimeout / 1000000LL,
1097 camera_stream::max_buffers);
1098 }
1099 return res;
1100 }
1101 size_t updatedNumOutstandingBuffers = getHandoutOutputBufferCountLocked();
1102 size_t updatedNumCachedBuffers = getCachedOutputBufferCountLocked();
1103 if (updatedNumOutstandingBuffers >= numOutstandingBuffers &&
1104 updatedNumCachedBuffers == numCachedBuffers) {
1105 ALOGE("%s: outstanding buffer count goes from %zu to %zu, "
1106 "getBuffer(s) call must not run in parallel!", __FUNCTION__,
1107 numOutstandingBuffers, updatedNumOutstandingBuffers);
1108 return INVALID_OPERATION;
1109 }
1110 numOutstandingBuffers = updatedNumOutstandingBuffers;
1111 numCachedBuffers = updatedNumCachedBuffers;
1112 }
1113
1114 res = getBuffersLocked(buffers);
1115 if (res == OK) {
1116 for (auto& outstandingBuffer : *buffers) {
1117 camera_stream_buffer* buffer = outstandingBuffer.outBuffer;
1118 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
1119 if (buffer->buffer) {
1120 Mutex::Autolock l(mOutstandingBuffersLock);
1121 mOutstandingBuffers.push_back(*buffer->buffer);
1122 }
1123 }
1124 }
1125
1126 return res;
1127 }
1128
queueHDRMetadata(buffer_handle_t buffer,sp<ANativeWindow> & anw,int64_t dynamicRangeProfile)1129 void Camera3Stream::queueHDRMetadata(buffer_handle_t buffer, sp<ANativeWindow>& anw,
1130 int64_t dynamicRangeProfile) {
1131 auto& mapper = GraphicBufferMapper::get();
1132 switch (dynamicRangeProfile) {
1133 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10: {
1134 std::optional<ui::Smpte2086> smpte2086;
1135 auto res = mapper.getSmpte2086(buffer, &smpte2086);
1136 if ((res == OK) && smpte2086.has_value()) {
1137 const auto& metaValue = smpte2086.value();
1138 android_smpte2086_metadata meta = {
1139 .displayPrimaryRed.x = metaValue.primaryRed.x,
1140 .displayPrimaryRed.y = metaValue.primaryRed.y,
1141 .displayPrimaryGreen.x = metaValue.primaryGreen.x,
1142 .displayPrimaryGreen.y = metaValue.primaryGreen.y,
1143 .displayPrimaryBlue.x = metaValue.primaryBlue.x,
1144 .displayPrimaryBlue.y = metaValue.primaryBlue.y,
1145 .whitePoint.x = metaValue.whitePoint.x,
1146 .whitePoint.y = metaValue.whitePoint.y,
1147 .maxLuminance = metaValue.maxLuminance,
1148 .minLuminance = metaValue.minLuminance};
1149 native_window_set_buffers_smpte2086_metadata(anw.get(), &meta);
1150 } else {
1151 ALOGE("%s Couldn't retrieve Smpte2086 metadata %s (%d)",
1152 __FUNCTION__, strerror(-res), res);
1153 }
1154 break;
1155 }
1156 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10_PLUS: {
1157 std::optional<std::vector<uint8_t>> smpte2094_40;
1158 auto res = mapper.getSmpte2094_40(buffer, &smpte2094_40);
1159 if ((res == OK) && smpte2094_40.has_value()) {
1160 native_window_set_buffers_hdr10_plus_metadata(anw.get(),
1161 smpte2094_40.value().size(), smpte2094_40.value().data());
1162 } else {
1163 ALOGE("%s Couldn't retrieve Smpte2094_40 metadata %s (%d)",
1164 __FUNCTION__, strerror(-res), res);
1165 }
1166 break;
1167 }
1168 default:
1169 // No-op
1170 break;
1171 }
1172 }
1173
1174
1175 }; // namespace camera3
1176
1177 }; // namespace android
1178