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