blob: 7d08089a448bab8b794b0fefbb75de441cce81b9 [file] [log] [blame]
Igor Murashkine3a9f962013-05-08 18:03:15 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine3a9f962013-05-08 18:03:15 -07003 *
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-IOStreamBase"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Austin Borger0fb3ad92023-06-01 16:51:35 -070021#include <sstream>
22
Colin Crosse5729fa2014-03-21 15:04:25 -070023#include <inttypes.h>
Igor Murashkine3a9f962013-05-08 18:03:15 -070024
25#include <utils/Log.h>
26#include <utils/Trace.h>
Austin Borger0fb3ad92023-06-01 16:51:35 -070027#include <camera/StringUtils.h>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070028#include "device3/Camera3IOStreamBase.h"
29#include "device3/StatusTracker.h"
Igor Murashkine3a9f962013-05-08 18:03:15 -070030
31namespace android {
32
33namespace camera3 {
34
Emilian Peevf4816702020-04-03 15:44:51 -070035Camera3IOStreamBase::Camera3IOStreamBase(int id, camera_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, size_t maxSize, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070037 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Austin Borger0fb3ad92023-06-01 16:51:35 -070038 const std::string& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080039 const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -080040 int setId, bool isMultiResolution, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wange4208922022-02-01 16:52:48 -080041 bool deviceTimeBaseIsRealtime, int timestampBase) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070042 Camera3Stream(id, type,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080043 width, height, maxSize, format, dataSpace, rotation,
Emilian Peev2295df72021-11-12 18:14:10 -080044 physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -080045 dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, timestampBase),
Igor Murashkine3a9f962013-05-08 18:03:15 -070046 mTotalBufferCount(0),
Shuzhen Wangc2352702022-09-06 18:36:31 -070047 mMaxCachedBufferCount(0),
Zhijun He6adc9cc2014-04-15 14:09:55 -070048 mHandoutTotalBufferCount(0),
49 mHandoutOutputBufferCount(0),
Shuzhen Wangc2352702022-09-06 18:36:31 -070050 mCachedOutputBufferCount(0),
Igor Murashkine3a9f962013-05-08 18:03:15 -070051 mFrameCount(0),
52 mLastTimestamp(0) {
53
54 mCombinedFence = new Fence();
55
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080056 if (maxSize > 0 &&
57 (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070058 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
59 format);
60 mState = STATE_ERROR;
61 }
62}
63
64Camera3IOStreamBase::~Camera3IOStreamBase() {
65 disconnectLocked();
66}
67
68bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
69 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070070 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
71 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070072 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
73 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070074 return true;
75 }
76 return false;
77}
78
Jing Mikec7f9b132023-03-12 11:12:04 +080079void Camera3IOStreamBase::dump(int fd, [[maybe_unused]] const Vector<String16> &args) const {
Austin Borger0fb3ad92023-06-01 16:51:35 -070080 std::ostringstream lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070081
Emilian Peev050f5dc2017-05-18 14:43:56 +010082 uint64_t consumerUsage = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070083 status_t res = getEndpointUsage(&consumerUsage);
84 if (res != OK) consumerUsage = 0;
85
Austin Borger0fb3ad92023-06-01 16:51:35 -070086 lines << fmt::sprintf(" State: %d\n", mState);
87 lines << fmt::sprintf(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Emilian Peevf4816702020-04-03 15:44:51 -070088 camera_stream::width, camera_stream::height,
89 camera_stream::format, camera_stream::data_space);
Austin Borger0fb3ad92023-06-01 16:51:35 -070090 lines << fmt::sprintf(" Max size: %zu\n", mMaxSize);
91 lines << fmt::sprintf(" Combined usage: 0x%" PRIx64 ", max HAL buffers: %d\n",
Emilian Peevf4816702020-04-03 15:44:51 -070092 mUsage | consumerUsage, camera_stream::max_buffers);
Austin Borger0fb3ad92023-06-01 16:51:35 -070093 if (!camera_stream::physical_camera_id.empty()) {
94 lines << " Physical camera id: " << camera_stream::physical_camera_id << "\n";
Shuzhen Wangc2cba122018-05-17 18:10:24 -070095 }
Austin Borger0fb3ad92023-06-01 16:51:35 -070096 lines << fmt::sprintf(" Dynamic Range Profile: 0x%" PRIx64 "\n",
Emilian Peevc81a7592022-02-14 17:38:18 -080097 camera_stream::dynamic_range_profile);
Austin Borger0fb3ad92023-06-01 16:51:35 -070098 lines << fmt::sprintf(" Stream use case: %" PRId64 "\n", camera_stream::use_case);
99 lines << fmt::sprintf(" Timestamp base: %d\n", getTimestampBase());
100 lines << fmt::sprintf(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -0700101 mFrameCount, mLastTimestamp);
Austin Borger0fb3ad92023-06-01 16:51:35 -0700102 lines << fmt::sprintf(" Total buffers: %zu, currently dequeued: %zu, "
103 "currently cached: %zu\n", mTotalBufferCount, mHandoutTotalBufferCount,
104 mCachedOutputBufferCount);
105 std::string linesStr = std::move(lines.str());
106 write(fd, linesStr.c_str(), linesStr.size());
Shuzhen Wang686f6442017-06-20 16:16:04 -0700107
108 Camera3Stream::dump(fd, args);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700109}
110
111status_t Camera3IOStreamBase::configureQueueLocked() {
112 status_t res;
113
114 switch (mState) {
115 case STATE_IN_RECONFIG:
116 res = disconnectLocked();
117 if (res != OK) {
118 return res;
119 }
120 break;
121 case STATE_IN_CONFIG:
122 // OK
123 break;
124 default:
125 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
126 return INVALID_OPERATION;
127 }
128
129 return OK;
130}
131
132size_t Camera3IOStreamBase::getBufferCountLocked() {
133 return mTotalBufferCount;
134}
135
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700136size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700137 return mHandoutOutputBufferCount;
138}
139
140size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
141 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
142}
143
Shuzhen Wangc2352702022-09-06 18:36:31 -0700144size_t Camera3IOStreamBase::getCachedOutputBufferCountLocked() const {
145 return mCachedOutputBufferCount;
146}
147
148size_t Camera3IOStreamBase::getMaxCachedOutputBuffersLocked() const {
149 return mMaxCachedBufferCount;
150}
151
Igor Murashkine3a9f962013-05-08 18:03:15 -0700152status_t Camera3IOStreamBase::disconnectLocked() {
153 switch (mState) {
154 case STATE_IN_RECONFIG:
155 case STATE_CONFIGURED:
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700156 case STATE_ABANDONED:
Igor Murashkine3a9f962013-05-08 18:03:15 -0700157 // OK
158 break;
159 default:
160 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700161 ALOGV("%s: Stream %d: Already disconnected",
162 __FUNCTION__, mId);
163 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700164 }
165
Zhijun He6adc9cc2014-04-15 14:09:55 -0700166 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700167 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700168 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700169 return INVALID_OPERATION;
170 }
171
172 return OK;
173}
174
Emilian Peevf4816702020-04-03 15:44:51 -0700175void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700176 buffer_handle_t *handle,
177 int acquireFence,
178 int releaseFence,
Emilian Peevf4816702020-04-03 15:44:51 -0700179 camera_buffer_status_t status,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700180 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700181 /**
182 * Note that all fences are now owned by HAL.
183 */
184
185 // Handing out a raw pointer to this object. Increment internal refcount.
186 incStrong(this);
187 buffer.stream = this;
188 buffer.buffer = handle;
189 buffer.acquire_fence = acquireFence;
190 buffer.release_fence = releaseFence;
191 buffer.status = status;
192
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700193 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700194 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700196 /**
197 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
198 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700199 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700200 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 sp<StatusTracker> statusTracker = mStatusTracker.promote();
202 if (statusTracker != 0) {
203 statusTracker->markComponentActive(mStatusId);
204 }
205 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700206 mHandoutTotalBufferCount++;
207
208 if (output) {
209 mHandoutOutputBufferCount++;
210 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700211}
212
213status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700214 // Allow dequeue during IN_[RE]CONFIG for registration, in
215 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700216 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700217 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
218 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700219 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
220 __FUNCTION__, mId, mState);
221 return INVALID_OPERATION;
222 }
223
Igor Murashkine3a9f962013-05-08 18:03:15 -0700224 return OK;
225}
226
227status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
228 // Allow buffers to be returned in the error state, to allow for disconnect
229 // and in the in-config states for registration
230 if (mState == STATE_CONSTRUCTED) {
231 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
232 __FUNCTION__, mId, mState);
233 return INVALID_OPERATION;
234 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700235 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700236 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
237 mId);
238 return INVALID_OPERATION;
239 }
240
241 return OK;
242}
243
244status_t Camera3IOStreamBase::returnAnyBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700245 const camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700246 nsecs_t timestamp,
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700247 nsecs_t readoutTimestamp,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700248 bool output,
Emilian Peev5104fe92021-10-21 14:27:09 -0700249 int32_t transform,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700250 const std::vector<size_t>& surface_ids) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700251 status_t res;
252
253 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
254 // decrementing the internal refcount next. In case this is the last ref, we
255 // might get destructed on the decStrong(), so keep an sp around until the
256 // end of the call - otherwise have to sprinkle the decStrong on all exit
257 // points.
258 sp<Camera3IOStreamBase> keepAlive(this);
259 decStrong(this);
260
261 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
262 return res;
263 }
264
265 sp<Fence> releaseFence;
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700266 res = returnBufferCheckedLocked(buffer, timestamp, readoutTimestamp,
267 output, transform, surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700268 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700269 // Res may be an error, but we still want to decrement our owned count
270 // to enable clean shutdown. So we'll just return the error but otherwise
271 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700272
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700273 if (releaseFence != 0) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700274 mCombinedFence = Fence::merge(toString8(mName), mCombinedFence, releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700275 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700276
Zhijun He6adc9cc2014-04-15 14:09:55 -0700277 if (output) {
278 mHandoutOutputBufferCount--;
279 }
280
281 mHandoutTotalBufferCount--;
282 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700283 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700284 /**
285 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
286 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700287 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700288 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700289 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
290 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 sp<StatusTracker> statusTracker = mStatusTracker.promote();
292 if (statusTracker != 0) {
293 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
294 }
295 }
296
Igor Murashkine3a9f962013-05-08 18:03:15 -0700297 if (output) {
298 mLastTimestamp = timestamp;
299 }
300
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700301 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700302}
303
304
305
306}; // namespace camera3
307
308}; // namespace android