Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1 | /* |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013-2018 The Android Open Source Project |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 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 | |
Shuzhen Wang | 34a5e28 | 2022-06-17 14:48:35 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 22 | #include <ctime> |
| 23 | #include <fstream> |
| 24 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 25 | #include <aidl/android/hardware/camera/device/CameraBlob.h> |
| 26 | #include <aidl/android/hardware/camera/device/CameraBlobId.h> |
Emilian Peev | e579d8b | 2023-02-28 14:16:08 -0800 | [diff] [blame] | 27 | #include "aidl/android/hardware/graphics/common/Dataspace.h" |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 28 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 30 | #include <com_android_internal_camera_flags.h> |
Shuzhen Wang | e4adddb | 2021-09-21 15:24:44 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 32 | #include <ui/GraphicBuffer.h> |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 33 | #include <utils/Log.h> |
| 34 | #include <utils/Trace.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 35 | #include <camera/StringUtils.h> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 36 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 37 | #include <common/CameraDeviceBase.h> |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 38 | #include "api1/client2/JpegProcessor.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 39 | #include "Camera3OutputStream.h" |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 40 | #include "utils/TraceHFR.h" |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 41 | |
| 42 | #ifndef container_of |
| 43 | #define container_of(ptr, type, member) \ |
| 44 | (type *)((char*)(ptr) - offsetof(type, member)) |
| 45 | #endif |
| 46 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 47 | namespace flags = com::android::internal::camera::flags; |
| 48 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | namespace camera3 { |
| 52 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 53 | using aidl::android::hardware::camera::device::CameraBlob; |
| 54 | using aidl::android::hardware::camera::device::CameraBlobId; |
| 55 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 56 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 57 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 58 | uint32_t width, uint32_t height, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 59 | android_dataspace dataSpace, camera_stream_rotation_t rotation, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 60 | nsecs_t timestampOffset, const std::string& physicalCameraId, |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 61 | const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport, |
Emilian Peev | c81a759 | 2022-02-14 17:38:18 -0800 | [diff] [blame] | 62 | int setId, bool isMultiResolution, int64_t dynamicRangeProfile, |
Shuzhen Wang | 8ed1e87 | 2022-03-08 16:34:33 -0800 | [diff] [blame] | 63 | int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase, |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 64 | int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 65 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 66 | /*maxSize*/0, format, dataSpace, rotation, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 67 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution, |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 68 | dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, |
Austin Borger | 9e2b27c | 2022-07-15 11:27:24 -0700 | [diff] [blame] | 69 | timestampBase, colorSpace), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 70 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 71 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 72 | mTraceFirstBuffer(true), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 73 | mUseBufferManager(false), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 74 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 75 | mUseReadoutTime(useReadoutTimestamp), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 76 | mConsumerUsage(0), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 77 | mDropBuffers(false), |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 78 | mMirrorMode(mirrorMode), |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 79 | mDequeueBufferLatency(kDequeueLatencyBinSize), |
| 80 | mIPCTransport(transport) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 81 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 82 | if (mConsumer == NULL) { |
| 83 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 84 | mState = STATE_ERROR; |
| 85 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 86 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 87 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 88 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Camera3OutputStream::Camera3OutputStream(int id, |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 92 | sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 93 | uint32_t width, uint32_t height, size_t maxSize, int format, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 94 | android_dataspace dataSpace, camera_stream_rotation_t rotation, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 95 | nsecs_t timestampOffset, const std::string& physicalCameraId, |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 96 | const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport, |
Emilian Peev | c81a759 | 2022-02-14 17:38:18 -0800 | [diff] [blame] | 97 | int setId, bool isMultiResolution, int64_t dynamicRangeProfile, |
Shuzhen Wang | 8ed1e87 | 2022-03-08 16:34:33 -0800 | [diff] [blame] | 98 | int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase, |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 99 | int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 100 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, maxSize, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 101 | format, dataSpace, rotation, physicalCameraId, sensorPixelModesUsed, |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 102 | setId, isMultiResolution, dynamicRangeProfile, streamUseCase, |
Austin Borger | 9e2b27c | 2022-07-15 11:27:24 -0700 | [diff] [blame] | 103 | deviceTimeBaseIsRealtime, timestampBase, colorSpace), |
Igor Murashkin | a55b545 | 2013-04-02 16:36:33 -0700 | [diff] [blame] | 104 | mConsumer(consumer), |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 105 | mTransform(0), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 106 | mTraceFirstBuffer(true), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 107 | mUseBufferManager(false), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 108 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 109 | mUseReadoutTime(useReadoutTimestamp), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 110 | mConsumerUsage(0), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 111 | mDropBuffers(false), |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 112 | mMirrorMode(mirrorMode), |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 113 | mDequeueBufferLatency(kDequeueLatencyBinSize), |
| 114 | mIPCTransport(transport) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 115 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 116 | if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 117 | ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, |
| 118 | format); |
| 119 | mState = STATE_ERROR; |
| 120 | } |
| 121 | |
| 122 | if (mConsumer == NULL) { |
| 123 | ALOGE("%s: Consumer is NULL!", __FUNCTION__); |
| 124 | mState = STATE_ERROR; |
| 125 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 126 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 127 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 128 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 131 | Camera3OutputStream::Camera3OutputStream(int id, |
| 132 | uint32_t width, uint32_t height, int format, |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 133 | uint64_t consumerUsage, android_dataspace dataSpace, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 134 | camera_stream_rotation_t rotation, nsecs_t timestampOffset, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 135 | const std::string& physicalCameraId, |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 136 | const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport, |
Emilian Peev | c81a759 | 2022-02-14 17:38:18 -0800 | [diff] [blame] | 137 | int setId, bool isMultiResolution, int64_t dynamicRangeProfile, |
Shuzhen Wang | 8ed1e87 | 2022-03-08 16:34:33 -0800 | [diff] [blame] | 138 | int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase, |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 139 | int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) : |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 140 | Camera3IOStreamBase(id, CAMERA_STREAM_OUTPUT, width, height, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 141 | /*maxSize*/0, format, dataSpace, rotation, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 142 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution, |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 143 | dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, |
Austin Borger | 9e2b27c | 2022-07-15 11:27:24 -0700 | [diff] [blame] | 144 | timestampBase, colorSpace), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 145 | mConsumer(nullptr), |
| 146 | mTransform(0), |
| 147 | mTraceFirstBuffer(true), |
| 148 | mUseBufferManager(false), |
| 149 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 150 | mUseReadoutTime(useReadoutTimestamp), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 151 | mConsumerUsage(consumerUsage), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 152 | mDropBuffers(false), |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 153 | mMirrorMode(mirrorMode), |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 154 | mDequeueBufferLatency(kDequeueLatencyBinSize), |
| 155 | mIPCTransport(transport) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 156 | // Deferred consumer only support preview surface format now. |
| 157 | if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 158 | ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!", |
| 159 | __FUNCTION__); |
| 160 | mState = STATE_ERROR; |
| 161 | } |
| 162 | |
Ivan Lozano | c0ad82f | 2020-07-30 09:32:57 -0400 | [diff] [blame] | 163 | // Validation check for the consumer usage flag. |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 164 | if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 && |
| 165 | (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 166 | ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!", |
| 167 | __FUNCTION__, consumerUsage); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 168 | mState = STATE_ERROR; |
| 169 | } |
| 170 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 171 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 172 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 175 | Camera3OutputStream::Camera3OutputStream(int id, camera_stream_type_t type, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 176 | uint32_t width, uint32_t height, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 177 | int format, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 178 | android_dataspace dataSpace, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 179 | camera_stream_rotation_t rotation, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 180 | const std::string& physicalCameraId, |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 181 | const std::unordered_set<int32_t> &sensorPixelModesUsed, |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 182 | IPCTransport transport, |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 183 | uint64_t consumerUsage, nsecs_t timestampOffset, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 184 | int setId, bool isMultiResolution, |
Shuzhen Wang | 8ed1e87 | 2022-03-08 16:34:33 -0800 | [diff] [blame] | 185 | int64_t dynamicRangeProfile, int64_t streamUseCase, |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 186 | bool deviceTimeBaseIsRealtime, int timestampBase, |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 187 | int mirrorMode, int32_t colorSpace, |
| 188 | bool useReadoutTimestamp) : |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 189 | Camera3IOStreamBase(id, type, width, height, |
| 190 | /*maxSize*/0, |
Shuzhen Wang | c28189a | 2017-11-27 23:05:10 -0800 | [diff] [blame] | 191 | format, dataSpace, rotation, |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 192 | physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution, |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 193 | dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, |
Austin Borger | 9e2b27c | 2022-07-15 11:27:24 -0700 | [diff] [blame] | 194 | timestampBase, colorSpace), |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 195 | mTransform(0), |
| 196 | mTraceFirstBuffer(true), |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 197 | mUseBufferManager(false), |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 198 | mTimestampOffset(timestampOffset), |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 199 | mUseReadoutTime(useReadoutTimestamp), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 200 | mConsumerUsage(consumerUsage), |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 201 | mDropBuffers(false), |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 202 | mMirrorMode(mirrorMode), |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 203 | mDequeueBufferLatency(kDequeueLatencyBinSize), |
| 204 | mIPCTransport(transport) { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 205 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 206 | bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID; |
| 207 | mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 208 | |
| 209 | // Subclasses expected to initialize mConsumer themselves |
| 210 | } |
| 211 | |
| 212 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 213 | Camera3OutputStream::~Camera3OutputStream() { |
| 214 | disconnectLocked(); |
| 215 | } |
| 216 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 217 | status_t Camera3OutputStream::getBufferLocked(camera_stream_buffer *buffer, |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 218 | const std::vector<size_t>&) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 219 | ATRACE_HFR_CALL(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 220 | |
| 221 | ANativeWindowBuffer* anb; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 222 | int fenceFd = -1; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 223 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 224 | status_t res; |
| 225 | res = getBufferLockedCommon(&anb, &fenceFd); |
| 226 | if (res != OK) { |
| 227 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 230 | /** |
| 231 | * FenceFD now owned by HAL except in case of error, |
| 232 | * in which case we reassign it to acquire_fence |
| 233 | */ |
| 234 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 235 | /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 236 | |
| 237 | return OK; |
| 238 | } |
| 239 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 240 | status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 241 | ANativeWindowBuffer* buffer, int anwReleaseFence, |
| 242 | const std::vector<size_t>&) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 243 | return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence); |
| 244 | } |
| 245 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 246 | status_t Camera3OutputStream::returnBufferLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 247 | const camera_stream_buffer &buffer, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 248 | nsecs_t timestamp, nsecs_t readoutTimestamp, |
| 249 | int32_t transform, const std::vector<size_t>& surface_ids) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 250 | ATRACE_HFR_CALL(); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 251 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 252 | if (mHandoutTotalBufferCount == 1) { |
| 253 | returnPrefetchedBuffersLocked(); |
| 254 | } |
| 255 | |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 256 | status_t res = returnAnyBufferLocked(buffer, timestamp, readoutTimestamp, |
| 257 | /*output*/true, transform, surface_ids); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 258 | |
| 259 | if (res != OK) { |
| 260 | return res; |
| 261 | } |
| 262 | |
| 263 | mLastTimestamp = timestamp; |
Eino-Ville Talvala | c31dc7e | 2017-01-31 17:35:41 -0800 | [diff] [blame] | 264 | mFrameCount++; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 265 | |
| 266 | return OK; |
| 267 | } |
| 268 | |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 269 | status_t Camera3OutputStream::fixUpHidlJpegBlobHeader(ANativeWindowBuffer* anwBuffer, int fence) { |
| 270 | // Lock the JPEG buffer for CPU read |
| 271 | sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(anwBuffer); |
| 272 | void* mapped = nullptr; |
| 273 | base::unique_fd fenceFd(dup(fence)); |
| 274 | // Use USAGE_SW_WRITE_RARELY since we're going to re-write the CameraBlob |
| 275 | // header. |
| 276 | GraphicBufferLocker gbLocker(graphicBuffer); |
| 277 | status_t res = |
| 278 | gbLocker.lockAsync( |
| 279 | GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_RARELY, |
Emilian Peev | 293bd97 | 2022-08-05 17:28:06 -0700 | [diff] [blame] | 280 | &mapped, fenceFd.release()); |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 281 | if (res != OK) { |
| 282 | ALOGE("%s: Failed to lock the buffer: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 283 | return res; |
| 284 | } |
| 285 | |
| 286 | uint8_t *hidlHeaderStart = |
| 287 | static_cast<uint8_t*>(mapped) + graphicBuffer->getWidth() - sizeof(camera_jpeg_blob_t); |
| 288 | // Check that the jpeg buffer is big enough to contain HIDL camera blob |
| 289 | if (hidlHeaderStart < static_cast<uint8_t *>(mapped)) { |
| 290 | ALOGE("%s, jpeg buffer not large enough to fit HIDL camera blob %" PRIu32, __FUNCTION__, |
| 291 | graphicBuffer->getWidth()); |
| 292 | return BAD_VALUE; |
| 293 | } |
| 294 | camera_jpeg_blob_t *hidlBlobHeader = reinterpret_cast<camera_jpeg_blob_t *>(hidlHeaderStart); |
| 295 | |
| 296 | // Check that the blob is indeed the jpeg blob id. |
| 297 | if (hidlBlobHeader->jpeg_blob_id != CAMERA_JPEG_BLOB_ID) { |
| 298 | ALOGE("%s, jpeg blob id %d is not correct", __FUNCTION__, hidlBlobHeader->jpeg_blob_id); |
| 299 | return BAD_VALUE; |
| 300 | } |
| 301 | |
| 302 | // Retrieve id and blob size |
| 303 | CameraBlobId blobId = static_cast<CameraBlobId>(hidlBlobHeader->jpeg_blob_id); |
| 304 | uint32_t blobSizeBytes = hidlBlobHeader->jpeg_size; |
| 305 | |
| 306 | if (blobSizeBytes > (graphicBuffer->getWidth() - sizeof(camera_jpeg_blob_t))) { |
| 307 | ALOGE("%s, blobSize in HIDL jpeg blob : %d is corrupt, buffer size %" PRIu32, __FUNCTION__, |
| 308 | blobSizeBytes, graphicBuffer->getWidth()); |
| 309 | } |
| 310 | |
| 311 | uint8_t *aidlHeaderStart = |
| 312 | static_cast<uint8_t*>(mapped) + graphicBuffer->getWidth() - sizeof(CameraBlob); |
| 313 | |
| 314 | // Check that the jpeg buffer is big enough to contain AIDL camera blob |
| 315 | if (aidlHeaderStart < static_cast<uint8_t *>(mapped)) { |
| 316 | ALOGE("%s, jpeg buffer not large enough to fit AIDL camera blob %" PRIu32, __FUNCTION__, |
| 317 | graphicBuffer->getWidth()); |
| 318 | return BAD_VALUE; |
| 319 | } |
| 320 | |
| 321 | if (static_cast<uint8_t*>(mapped) + blobSizeBytes > aidlHeaderStart) { |
| 322 | ALOGE("%s, jpeg blob with size %d , buffer size %" PRIu32 " not large enough to fit" |
| 323 | " AIDL camera blob without corrupting jpeg", __FUNCTION__, blobSizeBytes, |
| 324 | graphicBuffer->getWidth()); |
| 325 | return BAD_VALUE; |
| 326 | } |
| 327 | |
| 328 | // Fill in JPEG header |
Avichal Rakesh | 51af070 | 2022-05-26 22:58:03 +0000 | [diff] [blame] | 329 | CameraBlob aidlHeader = { |
| 330 | .blobId = blobId, |
| 331 | .blobSizeBytes = static_cast<int32_t>(blobSizeBytes) |
| 332 | }; |
| 333 | memcpy(aidlHeaderStart, &aidlHeader, sizeof(CameraBlob)); |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 334 | graphicBuffer->unlock(); |
| 335 | return OK; |
| 336 | } |
| 337 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 338 | status_t Camera3OutputStream::returnBufferCheckedLocked( |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 339 | const camera_stream_buffer &buffer, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 340 | nsecs_t timestamp, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 341 | nsecs_t readoutTimestamp, |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 342 | [[maybe_unused]] bool output, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 343 | int32_t transform, |
Yin-Chia Yeh | 58b1b4e | 2018-10-15 12:18:36 -0700 | [diff] [blame] | 344 | const std::vector<size_t>& surface_ids, |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 345 | /*out*/ |
| 346 | sp<Fence> *releaseFenceOut) { |
| 347 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 348 | ALOG_ASSERT(output, "Expected output to be true"); |
| 349 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 350 | status_t res; |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 351 | |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 352 | // Fence management - always honor release fence from HAL |
| 353 | sp<Fence> releaseFence = new Fence(buffer.release_fence); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 354 | int anwReleaseFence = releaseFence->dup(); |
| 355 | |
| 356 | /** |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 357 | * Release the lock briefly to avoid deadlock with |
| 358 | * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this |
| 359 | * thread will go into StreamingProcessor::onFrameAvailable) during |
| 360 | * queueBuffer |
| 361 | */ |
| 362 | sp<ANativeWindow> currentConsumer = mConsumer; |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 363 | StreamState state = mState; |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 364 | mLock.unlock(); |
| 365 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 366 | ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle); |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 367 | bool bufferDeferred = false; |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 368 | /** |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 369 | * Return buffer back to ANativeWindow |
| 370 | */ |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 371 | if (buffer.status == CAMERA_BUFFER_STATUS_ERROR || mDropBuffers || timestamp == 0) { |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 372 | // Cancel buffer |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 373 | if (mDropBuffers) { |
| 374 | ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId); |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 375 | } else if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 376 | ALOGV("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId); |
Shuzhen Wang | f0c4a6b | 2018-09-05 09:36:14 -0700 | [diff] [blame] | 377 | } else { |
| 378 | ALOGE("%s: Stream %d: timestamp shouldn't be 0", __FUNCTION__, mId); |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 381 | res = currentConsumer->cancelBuffer(currentConsumer.get(), |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 382 | anwBuffer, |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 383 | anwReleaseFence); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 384 | if (shouldLogError(res, state)) { |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 385 | ALOGE("%s: Stream %d: Error cancelling buffer to native window:" |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 386 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 387 | } |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 388 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 389 | notifyBufferReleased(anwBuffer); |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 390 | if (mUseBufferManager) { |
| 391 | // Return this buffer back to buffer manager. |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 392 | mBufferProducerListener->onBufferReleased(); |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 393 | } |
Igor Murashkin | 5a1798a | 2013-05-07 10:58:13 -0700 | [diff] [blame] | 394 | } else { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 395 | if (mTraceFirstBuffer && (stream_type == CAMERA_STREAM_OUTPUT)) { |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 396 | { |
| 397 | char traceLog[48]; |
| 398 | snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId); |
| 399 | ATRACE_NAME(traceLog); |
| 400 | } |
| 401 | mTraceFirstBuffer = false; |
| 402 | } |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 403 | // Fix CameraBlob id type discrepancy between HIDL and AIDL, details : http://b/229688810 |
Emilian Peev | e579d8b | 2023-02-28 14:16:08 -0800 | [diff] [blame] | 404 | if (getFormat() == HAL_PIXEL_FORMAT_BLOB && (getDataSpace() == HAL_DATASPACE_V0_JFIF || |
| 405 | (getDataSpace() == |
| 406 | static_cast<android_dataspace_t>( |
| 407 | aidl::android::hardware::graphics::common::Dataspace::JPEG_R)))) { |
Jayant Chowdhary | c67af1b | 2022-04-07 18:05:04 +0000 | [diff] [blame] | 408 | if (mIPCTransport == IPCTransport::HIDL) { |
| 409 | fixUpHidlJpegBlobHeader(anwBuffer, anwReleaseFence); |
| 410 | } |
| 411 | // If this is a JPEG output, and image dump mask is set, save image to |
| 412 | // disk. |
| 413 | if (mImageDumpMask) { |
| 414 | dumpImageToDisk(timestamp, anwBuffer, anwReleaseFence); |
| 415 | } |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 416 | } |
Yin-Chia Yeh | 4c9736f | 2015-03-05 15:01:36 -0800 | [diff] [blame] | 417 | |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 418 | nsecs_t captureTime = ((mUseReadoutTime || mSyncToDisplay) && readoutTimestamp != 0 ? |
Shuzhen Wang | ffc4c01 | 2022-04-20 15:55:46 -0700 | [diff] [blame] | 419 | readoutTimestamp : timestamp) - mTimestampOffset; |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 420 | if (mPreviewFrameSpacer != nullptr) { |
Shuzhen Wang | fe8a2a3 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 421 | nsecs_t readoutTime = (readoutTimestamp != 0 ? readoutTimestamp : timestamp) |
| 422 | - mTimestampOffset; |
| 423 | res = mPreviewFrameSpacer->queuePreviewBuffer(captureTime, readoutTime, |
| 424 | transform, anwBuffer, anwReleaseFence); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 425 | if (res != OK) { |
| 426 | ALOGE("%s: Stream %d: Error queuing buffer to preview buffer spacer: %s (%d)", |
| 427 | __FUNCTION__, mId, strerror(-res), res); |
| 428 | return res; |
| 429 | } |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 430 | bufferDeferred = true; |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 431 | } else { |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 432 | nsecs_t presentTime = mSyncToDisplay ? |
Ravneet | 446b3bf | 2023-07-11 19:26:38 +0000 | [diff] [blame] | 433 | syncTimestampToDisplayLocked(captureTime, releaseFence) : captureTime; |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 434 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 435 | setTransform(transform, true/*mayChangeMirror*/); |
| 436 | res = native_window_set_buffers_timestamp(mConsumer.get(), presentTime); |
| 437 | if (res != OK) { |
| 438 | ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)", |
| 439 | __FUNCTION__, mId, strerror(-res), res); |
| 440 | return res; |
| 441 | } |
Emilian Peev | 2295df7 | 2021-11-12 18:14:10 -0800 | [diff] [blame] | 442 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 443 | queueHDRMetadata(anwBuffer->handle, currentConsumer, dynamic_range_profile); |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 444 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 445 | res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence, surface_ids); |
| 446 | if (shouldLogError(res, state)) { |
| 447 | ALOGE("%s: Stream %d: Error queueing buffer to native window:" |
| 448 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 449 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 450 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 451 | } |
Zhijun He | 124ccf4 | 2013-05-22 14:01:30 -0700 | [diff] [blame] | 452 | mLock.lock(); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 453 | |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 454 | if (bufferDeferred) { |
| 455 | mCachedOutputBufferCount++; |
| 456 | } |
| 457 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 458 | // Once a valid buffer has been returned to the queue, can no longer |
| 459 | // dequeue all buffers for preallocation. |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 460 | if (buffer.status != CAMERA_BUFFER_STATUS_ERROR) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 461 | mStreamUnpreparable = true; |
| 462 | } |
| 463 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 464 | *releaseFenceOut = releaseFence; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 465 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 466 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 469 | void Camera3OutputStream::dump(int fd, [[maybe_unused]] const Vector<String16> &args) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 470 | std::string lines; |
| 471 | lines += fmt::sprintf(" Stream[%d]: Output\n", mId); |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 472 | lines += fmt::sprintf(" Consumer name: %s\n", (mConsumer.get() != nullptr) ? |
| 473 | mConsumer->getConsumerName() : "Deferred"); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 474 | write(fd, lines.c_str(), lines.size()); |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 475 | |
| 476 | Camera3IOStreamBase::dump(fd, args); |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 477 | |
| 478 | mDequeueBufferLatency.dump(fd, |
| 479 | " DequeueBuffer latency histogram:"); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 482 | status_t Camera3OutputStream::setTransform(int transform, bool mayChangeMirror) { |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 483 | ATRACE_CALL(); |
| 484 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 610d7b8 | 2022-02-08 14:37:22 -0800 | [diff] [blame] | 485 | if (mMirrorMode != OutputConfiguration::MIRROR_MODE_AUTO && mayChangeMirror) { |
| 486 | // If the mirroring mode is not AUTO, do not allow transform update |
| 487 | // which may change mirror. |
| 488 | return OK; |
| 489 | } |
| 490 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 491 | return setTransformLocked(transform); |
| 492 | } |
| 493 | |
| 494 | status_t Camera3OutputStream::setTransformLocked(int transform) { |
| 495 | status_t res = OK; |
Shuzhen Wang | e4adddb | 2021-09-21 15:24:44 -0700 | [diff] [blame] | 496 | |
| 497 | if (transform == -1) return res; |
| 498 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 499 | if (mState == STATE_ERROR) { |
| 500 | ALOGE("%s: Stream in error state", __FUNCTION__); |
| 501 | return INVALID_OPERATION; |
| 502 | } |
| 503 | |
| 504 | mTransform = transform; |
| 505 | if (mState == STATE_CONFIGURED) { |
| 506 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 507 | transform); |
| 508 | if (res != OK) { |
| 509 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 510 | __FUNCTION__, transform, strerror(-res), res); |
| 511 | } |
| 512 | } |
| 513 | return res; |
| 514 | } |
| 515 | |
| 516 | status_t Camera3OutputStream::configureQueueLocked() { |
| 517 | status_t res; |
| 518 | |
Ruchit Sharma | e0711f2 | 2014-08-18 13:48:24 -0400 | [diff] [blame] | 519 | mTraceFirstBuffer = true; |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 520 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 521 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 524 | if ((res = configureConsumerQueueLocked(true /*allowPreviewRespace*/)) != OK) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 525 | return res; |
| 526 | } |
| 527 | |
| 528 | // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture. |
| 529 | // We need skip these cases as timeout will disable the non-blocking (async) mode. |
| 530 | if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 531 | if (mUseBufferManager) { |
| 532 | // When buffer manager is handling the buffer, we should have available buffers in |
| 533 | // buffer queue before we calls into dequeueBuffer because buffer manager is tracking |
| 534 | // free buffers. |
| 535 | // There are however some consumer side feature (ImageReader::discardFreeBuffers) that |
| 536 | // can discard free buffers without notifying buffer manager. We want the timeout to |
| 537 | // happen immediately here so buffer manager can try to update its internal state and |
| 538 | // try to allocate a buffer instead of waiting. |
| 539 | mConsumer->setDequeueTimeout(0); |
| 540 | } else { |
| 541 | mConsumer->setDequeueTimeout(kDequeueBufferTimeout); |
| 542 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return OK; |
| 546 | } |
| 547 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 548 | status_t Camera3OutputStream::configureConsumerQueueLocked(bool allowPreviewRespace) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 549 | status_t res; |
| 550 | |
| 551 | mTraceFirstBuffer = true; |
| 552 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 553 | ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL"); |
| 554 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 555 | // Configure consumer-side ANativeWindow interface. The listener may be used |
| 556 | // to notify buffer manager (if it is used) of the returned buffers. |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 557 | res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 558 | /*reportBufferRemoval*/true, |
| 559 | /*listener*/mBufferProducerListener); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 560 | if (res != OK) { |
| 561 | ALOGE("%s: Unable to connect to native window for stream %d", |
| 562 | __FUNCTION__, mId); |
| 563 | return res; |
| 564 | } |
| 565 | |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 566 | res = native_window_set_usage(mConsumer.get(), mUsage); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 567 | if (res != OK) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 568 | ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d", |
| 569 | __FUNCTION__, mUsage, mId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 570 | return res; |
| 571 | } |
| 572 | |
| 573 | res = native_window_set_scaling_mode(mConsumer.get(), |
| 574 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 575 | if (res != OK) { |
| 576 | ALOGE("%s: Unable to configure stream scaling: %s (%d)", |
| 577 | __FUNCTION__, strerror(-res), res); |
| 578 | return res; |
| 579 | } |
| 580 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 581 | if (mMaxSize == 0) { |
| 582 | // For buffers of known size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 583 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 584 | camera_stream::width, camera_stream::height); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 585 | } else { |
| 586 | // For buffers with bounded size |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 587 | res = native_window_set_buffers_dimensions(mConsumer.get(), |
| 588 | mMaxSize, 1); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 589 | } |
| 590 | if (res != OK) { |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 591 | ALOGE("%s: Unable to configure stream buffer dimensions" |
| 592 | " %d x %d (maxSize %zu) for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 593 | __FUNCTION__, camera_stream::width, camera_stream::height, |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 594 | mMaxSize, mId); |
| 595 | return res; |
| 596 | } |
| 597 | res = native_window_set_buffers_format(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 598 | camera_stream::format); |
Eino-Ville Talvala | 7d70c5e | 2014-07-24 18:10:23 -0700 | [diff] [blame] | 599 | if (res != OK) { |
| 600 | ALOGE("%s: Unable to configure stream buffer format %#x for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 601 | __FUNCTION__, camera_stream::format, mId); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 602 | return res; |
| 603 | } |
| 604 | |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 605 | res = native_window_set_buffers_data_space(mConsumer.get(), |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 606 | camera_stream::data_space); |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 607 | if (res != OK) { |
| 608 | ALOGE("%s: Unable to configure stream dataspace %#x for stream %d", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 609 | __FUNCTION__, camera_stream::data_space, mId); |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 610 | return res; |
| 611 | } |
| 612 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 613 | int maxConsumerBuffers = 0; |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 614 | res = static_cast<ANativeWindow*>(mConsumer.get())->query( |
| 615 | mConsumer.get(), |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 616 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers); |
| 617 | if (res != OK) { |
| 618 | ALOGE("%s: Unable to query consumer undequeued" |
| 619 | " buffer count for stream %d", __FUNCTION__, mId); |
| 620 | return res; |
| 621 | } |
| 622 | |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 623 | ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 624 | maxConsumerBuffers, camera_stream::max_buffers); |
| 625 | if (camera_stream::max_buffers == 0) { |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 626 | ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1", |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 627 | __FUNCTION__, camera_stream::max_buffers); |
Alex Ray | 20cb300 | 2013-05-28 20:18:22 -0700 | [diff] [blame] | 628 | return INVALID_OPERATION; |
| 629 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 630 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 631 | mTotalBufferCount = maxConsumerBuffers + camera_stream::max_buffers; |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 632 | |
| 633 | int timestampBase = getTimestampBase(); |
| 634 | bool isDefaultTimeBase = (timestampBase == |
| 635 | OutputConfiguration::TIMESTAMP_BASE_DEFAULT); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 636 | if (allowPreviewRespace) { |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 637 | bool forceChoreographer = (timestampBase == |
| 638 | OutputConfiguration::TIMESTAMP_BASE_CHOREOGRAPHER_SYNCED); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 639 | bool defaultToChoreographer = (isDefaultTimeBase && |
Shuzhen Wang | fe8a2a3 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 640 | isConsumedByHWComposer()); |
| 641 | bool defaultToSpacer = (isDefaultTimeBase && |
| 642 | isConsumedByHWTexture() && |
| 643 | !isConsumedByCPU() && |
| 644 | !isVideoStream()); |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 645 | if (forceChoreographer || defaultToChoreographer) { |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 646 | mSyncToDisplay = true; |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 647 | // For choreographer synced stream, extra buffers aren't kept by |
| 648 | // camera service. So no need to update mMaxCachedBufferCount. |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 649 | mTotalBufferCount += kDisplaySyncExtraBuffer; |
Shuzhen Wang | fe8a2a3 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 650 | } else if (defaultToSpacer) { |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 651 | mPreviewFrameSpacer = new PreviewFrameSpacer(this, mConsumer); |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 652 | // For preview frame spacer, the extra buffer is kept by camera |
| 653 | // service. So update mMaxCachedBufferCount. |
| 654 | mMaxCachedBufferCount = 1; |
| 655 | mTotalBufferCount += mMaxCachedBufferCount; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 656 | res = mPreviewFrameSpacer->run((std::string("PreviewSpacer-") |
| 657 | + std::to_string(mId)).c_str()); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 658 | if (res != OK) { |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame] | 659 | ALOGE("%s: Unable to start preview spacer: %s (%d)", __FUNCTION__, |
| 660 | strerror(-res), res); |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 661 | return res; |
| 662 | } |
Shuzhen Wang | e4adddb | 2021-09-21 15:24:44 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
Zhijun He | 6adc9cc | 2014-04-15 14:09:55 -0700 | [diff] [blame] | 665 | mHandoutTotalBufferCount = 0; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 666 | mFrameCount = 0; |
| 667 | mLastTimestamp = 0; |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 668 | |
| 669 | if (isDeviceTimeBaseRealtime()) { |
| 670 | if (isDefaultTimeBase && !isConsumedByHWComposer() && !isVideoStream()) { |
| 671 | // Default time base, but not hardware composer or video encoder |
| 672 | mTimestampOffset = 0; |
| 673 | } else if (timestampBase == OutputConfiguration::TIMESTAMP_BASE_REALTIME || |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 674 | timestampBase == OutputConfiguration::TIMESTAMP_BASE_SENSOR) { |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 675 | mTimestampOffset = 0; |
| 676 | } |
| 677 | // If timestampBase is CHOREOGRAPHER SYNCED or MONOTONIC, leave |
| 678 | // timestamp offset as bootTime - monotonicTime. |
| 679 | } else { |
| 680 | if (timestampBase == OutputConfiguration::TIMESTAMP_BASE_REALTIME) { |
| 681 | // Reverse offset for monotonicTime -> bootTime |
| 682 | mTimestampOffset = -mTimestampOffset; |
| 683 | } else { |
Shuzhen Wang | bce53db | 2022-12-03 00:38:20 +0000 | [diff] [blame] | 684 | // If timestampBase is DEFAULT, MONOTONIC, SENSOR or |
Shuzhen Wang | e420892 | 2022-02-01 16:52:48 -0800 | [diff] [blame] | 685 | // CHOREOGRAPHER_SYNCED, timestamp offset is 0. |
| 686 | mTimestampOffset = 0; |
| 687 | } |
| 688 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 689 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 690 | if (flags::surface_ipc()) { |
| 691 | res = mConsumer->setMaxDequeuedBufferCount(mTotalBufferCount - maxConsumerBuffers); |
| 692 | } else { |
| 693 | res = native_window_set_buffer_count(mConsumer.get(), mTotalBufferCount); |
| 694 | } |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 695 | if (res != OK) { |
| 696 | ALOGE("%s: Unable to set buffer count for stream %d", |
| 697 | __FUNCTION__, mId); |
| 698 | return res; |
| 699 | } |
| 700 | |
| 701 | res = native_window_set_buffers_transform(mConsumer.get(), |
| 702 | mTransform); |
| 703 | if (res != OK) { |
| 704 | ALOGE("%s: Unable to configure stream transform to %x: %s (%d)", |
| 705 | __FUNCTION__, mTransform, strerror(-res), res); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 706 | return res; |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 709 | /** |
Zhijun He | edd41ae | 2016-02-03 14:45:53 -0800 | [diff] [blame] | 710 | * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 711 | * buffers to be statically allocated for internal static buffer registration, while the |
| 712 | * buffers provided by buffer manager are really dynamically allocated. Camera3Device only |
Zhijun He | edd41ae | 2016-02-03 14:45:53 -0800 | [diff] [blame] | 713 | * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer |
| 714 | * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some |
| 715 | * HAL3.2 devices may not support the dynamic buffer registeration. |
Yin-Chia Yeh | b657890 | 2019-04-16 13:36:16 -0700 | [diff] [blame] | 716 | * Also Camera3BufferManager does not support display/texture streams as they have its own |
| 717 | * buffer management logic. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 718 | */ |
Yin-Chia Yeh | b657890 | 2019-04-16 13:36:16 -0700 | [diff] [blame] | 719 | if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID && |
| 720 | !(isConsumedByHWComposer() || isConsumedByHWTexture())) { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 721 | uint64_t consumerUsage = 0; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 722 | getEndpointUsage(&consumerUsage); |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 723 | uint32_t width = (mMaxSize == 0) ? getWidth() : mMaxSize; |
| 724 | uint32_t height = (mMaxSize == 0) ? getHeight() : 1; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 725 | StreamInfo streamInfo( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 726 | getId(), getStreamSetId(), width, height, getFormat(), getDataSpace(), |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 727 | mUsage | consumerUsage, mTotalBufferCount, |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 728 | /*isConfigured*/true, isMultiResolution()); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 729 | wp<Camera3OutputStream> weakThis(this); |
| 730 | res = mBufferManager->registerStream(weakThis, |
| 731 | streamInfo); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 732 | if (res == OK) { |
| 733 | // Disable buffer allocation for this BufferQueue, buffer manager will take over |
| 734 | // the buffer allocation responsibility. |
| 735 | mConsumer->getIGraphicBufferProducer()->allowAllocation(false); |
| 736 | mUseBufferManager = true; |
| 737 | } else { |
| 738 | ALOGE("%s: Unable to register stream %d to camera3 buffer manager, " |
| 739 | "(error %d %s), fall back to BufferQueue for buffer management!", |
| 740 | __FUNCTION__, mId, res, strerror(-res)); |
| 741 | } |
| 742 | } |
| 743 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 744 | return OK; |
| 745 | } |
| 746 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 747 | status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) { |
Jayant Chowdhary | d477626 | 2020-06-23 23:45:57 -0700 | [diff] [blame] | 748 | ATRACE_HFR_CALL(); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 749 | status_t res; |
| 750 | |
| 751 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 752 | return res; |
| 753 | } |
| 754 | |
| 755 | bool gotBufferFromManager = false; |
| 756 | |
| 757 | if (mUseBufferManager) { |
| 758 | sp<GraphicBuffer> gb; |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 759 | res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), |
| 760 | isMultiResolution(), &gb, fenceFd); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 761 | if (res == OK) { |
| 762 | // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a |
| 763 | // successful return. |
| 764 | *anb = gb.get(); |
| 765 | res = mConsumer->attachBuffer(*anb); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 766 | if (shouldLogError(res, mState)) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 767 | ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)", |
| 768 | __FUNCTION__, mId, strerror(-res), res); |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 769 | } |
| 770 | if (res != OK) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 771 | checkRetAndSetAbandonedLocked(res); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 772 | return res; |
| 773 | } |
| 774 | gotBufferFromManager = true; |
| 775 | ALOGV("Stream %d: Attached new buffer", getId()); |
| 776 | } else if (res == ALREADY_EXISTS) { |
| 777 | // Have sufficient free buffers already attached, can just |
| 778 | // dequeue from buffer queue |
| 779 | ALOGV("Stream %d: Reusing attached buffer", getId()); |
| 780 | gotBufferFromManager = false; |
| 781 | } else if (res != OK) { |
| 782 | ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)", |
| 783 | __FUNCTION__, mId, strerror(-res), res); |
| 784 | return res; |
| 785 | } |
| 786 | } |
| 787 | if (!gotBufferFromManager) { |
| 788 | /** |
| 789 | * Release the lock briefly to avoid deadlock for below scenario: |
| 790 | * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring(). |
| 791 | * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock. |
| 792 | * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable(). |
| 793 | * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock |
| 794 | * StreamingProcessor lock. |
| 795 | * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock |
| 796 | * and try to lock bufferQueue lock. |
| 797 | * Then there is circular locking dependency. |
| 798 | */ |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 799 | sp<Surface> consumer = mConsumer; |
Shuzhen Wang | 6c14e31 | 2021-07-05 16:20:33 -0700 | [diff] [blame] | 800 | size_t remainingBuffers = (mState == STATE_PREPARING ? mTotalBufferCount : |
| 801 | camera_stream::max_buffers) - mHandoutTotalBufferCount; |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 802 | mLock.unlock(); |
| 803 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 804 | nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 805 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 806 | size_t batchSize = mBatchSize.load(); |
| 807 | if (batchSize == 1) { |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 808 | sp<ANativeWindow> anw = consumer; |
| 809 | res = anw->dequeueBuffer(anw.get(), anb, fenceFd); |
| 810 | } else { |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 811 | std::unique_lock<std::mutex> batchLock(mBatchLock); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 812 | res = OK; |
| 813 | if (mBatchedBuffers.size() == 0) { |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 814 | if (remainingBuffers == 0) { |
| 815 | ALOGE("%s: cannot get buffer while all buffers are handed out", __FUNCTION__); |
| 816 | return INVALID_OPERATION; |
| 817 | } |
| 818 | if (batchSize > remainingBuffers) { |
| 819 | batchSize = remainingBuffers; |
| 820 | } |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 821 | batchLock.unlock(); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 822 | // Refill batched buffers |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 823 | std::vector<Surface::BatchBuffer> batchedBuffers; |
| 824 | batchedBuffers.resize(batchSize); |
| 825 | res = consumer->dequeueBuffers(&batchedBuffers); |
| 826 | batchLock.lock(); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 827 | if (res != OK) { |
| 828 | ALOGE("%s: batch dequeueBuffers call failed! %s (%d)", |
| 829 | __FUNCTION__, strerror(-res), res); |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 830 | } else { |
| 831 | mBatchedBuffers = std::move(batchedBuffers); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
| 835 | if (res == OK) { |
| 836 | // Dispatch batch buffers |
| 837 | *anb = mBatchedBuffers.back().buffer; |
| 838 | *fenceFd = mBatchedBuffers.back().fenceFd; |
| 839 | mBatchedBuffers.pop_back(); |
| 840 | } |
| 841 | } |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 842 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 843 | nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC); |
| 844 | mDequeueBufferLatency.add(dequeueStart, dequeueEnd); |
| 845 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 846 | mLock.lock(); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 847 | |
| 848 | if (mUseBufferManager && res == TIMED_OUT) { |
| 849 | checkRemovedBuffersLocked(); |
| 850 | |
| 851 | sp<GraphicBuffer> gb; |
| 852 | res = mBufferManager->getBufferForStream( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 853 | getId(), getStreamSetId(), isMultiResolution(), |
| 854 | &gb, fenceFd, /*noFreeBuffer*/true); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 855 | |
| 856 | if (res == OK) { |
| 857 | // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after |
| 858 | // a successful return. |
| 859 | *anb = gb.get(); |
| 860 | res = mConsumer->attachBuffer(*anb); |
| 861 | gotBufferFromManager = true; |
| 862 | ALOGV("Stream %d: Attached new buffer", getId()); |
| 863 | |
| 864 | if (res != OK) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 865 | if (shouldLogError(res, mState)) { |
| 866 | ALOGE("%s: Stream %d: Can't attach the output buffer to this surface:" |
| 867 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 868 | } |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 869 | checkRetAndSetAbandonedLocked(res); |
| 870 | return res; |
| 871 | } |
| 872 | } else { |
| 873 | ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager:" |
| 874 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 875 | return res; |
| 876 | } |
| 877 | } else if (res != OK) { |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 878 | if (shouldLogError(res, mState)) { |
| 879 | ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)", |
| 880 | __FUNCTION__, mId, strerror(-res), res); |
| 881 | } |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 882 | checkRetAndSetAbandonedLocked(res); |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 883 | return res; |
| 884 | } |
| 885 | } |
| 886 | |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 887 | if (res == OK) { |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 888 | checkRemovedBuffersLocked(); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 891 | return res; |
| 892 | } |
| 893 | |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 894 | void Camera3OutputStream::checkRemovedBuffersLocked(bool notifyBufferManager) { |
| 895 | std::vector<sp<GraphicBuffer>> removedBuffers; |
| 896 | status_t res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers); |
| 897 | if (res == OK) { |
| 898 | onBuffersRemovedLocked(removedBuffers); |
| 899 | |
| 900 | if (notifyBufferManager && mUseBufferManager && removedBuffers.size() > 0) { |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 901 | mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), isMultiResolution(), |
| 902 | removedBuffers.size()); |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | void Camera3OutputStream::checkRetAndSetAbandonedLocked(status_t res) { |
| 908 | // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is |
| 909 | // STATE_PREPARING, let prepareNextBuffer handle the error.) |
| 910 | if ((res == NO_INIT || res == DEAD_OBJECT) && mState == STATE_CONFIGURED) { |
| 911 | mState = STATE_ABANDONED; |
| 912 | } |
| 913 | } |
| 914 | |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 915 | bool Camera3OutputStream::shouldLogError(status_t res, StreamState state) { |
| 916 | if (res == OK) { |
| 917 | return false; |
| 918 | } |
| 919 | if ((res == DEAD_OBJECT || res == NO_INIT) && state == STATE_ABANDONED) { |
| 920 | return false; |
| 921 | } |
| 922 | return true; |
| 923 | } |
| 924 | |
Shuzhen Wang | c235270 | 2022-09-06 18:36:31 -0700 | [diff] [blame] | 925 | void Camera3OutputStream::onCachedBufferQueued() { |
| 926 | Mutex::Autolock l(mLock); |
| 927 | mCachedOutputBufferCount--; |
| 928 | // Signal whoever is waiting for the buffer to be returned to the buffer |
| 929 | // queue. |
| 930 | mOutputBufferReturnedSignal.signal(); |
| 931 | } |
| 932 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 933 | status_t Camera3OutputStream::disconnectLocked() { |
| 934 | status_t res; |
| 935 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 936 | if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { |
| 937 | return res; |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 940 | // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED |
| 941 | // state), don't need change the stream state, return OK. |
| 942 | if (mConsumer == nullptr) { |
| 943 | return OK; |
| 944 | } |
| 945 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 946 | returnPrefetchedBuffersLocked(); |
| 947 | |
Shuzhen Wang | dc9aa82 | 2022-05-16 10:04:17 -0700 | [diff] [blame] | 948 | if (mPreviewFrameSpacer != nullptr) { |
| 949 | mPreviewFrameSpacer->requestExit(); |
| 950 | } |
| 951 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 952 | ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId()); |
| 953 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 954 | res = native_window_api_disconnect(mConsumer.get(), |
| 955 | NATIVE_WINDOW_API_CAMERA); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 956 | /** |
| 957 | * This is not an error. if client calling process dies, the window will |
| 958 | * also die and all calls to it will return DEAD_OBJECT, thus it's already |
| 959 | * "disconnected" |
| 960 | */ |
| 961 | if (res == DEAD_OBJECT) { |
| 962 | ALOGW("%s: While disconnecting stream %d from native window, the" |
| 963 | " native window died from under us", __FUNCTION__, mId); |
| 964 | } |
| 965 | else if (res != OK) { |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 966 | ALOGE("%s: Unable to disconnect stream %d from native window " |
| 967 | "(error %d %s)", |
| 968 | __FUNCTION__, mId, res, strerror(-res)); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 969 | mState = STATE_ERROR; |
| 970 | return res; |
| 971 | } |
| 972 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 973 | // Since device is already idle, there is no getBuffer call to buffer manager, unregister the |
| 974 | // stream at this point should be safe. |
| 975 | if (mUseBufferManager) { |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 976 | res = mBufferManager->unregisterStream(getId(), getStreamSetId(), isMultiResolution()); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 977 | if (res != OK) { |
| 978 | ALOGE("%s: Unable to unregister stream %d from buffer manager " |
| 979 | "(error %d %s)", __FUNCTION__, mId, res, strerror(-res)); |
| 980 | mState = STATE_ERROR; |
| 981 | return res; |
| 982 | } |
| 983 | // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as |
| 984 | // the stream is still in usable state after this call. |
| 985 | mUseBufferManager = false; |
| 986 | } |
| 987 | |
Igor Murashkin | e3a9f96 | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 988 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG |
| 989 | : STATE_CONSTRUCTED; |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 990 | |
| 991 | mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId); |
| 992 | mDequeueBufferLatency.reset(); |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 993 | return OK; |
| 994 | } |
| 995 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 996 | status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) { |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 997 | |
| 998 | status_t res; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 999 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1000 | if (mConsumer == nullptr) { |
| 1001 | // mConsumerUsage was sanitized before the Camera3OutputStream was constructed. |
| 1002 | *usage = mConsumerUsage; |
| 1003 | return OK; |
| 1004 | } |
| 1005 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1006 | res = getEndpointUsageForSurface(usage, mConsumer); |
| 1007 | |
| 1008 | return res; |
| 1009 | } |
| 1010 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1011 | void Camera3OutputStream::applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/) { |
| 1012 | if (consumerUsage == nullptr) { |
| 1013 | return; |
| 1014 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1015 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1016 | // If an opaque output stream's endpoint is ImageReader, add |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 1017 | // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1018 | // for the ZSL use case. |
| 1019 | // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set: |
| 1020 | // 1. GRALLOC_USAGE_HW_TEXTURE |
| 1021 | // 2. GRALLOC_USAGE_HW_RENDER |
| 1022 | // 3. GRALLOC_USAGE_HW_COMPOSER |
| 1023 | // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1024 | if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && |
| 1025 | (*consumerUsage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1026 | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) { |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1027 | *consumerUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL; |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1028 | } |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1029 | } |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1030 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1031 | status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage, |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1032 | const sp<Surface>& surface) { |
Emilian Peev | a495a7b | 2024-04-02 22:52:42 +0000 | [diff] [blame] | 1033 | bool internalConsumer = (mConsumer.get() != nullptr) && (mConsumer == surface); |
| 1034 | if (mConsumerUsageCachedValue.has_value() && flags::surface_ipc() && internalConsumer) { |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1035 | *usage = mConsumerUsageCachedValue.value(); |
| 1036 | return OK; |
| 1037 | } |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 1038 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1039 | status_t res; |
| 1040 | |
| 1041 | res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), usage); |
| 1042 | applyZSLUsageQuirk(camera_stream::format, usage); |
Emilian Peev | a495a7b | 2024-04-02 22:52:42 +0000 | [diff] [blame] | 1043 | if (internalConsumer) { |
| 1044 | mConsumerUsageCachedValue = *usage; |
| 1045 | } |
Eino-Ville Talvala | b2f5b19 | 2013-07-30 14:36:03 -0700 | [diff] [blame] | 1046 | return res; |
| 1047 | } |
| 1048 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1049 | bool Camera3OutputStream::isVideoStream() { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 1050 | uint64_t usage = 0; |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 1051 | status_t res = getEndpointUsage(&usage); |
| 1052 | if (res != OK) { |
| 1053 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
| 1057 | return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0; |
| 1058 | } |
| 1059 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1060 | status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) { |
| 1061 | Mutex::Autolock l(mLock); |
| 1062 | if (mState != STATE_CONSTRUCTED) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1063 | ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.", |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1064 | __FUNCTION__); |
| 1065 | return INVALID_OPERATION; |
| 1066 | } |
| 1067 | mBufferManager = bufferManager; |
| 1068 | |
| 1069 | return OK; |
| 1070 | } |
| 1071 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 1072 | status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/, |
| 1073 | const std::vector<OutputStreamInfo> &/*outputInfo*/, |
| 1074 | const std::vector<size_t> &/*removedSurfaceIds*/, |
| 1075 | KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) { |
| 1076 | ALOGE("%s: this method is not supported!", __FUNCTION__); |
| 1077 | return INVALID_OPERATION; |
| 1078 | } |
| 1079 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 1080 | void Camera3OutputStream::BufferProducerListener::onBufferReleased() { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1081 | sp<Camera3OutputStream> stream = mParent.promote(); |
| 1082 | if (stream == nullptr) { |
| 1083 | ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); |
| 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | Mutex::Autolock l(stream->mLock); |
| 1088 | if (!(stream->mUseBufferManager)) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1092 | ALOGV("Stream %d: Buffer released", stream->getId()); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 1093 | bool shouldFreeBuffer = false; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1094 | status_t res = stream->mBufferManager->onBufferReleased( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 1095 | stream->getId(), stream->getStreamSetId(), stream->isMultiResolution(), |
| 1096 | &shouldFreeBuffer); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1097 | if (res != OK) { |
| 1098 | ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__, |
| 1099 | strerror(-res), res); |
| 1100 | stream->mState = STATE_ERROR; |
| 1101 | } |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 1102 | |
| 1103 | if (shouldFreeBuffer) { |
| 1104 | sp<GraphicBuffer> buffer; |
| 1105 | // Detach and free a buffer (when buffer goes out of scope) |
| 1106 | stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr); |
| 1107 | if (buffer.get() != nullptr) { |
| 1108 | stream->mBufferManager->notifyBufferRemoved( |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 1109 | stream->getId(), stream->getStreamSetId(), stream->isMultiResolution()); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 1114 | void Camera3OutputStream::BufferProducerListener::onBuffersDiscarded( |
| 1115 | const std::vector<sp<GraphicBuffer>>& buffers) { |
| 1116 | sp<Camera3OutputStream> stream = mParent.promote(); |
| 1117 | if (stream == nullptr) { |
| 1118 | ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | if (buffers.size() > 0) { |
| 1123 | Mutex::Autolock l(stream->mLock); |
| 1124 | stream->onBuffersRemovedLocked(buffers); |
| 1125 | if (stream->mUseBufferManager) { |
| 1126 | stream->mBufferManager->onBuffersRemoved(stream->getId(), |
Shuzhen Wang | 83bff12 | 2020-11-20 15:51:39 -0800 | [diff] [blame] | 1127 | stream->getStreamSetId(), stream->isMultiResolution(), buffers.size()); |
Shuzhen Wang | 0160ddd | 2019-08-15 09:11:56 -0700 | [diff] [blame] | 1128 | } |
| 1129 | ALOGV("Stream %d: %zu Buffers discarded.", stream->getId(), buffers.size()); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 1133 | void Camera3OutputStream::onBuffersRemovedLocked( |
| 1134 | const std::vector<sp<GraphicBuffer>>& removedBuffers) { |
Yin-Chia Yeh | db1e864 | 2017-07-14 15:19:30 -0700 | [diff] [blame] | 1135 | sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote(); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 1136 | if (callback != nullptr) { |
Chih-Hung Hsieh | 48fc619 | 2017-08-04 14:37:31 -0700 | [diff] [blame] | 1137 | for (const auto& gb : removedBuffers) { |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 1138 | callback->onBufferFreed(mId, gb->handle); |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1143 | status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) { |
| 1144 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 1145 | return detachBufferLocked(buffer, fenceFd); |
| 1146 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1147 | |
Yin-Chia Yeh | 89954d9 | 2017-05-21 17:28:53 -0700 | [diff] [blame] | 1148 | status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) { |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1149 | ALOGV("Stream %d: detachBuffer", getId()); |
| 1150 | if (buffer == nullptr) { |
| 1151 | return BAD_VALUE; |
| 1152 | } |
| 1153 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1154 | sp<Fence> fence; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1155 | status_t res = mConsumer->detachNextBuffer(buffer, &fence); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1156 | if (res == NO_MEMORY) { |
| 1157 | // This may rarely happen, which indicates that the released buffer was freed by other |
| 1158 | // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the |
| 1159 | // buffer manager that this buffer has been freed. It's not fatal, but should be avoided, |
| 1160 | // therefore log a warning. |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1161 | *buffer = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1162 | ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__); |
| 1163 | } else if (res != OK) { |
Eino-Ville Talvala | ff51b47 | 2016-06-28 15:26:19 -0700 | [diff] [blame] | 1164 | // Treat other errors as abandonment |
Yin-Chia Yeh | a1b56c8 | 2019-03-27 15:50:39 -0700 | [diff] [blame] | 1165 | if (shouldLogError(res, mState)) { |
| 1166 | ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1167 | } |
Eino-Ville Talvala | ff51b47 | 2016-06-28 15:26:19 -0700 | [diff] [blame] | 1168 | mState = STATE_ABANDONED; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1169 | return res; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1172 | if (fenceFd != nullptr) { |
| 1173 | if (fence!= 0 && fence->isValid()) { |
| 1174 | *fenceFd = fence->dup(); |
| 1175 | } else { |
| 1176 | *fenceFd = -1; |
| 1177 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1178 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 1179 | |
Yin-Chia Yeh | bf1b8b9 | 2019-03-06 14:56:08 -0800 | [diff] [blame] | 1180 | // Here we assume detachBuffer is called by buffer manager so it doesn't need to be notified |
| 1181 | checkRemovedBuffersLocked(/*notifyBufferManager*/false); |
Yin-Chia Yeh | 017d49c | 2017-03-31 19:11:00 -0700 | [diff] [blame] | 1182 | return res; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1183 | } |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 1184 | |
Chien-Yu Chen | a936ac2 | 2017-10-23 15:59:49 -0700 | [diff] [blame] | 1185 | status_t Camera3OutputStream::dropBuffers(bool dropping) { |
| 1186 | Mutex::Autolock l(mLock); |
| 1187 | mDropBuffers = dropping; |
| 1188 | return OK; |
| 1189 | } |
| 1190 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1191 | const std::string& Camera3OutputStream::getPhysicalCameraId() const { |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 1192 | Mutex::Autolock l(mLock); |
| 1193 | return physicalCameraId(); |
| 1194 | } |
| 1195 | |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 1196 | status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1197 | return OK; |
| 1198 | } |
| 1199 | |
| 1200 | bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1201 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1202 | |
| 1203 | if (surface_id != 0) { |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1204 | ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1205 | } |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1206 | return mConsumer == nullptr; |
| 1207 | } |
| 1208 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1209 | status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) { |
Shuzhen Wang | bee0f0a | 2017-01-24 14:51:37 -0800 | [diff] [blame] | 1210 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1211 | if (consumers.size() != 1) { |
| 1212 | ALOGE("%s: it's illegal to set %zu consumer surfaces!", |
| 1213 | __FUNCTION__, consumers.size()); |
| 1214 | return INVALID_OPERATION; |
| 1215 | } |
| 1216 | if (consumers[0] == nullptr) { |
| 1217 | ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1218 | return INVALID_OPERATION; |
| 1219 | } |
| 1220 | |
| 1221 | if (mConsumer != nullptr) { |
| 1222 | ALOGE("%s: consumer surface was already set!", __FUNCTION__); |
| 1223 | return INVALID_OPERATION; |
| 1224 | } |
| 1225 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1226 | mConsumer = consumers[0]; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1227 | return OK; |
| 1228 | } |
| 1229 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1230 | bool Camera3OutputStream::isConsumedByHWComposer() { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 1231 | uint64_t usage = 0; |
Shuzhen Wang | 13a6963 | 2016-01-26 09:51:07 -0800 | [diff] [blame] | 1232 | status_t res = getEndpointUsage(&usage); |
| 1233 | if (res != OK) { |
| 1234 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0; |
| 1239 | } |
| 1240 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1241 | bool Camera3OutputStream::isConsumedByHWTexture() { |
Emilian Peev | 050f5dc | 2017-05-18 14:43:56 +0100 | [diff] [blame] | 1242 | uint64_t usage = 0; |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 1243 | status_t res = getEndpointUsage(&usage); |
| 1244 | if (res != OK) { |
| 1245 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
| 1249 | return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0; |
| 1250 | } |
| 1251 | |
Emilian Peev | 3b93acb | 2024-03-11 21:09:48 +0000 | [diff] [blame] | 1252 | bool Camera3OutputStream::isConsumedByCPU() { |
Shuzhen Wang | fe8a2a3 | 2022-05-10 18:18:54 -0700 | [diff] [blame] | 1253 | uint64_t usage = 0; |
| 1254 | status_t res = getEndpointUsage(&usage); |
| 1255 | if (res != OK) { |
| 1256 | ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res); |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | return (usage & GRALLOC_USAGE_SW_READ_MASK) != 0; |
| 1261 | } |
| 1262 | |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1263 | void Camera3OutputStream::dumpImageToDisk(nsecs_t timestamp, |
| 1264 | ANativeWindowBuffer* anwBuffer, int fence) { |
| 1265 | // Deriver output file name |
| 1266 | std::string fileExtension = "jpg"; |
| 1267 | char imageFileName[64]; |
| 1268 | time_t now = time(0); |
| 1269 | tm *localTime = localtime(&now); |
| 1270 | snprintf(imageFileName, sizeof(imageFileName), "IMG_%4d%02d%02d_%02d%02d%02d_%" PRId64 ".%s", |
Shuzhen Wang | 6a8237f | 2021-07-13 14:42:57 -0700 | [diff] [blame] | 1271 | 1900 + localTime->tm_year, localTime->tm_mon + 1, localTime->tm_mday, |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1272 | localTime->tm_hour, localTime->tm_min, localTime->tm_sec, |
| 1273 | timestamp, fileExtension.c_str()); |
| 1274 | |
| 1275 | // Lock the image for CPU read |
| 1276 | sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(anwBuffer); |
| 1277 | void* mapped = nullptr; |
| 1278 | base::unique_fd fenceFd(dup(fence)); |
| 1279 | status_t res = graphicBuffer->lockAsync(GraphicBuffer::USAGE_SW_READ_OFTEN, &mapped, |
Emilian Peev | 293bd97 | 2022-08-05 17:28:06 -0700 | [diff] [blame] | 1280 | fenceFd.release()); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1281 | if (res != OK) { |
| 1282 | ALOGE("%s: Failed to lock the buffer: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | // Figure out actual file size |
| 1287 | auto actualJpegSize = android::camera2::JpegProcessor::findJpegSize((uint8_t*)mapped, mMaxSize); |
| 1288 | if (actualJpegSize == 0) { |
| 1289 | actualJpegSize = mMaxSize; |
| 1290 | } |
| 1291 | |
| 1292 | // Output image data to file |
| 1293 | std::string filePath = "/data/misc/cameraserver/"; |
| 1294 | filePath += imageFileName; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1295 | std::ofstream imageFile(filePath, std::ofstream::binary); |
Shuzhen Wang | abbcb6b | 2020-12-09 22:32:44 -0800 | [diff] [blame] | 1296 | if (!imageFile.is_open()) { |
| 1297 | ALOGE("%s: Unable to create file %s", __FUNCTION__, filePath.c_str()); |
| 1298 | graphicBuffer->unlock(); |
| 1299 | return; |
| 1300 | } |
| 1301 | imageFile.write((const char*)mapped, actualJpegSize); |
| 1302 | |
| 1303 | graphicBuffer->unlock(); |
| 1304 | } |
| 1305 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1306 | status_t Camera3OutputStream::setBatchSize(size_t batchSize) { |
| 1307 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1308 | if (batchSize == 0) { |
| 1309 | ALOGE("%s: invalid batch size 0", __FUNCTION__); |
| 1310 | return BAD_VALUE; |
| 1311 | } |
| 1312 | |
| 1313 | if (mUseBufferManager) { |
| 1314 | ALOGE("%s: batch operation is not supported with buffer manager", __FUNCTION__); |
| 1315 | return INVALID_OPERATION; |
| 1316 | } |
| 1317 | |
| 1318 | if (!isVideoStream()) { |
| 1319 | ALOGE("%s: batch operation is not supported with non-video stream", __FUNCTION__); |
| 1320 | return INVALID_OPERATION; |
| 1321 | } |
| 1322 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1323 | if (camera_stream::max_buffers < batchSize) { |
| 1324 | ALOGW("%s: batch size is capped by max_buffers %d", __FUNCTION__, |
| 1325 | camera_stream::max_buffers); |
| 1326 | batchSize = camera_stream::max_buffers; |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1327 | } |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1328 | |
| 1329 | size_t defaultBatchSize = 1; |
| 1330 | if (!mBatchSize.compare_exchange_strong(defaultBatchSize, batchSize)) { |
| 1331 | ALOGE("%s: change batch size from %zu to %zu dynamically is not supported", |
| 1332 | __FUNCTION__, defaultBatchSize, batchSize); |
| 1333 | return INVALID_OPERATION; |
| 1334 | } |
| 1335 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1336 | return OK; |
| 1337 | } |
| 1338 | |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1339 | void Camera3OutputStream::onMinDurationChanged(nsecs_t duration, bool fixedFps) { |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1340 | Mutex::Autolock l(mLock); |
| 1341 | mMinExpectedDuration = duration; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1342 | mFixedFps = fixedFps; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 1345 | void Camera3OutputStream::setStreamUseCase(int64_t streamUseCase) { |
| 1346 | Mutex::Autolock l(mLock); |
| 1347 | camera_stream::use_case = streamUseCase; |
| 1348 | } |
| 1349 | |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1350 | void Camera3OutputStream::returnPrefetchedBuffersLocked() { |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1351 | std::vector<Surface::BatchBuffer> batchedBuffers; |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1352 | |
Shuzhen Wang | c762946 | 2021-07-12 15:02:58 -0700 | [diff] [blame] | 1353 | { |
| 1354 | std::lock_guard<std::mutex> batchLock(mBatchLock); |
| 1355 | if (mBatchedBuffers.size() != 0) { |
| 1356 | ALOGW("%s: %zu extra prefetched buffers detected. Returning", |
| 1357 | __FUNCTION__, mBatchedBuffers.size()); |
| 1358 | batchedBuffers = std::move(mBatchedBuffers); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | if (batchedBuffers.size() > 0) { |
| 1363 | mConsumer->cancelBuffers(batchedBuffers); |
Yin-Chia Yeh | 14ef48d | 2020-02-10 15:06:37 -0800 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Ravneet | 446b3bf | 2023-07-11 19:26:38 +0000 | [diff] [blame] | 1367 | nsecs_t Camera3OutputStream::syncTimestampToDisplayLocked(nsecs_t t, sp<Fence> releaseFence) { |
Shuzhen Wang | 35bd355 | 2022-09-21 16:56:04 -0700 | [diff] [blame] | 1368 | nsecs_t currentTime = systemTime(); |
| 1369 | if (!mFixedFps) { |
| 1370 | mLastCaptureTime = t; |
| 1371 | mLastPresentTime = currentTime; |
| 1372 | return t; |
| 1373 | } |
| 1374 | |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1375 | ParcelableVsyncEventData parcelableVsyncEventData; |
| 1376 | auto res = mDisplayEventReceiver.getLatestVsyncEventData(&parcelableVsyncEventData); |
| 1377 | if (res != OK) { |
| 1378 | ALOGE("%s: Stream %d: Error getting latest vsync event data: %s (%d)", |
| 1379 | __FUNCTION__, mId, strerror(-res), res); |
Shuzhen Wang | 14c62b8 | 2022-04-11 09:37:05 -0700 | [diff] [blame] | 1380 | mLastCaptureTime = t; |
Shuzhen Wang | 35bd355 | 2022-09-21 16:56:04 -0700 | [diff] [blame] | 1381 | mLastPresentTime = currentTime; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1382 | return t; |
| 1383 | } |
| 1384 | |
| 1385 | const VsyncEventData& vsyncEventData = parcelableVsyncEventData.vsync; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1386 | nsecs_t minPresentT = mLastPresentTime + vsyncEventData.frameInterval / 2; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1387 | |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1388 | // Find the best presentation time without worrying about previous frame's |
| 1389 | // presentation time if capture interval is more than kSpacingResetIntervalNs. |
| 1390 | // |
| 1391 | // When frame interval is more than 50 ms apart (3 vsyncs for 60hz refresh rate), |
| 1392 | // there is little risk in starting over and finding the earliest vsync to latch onto. |
| 1393 | // - Update captureToPresentTime offset to be used for later frames. |
| 1394 | // - Example use cases: |
| 1395 | // - when frame rate drops down to below 20 fps, or |
| 1396 | // - A new streaming session starts (stopPreview followed by |
| 1397 | // startPreview) |
| 1398 | // |
Shuzhen Wang | 34a5e28 | 2022-06-17 14:48:35 -0700 | [diff] [blame] | 1399 | nsecs_t captureInterval = t - mLastCaptureTime; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1400 | if (captureInterval > kSpacingResetIntervalNs) { |
Rachel Lee | 86d90eb | 2023-04-25 14:37:34 -0700 | [diff] [blame] | 1401 | for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) { |
Shuzhen Wang | 34a5e28 | 2022-06-17 14:48:35 -0700 | [diff] [blame] | 1402 | const auto& timeline = vsyncEventData.frameTimelines[i]; |
| 1403 | if (timeline.deadlineTimestamp >= currentTime && |
| 1404 | timeline.expectedPresentationTime > minPresentT) { |
| 1405 | nsecs_t presentT = vsyncEventData.frameTimelines[i].expectedPresentationTime; |
| 1406 | mCaptureToPresentOffset = presentT - t; |
| 1407 | mLastCaptureTime = t; |
| 1408 | mLastPresentTime = presentT; |
| 1409 | |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1410 | // If releaseFence is available, store the fence to check signal |
| 1411 | // time later. |
| 1412 | mRefVsyncData = vsyncEventData; |
| 1413 | mReferenceCaptureTime = t; |
| 1414 | mReferenceArrivalTime = currentTime; |
Ravneet | 446b3bf | 2023-07-11 19:26:38 +0000 | [diff] [blame] | 1415 | if (releaseFence->isValid()) { |
| 1416 | mReferenceFrameFence = new Fence(releaseFence->dup()); |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1417 | } else { |
| 1418 | mFenceSignalOffset = 0; |
| 1419 | } |
| 1420 | |
Shuzhen Wang | 34a5e28 | 2022-06-17 14:48:35 -0700 | [diff] [blame] | 1421 | // Move the expected presentation time back by 1/3 of frame interval to |
| 1422 | // mitigate the time drift. Due to time drift, if we directly use the |
| 1423 | // expected presentation time, often times 2 expected presentation time |
| 1424 | // falls into the same VSYNC interval. |
| 1425 | return presentT - vsyncEventData.frameInterval/3; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1430 | // If there is a reference frame release fence, get the signal time and |
| 1431 | // update the captureToPresentOffset. |
| 1432 | if (mReferenceFrameFence != nullptr) { |
| 1433 | mFenceSignalOffset = 0; |
| 1434 | nsecs_t signalTime = mReferenceFrameFence->getSignalTime(); |
| 1435 | // Now that the fence has signaled, recalculate the offsets based on |
| 1436 | // the timeline which was actually latched |
| 1437 | if (signalTime != INT64_MAX) { |
| 1438 | for (size_t i = 0; i < mRefVsyncData.frameTimelinesLength; i++) { |
| 1439 | const auto& timeline = mRefVsyncData.frameTimelines[i]; |
| 1440 | if (timeline.deadlineTimestamp >= signalTime) { |
| 1441 | nsecs_t originalOffset = mCaptureToPresentOffset; |
| 1442 | mCaptureToPresentOffset = timeline.expectedPresentationTime |
| 1443 | - mReferenceCaptureTime; |
| 1444 | mLastPresentTime = timeline.expectedPresentationTime; |
| 1445 | mFenceSignalOffset = signalTime > mReferenceArrivalTime ? |
| 1446 | signalTime - mReferenceArrivalTime : 0; |
| 1447 | |
| 1448 | ALOGV("%s: Last deadline %" PRId64 " signalTime %" PRId64 |
| 1449 | " original offset %" PRId64 " new offset %" PRId64 |
| 1450 | " fencesignal offset %" PRId64, __FUNCTION__, |
| 1451 | timeline.deadlineTimestamp, signalTime, originalOffset, |
| 1452 | mCaptureToPresentOffset, mFenceSignalOffset); |
| 1453 | break; |
| 1454 | } |
| 1455 | } |
| 1456 | mReferenceFrameFence.clear(); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1460 | nsecs_t idealPresentT = t + mCaptureToPresentOffset; |
Shuzhen Wang | 14c62b8 | 2022-04-11 09:37:05 -0700 | [diff] [blame] | 1461 | nsecs_t expectedPresentT = mLastPresentTime; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1462 | nsecs_t minDiff = INT64_MAX; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1463 | |
| 1464 | // In fixed FPS case, when frame durations are close to multiples of display refresh |
| 1465 | // rate, derive minimum intervals between presentation times based on minimal |
Shuzhen Wang | 661b34f | 2022-05-18 22:00:19 -0700 | [diff] [blame] | 1466 | // expected duration. The minimum number of Vsyncs is: |
| 1467 | // - 0 if minFrameDuration in (0, 1.5] * vSyncInterval, |
| 1468 | // - 1 if minFrameDuration in (1.5, 2.5] * vSyncInterval, |
| 1469 | // - and so on. |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1470 | // |
| 1471 | // This spaces out the displaying of the frames so that the frame |
| 1472 | // presentations are roughly in sync with frame captures. |
Shuzhen Wang | 661b34f | 2022-05-18 22:00:19 -0700 | [diff] [blame] | 1473 | int minVsyncs = (mMinExpectedDuration - vsyncEventData.frameInterval / 2) / |
| 1474 | vsyncEventData.frameInterval; |
| 1475 | if (minVsyncs < 0) minVsyncs = 0; |
Shuzhen Wang | ed08fbe | 2022-06-21 01:00:50 -0700 | [diff] [blame] | 1476 | nsecs_t minInterval = minVsyncs * vsyncEventData.frameInterval; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1477 | |
| 1478 | // In fixed FPS case, if the frame duration deviates from multiples of |
| 1479 | // display refresh rate, find the closest Vsync without requiring a minimum |
| 1480 | // number of Vsync. |
| 1481 | // |
| 1482 | // Example: (24fps camera, 60hz refresh): |
| 1483 | // capture readout: | t1 | t1 | .. | t1 | .. | t1 | .. | t1 | |
| 1484 | // display VSYNC: | t2 | t2 | ... | t2 | ... | t2 | ... | t2 | |
| 1485 | // | : 1 frame |
| 1486 | // t1 : 41.67ms |
| 1487 | // t2 : 16.67ms |
| 1488 | // t1/t2 = 2.5 |
| 1489 | // |
| 1490 | // 24fps is a commonly used video frame rate. Because the capture |
| 1491 | // interval is 2.5 times of display refresh interval, the minVsyncs |
| 1492 | // calculation will directly fall at the boundary condition. In this case, |
| 1493 | // we should fall back to the basic logic of finding closest vsync |
| 1494 | // timestamp without worrying about minVsyncs. |
| 1495 | float captureToVsyncIntervalRatio = 1.0f * mMinExpectedDuration / vsyncEventData.frameInterval; |
| 1496 | float ratioDeviation = std::fabs( |
| 1497 | captureToVsyncIntervalRatio - std::roundf(captureToVsyncIntervalRatio)); |
| 1498 | bool captureDeviateFromVsync = ratioDeviation >= kMaxIntervalRatioDeviation; |
| 1499 | bool cameraDisplayInSync = (mFixedFps && !captureDeviateFromVsync); |
| 1500 | |
Shuzhen Wang | ed08fbe | 2022-06-21 01:00:50 -0700 | [diff] [blame] | 1501 | // Find best timestamp in the vsync timelines: |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1502 | // - Only use at most kMaxTimelines timelines to avoid long latency |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1503 | // - Add an extra timeline if display fence is used |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1504 | // - closest to the ideal presentation time, |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1505 | // - deadline timestamp is greater than the current time, and |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1506 | // - For fixed FPS, if the capture interval doesn't deviate too much from refresh interval, |
| 1507 | // the candidate presentation time is at least minInterval in the future compared to last |
| 1508 | // presentation time. |
| 1509 | // - For variable FPS, or if the capture interval deviates from refresh |
| 1510 | // interval for more than 5%, find a presentation time closest to the |
| 1511 | // (lastPresentationTime + captureToPresentOffset) instead. |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1512 | int fenceAdjustment = (mFenceSignalOffset > 0) ? 1 : 0; |
| 1513 | int maxTimelines = std::min(kMaxTimelines + fenceAdjustment, |
| 1514 | (int)vsyncEventData.frameTimelinesLength); |
Shuzhen Wang | ed08fbe | 2022-06-21 01:00:50 -0700 | [diff] [blame] | 1515 | float biasForShortDelay = 1.0f; |
| 1516 | for (int i = 0; i < maxTimelines; i ++) { |
| 1517 | const auto& vsyncTime = vsyncEventData.frameTimelines[i]; |
| 1518 | if (minVsyncs > 0) { |
| 1519 | // Bias towards using smaller timeline index: |
| 1520 | // i = 0: bias = 1 |
| 1521 | // i = maxTimelines-1: bias = -1 |
| 1522 | biasForShortDelay = 1.0 - 2.0 * i / (maxTimelines - 1); |
| 1523 | } |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1524 | if (std::abs(vsyncTime.expectedPresentationTime - idealPresentT) < minDiff && |
Shuzhen Wang | 0897d59 | 2023-04-07 12:48:05 -0700 | [diff] [blame] | 1525 | vsyncTime.deadlineTimestamp >= currentTime + mFenceSignalOffset && |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1526 | ((!cameraDisplayInSync && vsyncTime.expectedPresentationTime > minPresentT) || |
| 1527 | (cameraDisplayInSync && vsyncTime.expectedPresentationTime > |
Kwangkyu Park | 1c0042b | 2022-12-20 00:03:17 +0900 | [diff] [blame] | 1528 | mLastPresentTime + minInterval + |
| 1529 | static_cast<nsecs_t>(biasForShortDelay * kTimelineThresholdNs)))) { |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1530 | expectedPresentT = vsyncTime.expectedPresentationTime; |
| 1531 | minDiff = std::abs(vsyncTime.expectedPresentationTime - idealPresentT); |
| 1532 | } |
| 1533 | } |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1534 | |
Shuzhen Wang | 35bd355 | 2022-09-21 16:56:04 -0700 | [diff] [blame] | 1535 | if (expectedPresentT == mLastPresentTime && expectedPresentT < |
| 1536 | vsyncEventData.frameTimelines[maxTimelines-1].expectedPresentationTime) { |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1537 | // Couldn't find a reasonable presentation time. Using last frame's |
| 1538 | // presentation time would cause a frame drop. The best option now |
| 1539 | // is to use the next VSync as long as the last presentation time |
| 1540 | // doesn't already has the maximum latency, in which case dropping the |
| 1541 | // buffer is more desired than increasing latency. |
| 1542 | // |
| 1543 | // Example: (60fps camera, 59.9hz refresh): |
| 1544 | // capture readout: | t1 | t1 | .. | t1 | .. | t1 | .. | t1 | |
| 1545 | // \ \ \ \ \ \ \ \ \ |
| 1546 | // queue to BQ: | | | | | | | | | |
| 1547 | // \ \ \ \ \ \ \ \ \ |
| 1548 | // display VSYNC: | t2 | t2 | ... | t2 | ... | t2 | ... | t2 | |
| 1549 | // |
| 1550 | // |: 1 frame |
| 1551 | // t1 : 16.67ms |
| 1552 | // t2 : 16.69ms |
| 1553 | // |
| 1554 | // It takes 833 frames for capture readout count and display VSYNC count to be off |
| 1555 | // by 1. |
| 1556 | // - At frames [0, 832], presentationTime is set to timeline[0] |
| 1557 | // - At frames [833, 833*2-1], presentationTime is set to timeline[1] |
| 1558 | // - At frames [833*2, 833*3-1] presentationTime is set to timeline[2] |
| 1559 | // - At frame 833*3, no presentation time is found because we only |
| 1560 | // search for timeline[0..2]. |
| 1561 | // - Drop one buffer is better than further extend the presentation |
| 1562 | // time. |
| 1563 | // |
| 1564 | // However, if frame 833*2 arrives 16.67ms early (right after frame |
| 1565 | // 833*2-1), no presentation time can be found because |
| 1566 | // getLatestVsyncEventData is called early. In that case, it's better to |
| 1567 | // set presentation time by offseting last presentation time. |
| 1568 | expectedPresentT += vsyncEventData.frameInterval; |
| 1569 | } |
| 1570 | |
Shuzhen Wang | 14c62b8 | 2022-04-11 09:37:05 -0700 | [diff] [blame] | 1571 | mLastCaptureTime = t; |
| 1572 | mLastPresentTime = expectedPresentT; |
| 1573 | |
| 1574 | // Move the expected presentation time back by 1/3 of frame interval to |
| 1575 | // mitigate the time drift. Due to time drift, if we directly use the |
| 1576 | // expected presentation time, often times 2 expected presentation time |
| 1577 | // falls into the same VSYNC interval. |
| 1578 | return expectedPresentT - vsyncEventData.frameInterval/3; |
Shuzhen Wang | e4adddb | 2021-09-21 15:24:44 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Shuzhen Wang | ba92d77 | 2022-04-11 11:47:24 -0700 | [diff] [blame] | 1581 | bool Camera3OutputStream::shouldLogError(status_t res) { |
| 1582 | Mutex::Autolock l(mLock); |
| 1583 | return shouldLogError(res, mState); |
| 1584 | } |
| 1585 | |
Eino-Ville Talvala | fd58f1a | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1586 | }; // namespace camera3 |
| 1587 | |
| 1588 | }; // namespace android |