blob: add1483bf844fe79948149e7089b80a8eb151d07 [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),
Zhijun He6adc9cc2014-04-15 14:09:55 -070044 mHandoutTotalBufferCount(0),
45 mHandoutOutputBufferCount(0),
Igor Murashkine3a9f962013-05-08 18:03:15 -070046 mFrameCount(0),
47 mLastTimestamp(0) {
48
49 mCombinedFence = new Fence();
50
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080051 if (maxSize > 0 &&
52 (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070053 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
54 format);
55 mState = STATE_ERROR;
56 }
57}
58
59Camera3IOStreamBase::~Camera3IOStreamBase() {
60 disconnectLocked();
61}
62
63bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
64 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070065 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
66 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070067 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
68 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070069 return true;
70 }
71 return false;
72}
73
Igor Murashkine3a9f962013-05-08 18:03:15 -070074void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
75 (void) args;
76 String8 lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070077
Emilian Peev050f5dc2017-05-18 14:43:56 +010078 uint64_t consumerUsage = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070079 status_t res = getEndpointUsage(&consumerUsage);
80 if (res != OK) consumerUsage = 0;
81
Igor Murashkine3a9f962013-05-08 18:03:15 -070082 lines.appendFormat(" State: %d\n", mState);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070083 lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Emilian Peevf4816702020-04-03 15:44:51 -070084 camera_stream::width, camera_stream::height,
85 camera_stream::format, camera_stream::data_space);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000086 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Shuzhen Wang93b10792021-07-26 09:46:31 -070087 lines.appendFormat(" Combined usage: 0x%" PRIx64 ", max HAL buffers: %d\n",
Emilian Peevf4816702020-04-03 15:44:51 -070088 mUsage | consumerUsage, camera_stream::max_buffers);
89 if (strlen(camera_stream::physical_camera_id) > 0) {
90 lines.appendFormat(" Physical camera id: %s\n", camera_stream::physical_camera_id);
Shuzhen Wangc2cba122018-05-17 18:10:24 -070091 }
Shuzhen Wangfe8a2a32022-05-10 18:18:54 -070092 lines.appendFormat(" Dynamic Range Profile: 0x%" PRIx64 "\n",
Emilian Peevc81a7592022-02-14 17:38:18 -080093 camera_stream::dynamic_range_profile);
Shuzhen Wang8ed1e872022-03-08 16:34:33 -080094 lines.appendFormat(" Stream use case: %" PRId64 "\n", camera_stream::use_case);
Shuzhen Wangfe8a2a32022-05-10 18:18:54 -070095 lines.appendFormat(" Timestamp base: %d\n", getTimestampBase());
Colin Crosse5729fa2014-03-21 15:04:25 -070096 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070097 mFrameCount, mLastTimestamp);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000098 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
Zhijun He6adc9cc2014-04-15 14:09:55 -070099 mTotalBufferCount, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700100 write(fd, lines.string(), lines.size());
Shuzhen Wang686f6442017-06-20 16:16:04 -0700101
102 Camera3Stream::dump(fd, args);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700103}
104
105status_t Camera3IOStreamBase::configureQueueLocked() {
106 status_t res;
107
108 switch (mState) {
109 case STATE_IN_RECONFIG:
110 res = disconnectLocked();
111 if (res != OK) {
112 return res;
113 }
114 break;
115 case STATE_IN_CONFIG:
116 // OK
117 break;
118 default:
119 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
120 return INVALID_OPERATION;
121 }
122
123 return OK;
124}
125
126size_t Camera3IOStreamBase::getBufferCountLocked() {
127 return mTotalBufferCount;
128}
129
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700130size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700131 return mHandoutOutputBufferCount;
132}
133
134size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
135 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
136}
137
Igor Murashkine3a9f962013-05-08 18:03:15 -0700138status_t Camera3IOStreamBase::disconnectLocked() {
139 switch (mState) {
140 case STATE_IN_RECONFIG:
141 case STATE_CONFIGURED:
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700142 case STATE_ABANDONED:
Igor Murashkine3a9f962013-05-08 18:03:15 -0700143 // OK
144 break;
145 default:
146 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700147 ALOGV("%s: Stream %d: Already disconnected",
148 __FUNCTION__, mId);
149 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700150 }
151
Zhijun He6adc9cc2014-04-15 14:09:55 -0700152 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700153 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700154 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700155 return INVALID_OPERATION;
156 }
157
158 return OK;
159}
160
Emilian Peevf4816702020-04-03 15:44:51 -0700161void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700162 buffer_handle_t *handle,
163 int acquireFence,
164 int releaseFence,
Emilian Peevf4816702020-04-03 15:44:51 -0700165 camera_buffer_status_t status,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700166 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700167 /**
168 * Note that all fences are now owned by HAL.
169 */
170
171 // Handing out a raw pointer to this object. Increment internal refcount.
172 incStrong(this);
173 buffer.stream = this;
174 buffer.buffer = handle;
175 buffer.acquire_fence = acquireFence;
176 buffer.release_fence = releaseFence;
177 buffer.status = status;
178
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700180 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700181 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700182 /**
183 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
184 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700185 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700186 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187 sp<StatusTracker> statusTracker = mStatusTracker.promote();
188 if (statusTracker != 0) {
189 statusTracker->markComponentActive(mStatusId);
190 }
191 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700192 mHandoutTotalBufferCount++;
193
194 if (output) {
195 mHandoutOutputBufferCount++;
196 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700197}
198
199status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700200 // Allow dequeue during IN_[RE]CONFIG for registration, in
201 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700202 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700203 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
204 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700205 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
206 __FUNCTION__, mId, mState);
207 return INVALID_OPERATION;
208 }
209
Igor Murashkine3a9f962013-05-08 18:03:15 -0700210 return OK;
211}
212
213status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
214 // Allow buffers to be returned in the error state, to allow for disconnect
215 // and in the in-config states for registration
216 if (mState == STATE_CONSTRUCTED) {
217 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
218 __FUNCTION__, mId, mState);
219 return INVALID_OPERATION;
220 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700221 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700222 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
223 mId);
224 return INVALID_OPERATION;
225 }
226
227 return OK;
228}
229
230status_t Camera3IOStreamBase::returnAnyBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700231 const camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700232 nsecs_t timestamp,
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700233 nsecs_t readoutTimestamp,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700234 bool output,
Emilian Peev5104fe92021-10-21 14:27:09 -0700235 int32_t transform,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700236 const std::vector<size_t>& surface_ids) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700237 status_t res;
238
239 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
240 // decrementing the internal refcount next. In case this is the last ref, we
241 // might get destructed on the decStrong(), so keep an sp around until the
242 // end of the call - otherwise have to sprinkle the decStrong on all exit
243 // points.
244 sp<Camera3IOStreamBase> keepAlive(this);
245 decStrong(this);
246
247 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
248 return res;
249 }
250
251 sp<Fence> releaseFence;
Shuzhen Wang90708ea2021-11-04 11:40:49 -0700252 res = returnBufferCheckedLocked(buffer, timestamp, readoutTimestamp,
253 output, transform, surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700254 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700255 // Res may be an error, but we still want to decrement our owned count
256 // to enable clean shutdown. So we'll just return the error but otherwise
257 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700258
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700259 if (releaseFence != 0) {
260 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
261 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700262
Zhijun He6adc9cc2014-04-15 14:09:55 -0700263 if (output) {
264 mHandoutOutputBufferCount--;
265 }
266
267 mHandoutTotalBufferCount--;
268 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700269 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700270 /**
271 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
272 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700273 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700274 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700275 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
276 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 sp<StatusTracker> statusTracker = mStatusTracker.promote();
278 if (statusTracker != 0) {
279 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
280 }
281 }
282
Igor Murashkine3a9f962013-05-08 18:03:15 -0700283 if (output) {
284 mLastTimestamp = timestamp;
285 }
286
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700287 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700288}
289
290
291
292}; // namespace camera3
293
294}; // namespace android