blob: 314e007b2c4653fba6cdad20534f4f7f99d99052 [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
Colin Crosse5729fa2014-03-21 15:04:25 -070021#include <inttypes.h>
Igor Murashkine3a9f962013-05-08 18:03:15 -070022
23#include <utils/Log.h>
24#include <utils/Trace.h>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070025#include "device3/Camera3IOStreamBase.h"
26#include "device3/StatusTracker.h"
Igor Murashkine3a9f962013-05-08 18:03:15 -070027
28namespace android {
29
30namespace camera3 {
31
Emilian Peevf4816702020-04-03 15:44:51 -070032Camera3IOStreamBase::Camera3IOStreamBase(int id, camera_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080033 uint32_t width, uint32_t height, size_t maxSize, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070034 android_dataspace dataSpace, camera_stream_rotation_t rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080035 const String8& physicalCameraId,
36 const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -080037 int setId, bool isMultiResolution, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wange4208922022-02-01 16:52:48 -080038 bool deviceTimeBaseIsRealtime, int timestampBase) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070039 Camera3Stream(id, type,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080040 width, height, maxSize, format, dataSpace, rotation,
Emilian Peev2295df72021-11-12 18:14:10 -080041 physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -080042 dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, timestampBase),
Igor Murashkine3a9f962013-05-08 18:03:15 -070043 mTotalBufferCount(0),
Shuzhen Wangc2352702022-09-06 18:36:31 -070044 mMaxCachedBufferCount(0),
Zhijun He6adc9cc2014-04-15 14:09:55 -070045 mHandoutTotalBufferCount(0),
46 mHandoutOutputBufferCount(0),
Shuzhen Wangc2352702022-09-06 18:36:31 -070047 mCachedOutputBufferCount(0),
Igor Murashkine3a9f962013-05-08 18:03:15 -070048 mFrameCount(0),
49 mLastTimestamp(0) {
50
51 mCombinedFence = new Fence();
52
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080053 if (maxSize > 0 &&
54 (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070055 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
56 format);
57 mState = STATE_ERROR;
58 }
59}
60
61Camera3IOStreamBase::~Camera3IOStreamBase() {
62 disconnectLocked();
63}
64
65bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
66 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070067 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
68 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070069 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
70 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070071 return true;
72 }
73 return false;
74}
75
Jing Mikec7f9b132023-03-12 11:12:04 +080076void Camera3IOStreamBase::dump(int fd, [[maybe_unused]] const Vector<String16> &args) const {
Igor Murashkine3a9f962013-05-08 18:03:15 -070077 String8 lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070078
Emilian Peev050f5dc2017-05-18 14:43:56 +010079 uint64_t consumerUsage = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070080 status_t res = getEndpointUsage(&consumerUsage);
81 if (res != OK) consumerUsage = 0;
82
Igor Murashkine3a9f962013-05-08 18:03:15 -070083 lines.appendFormat(" State: %d\n", mState);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070084 lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Emilian Peevf4816702020-04-03 15:44:51 -070085 camera_stream::width, camera_stream::height,
86 camera_stream::format, camera_stream::data_space);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000087 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Shuzhen Wang93b10792021-07-26 09:46:31 -070088 lines.appendFormat(" Combined usage: 0x%" PRIx64 ", max HAL buffers: %d\n",
Emilian Peevf4816702020-04-03 15:44:51 -070089 mUsage | consumerUsage, camera_stream::max_buffers);
90 if (strlen(camera_stream::physical_camera_id) > 0) {
91 lines.appendFormat(" Physical camera id: %s\n", camera_stream::physical_camera_id);
Shuzhen Wangc2cba122018-05-17 18:10:24 -070092 }
Shuzhen Wangfe8a2a32022-05-10 18:18:54 -070093 lines.appendFormat(" Dynamic Range Profile: 0x%" PRIx64 "\n",
Emilian Peevc81a7592022-02-14 17:38:18 -080094 camera_stream::dynamic_range_profile);
Shuzhen Wang8ed1e872022-03-08 16:34:33 -080095 lines.appendFormat(" Stream use case: %" PRId64 "\n", camera_stream::use_case);
Shuzhen Wangfe8a2a32022-05-10 18:18:54 -070096 lines.appendFormat(" Timestamp base: %d\n", getTimestampBase());
Colin Crosse5729fa2014-03-21 15:04:25 -070097 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070098 mFrameCount, mLastTimestamp);
Shuzhen Wangc2352702022-09-06 18:36:31 -070099 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu, currently cached: %zu\n",
100 mTotalBufferCount, mHandoutTotalBufferCount, mCachedOutputBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700101 write(fd, lines.string(), lines.size());
Shuzhen Wang686f6442017-06-20 16:16:04 -0700102
103 Camera3Stream::dump(fd, args);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700104}
105
106status_t Camera3IOStreamBase::configureQueueLocked() {
107 status_t res;
108
109 switch (mState) {
110 case STATE_IN_RECONFIG:
111 res = disconnectLocked();
112 if (res != OK) {
113 return res;
114 }
115 break;
116 case STATE_IN_CONFIG:
117 // OK
118 break;
119 default:
120 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
121 return INVALID_OPERATION;
122 }
123
124 return OK;
125}
126
127size_t Camera3IOStreamBase::getBufferCountLocked() {
128 return mTotalBufferCount;
129}
130
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700131size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700132 return mHandoutOutputBufferCount;
133}
134
135size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
136 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
137}
138
Shuzhen Wangc2352702022-09-06 18:36:31 -0700139size_t Camera3IOStreamBase::getCachedOutputBufferCountLocked() const {
140 return mCachedOutputBufferCount;
141}
142
143size_t Camera3IOStreamBase::getMaxCachedOutputBuffersLocked() const {
144 return mMaxCachedBufferCount;
145}
146
Igor Murashkine3a9f962013-05-08 18:03:15 -0700147status_t Camera3IOStreamBase::disconnectLocked() {
148 switch (mState) {
149 case STATE_IN_RECONFIG:
150 case STATE_CONFIGURED:
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700151 case STATE_ABANDONED:
Igor Murashkine3a9f962013-05-08 18:03:15 -0700152 // OK
153 break;
154 default:
155 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700156 ALOGV("%s: Stream %d: Already disconnected",
157 __FUNCTION__, mId);
158 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700159 }
160
Zhijun He6adc9cc2014-04-15 14:09:55 -0700161 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700162 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700163 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700164 return INVALID_OPERATION;
165 }
166
167 return OK;
168}
169
Emilian Peevf4816702020-04-03 15:44:51 -0700170void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700171 buffer_handle_t *handle,
172 int acquireFence,
173 int releaseFence,
Emilian Peevf4816702020-04-03 15:44:51 -0700174 camera_buffer_status_t status,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700175 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700176 /**
177 * Note that all fences are now owned by HAL.
178 */
179
180 // Handing out a raw pointer to this object. Increment internal refcount.
181 incStrong(this);
182 buffer.stream = this;
183 buffer.buffer = handle;
184 buffer.acquire_fence = acquireFence;
185 buffer.release_fence = releaseFence;
186 buffer.status = status;
187
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700188 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700189 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700190 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700191 /**
192 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
193 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700194 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700195 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700196 sp<StatusTracker> statusTracker = mStatusTracker.promote();
197 if (statusTracker != 0) {
198 statusTracker->markComponentActive(mStatusId);
199 }
200 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700201 mHandoutTotalBufferCount++;
202
203 if (output) {
204 mHandoutOutputBufferCount++;
205 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700206}
207
208status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700209 // Allow dequeue during IN_[RE]CONFIG for registration, in
210 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700211 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700212 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
213 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700214 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
215 __FUNCTION__, mId, mState);
216 return INVALID_OPERATION;
217 }
218
Igor Murashkine3a9f962013-05-08 18:03:15 -0700219 return OK;
220}
221
222status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
223 // Allow buffers to be returned in the error state, to allow for disconnect
224 // and in the in-config states for registration
225 if (mState == STATE_CONSTRUCTED) {
226 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
227 __FUNCTION__, mId, mState);
228 return INVALID_OPERATION;
229 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700230 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700231 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
232 mId);
233 return INVALID_OPERATION;
234 }
235
236 return OK;
237}
238
239status_t Camera3IOStreamBase::returnAnyBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700240 const camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700241 nsecs_t timestamp,
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700242 nsecs_t readoutTimestamp,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700243 bool output,
Emilian Peev5104fe92021-10-21 14:27:09 -0700244 int32_t transform,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700245 const std::vector<size_t>& surface_ids) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700246 status_t res;
247
248 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
249 // decrementing the internal refcount next. In case this is the last ref, we
250 // might get destructed on the decStrong(), so keep an sp around until the
251 // end of the call - otherwise have to sprinkle the decStrong on all exit
252 // points.
253 sp<Camera3IOStreamBase> keepAlive(this);
254 decStrong(this);
255
256 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
257 return res;
258 }
259
260 sp<Fence> releaseFence;
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700261 res = returnBufferCheckedLocked(buffer, timestamp, readoutTimestamp,
262 output, transform, surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700263 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700264 // Res may be an error, but we still want to decrement our owned count
265 // to enable clean shutdown. So we'll just return the error but otherwise
266 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700267
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700268 if (releaseFence != 0) {
269 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
270 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700271
Zhijun He6adc9cc2014-04-15 14:09:55 -0700272 if (output) {
273 mHandoutOutputBufferCount--;
274 }
275
276 mHandoutTotalBufferCount--;
277 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700278 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700279 /**
280 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
281 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700282 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700283 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700284 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
285 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 sp<StatusTracker> statusTracker = mStatusTracker.promote();
287 if (statusTracker != 0) {
288 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
289 }
290 }
291
Igor Murashkine3a9f962013-05-08 18:03:15 -0700292 if (output) {
293 mLastTimestamp = timestamp;
294 }
295
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700296 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700297}
298
299
300
301}; // namespace camera3
302
303}; // namespace android