blob: 0204d491d110fd068b78a146c3facd184bedaade [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,
37 int setId, bool isMultiResolution) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070038 Camera3Stream(id, type,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080039 width, height, maxSize, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080040 physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution),
Igor Murashkine3a9f962013-05-08 18:03:15 -070041 mTotalBufferCount(0),
Zhijun He6adc9cc2014-04-15 14:09:55 -070042 mHandoutTotalBufferCount(0),
43 mHandoutOutputBufferCount(0),
Igor Murashkine3a9f962013-05-08 18:03:15 -070044 mFrameCount(0),
45 mLastTimestamp(0) {
46
47 mCombinedFence = new Fence();
48
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080049 if (maxSize > 0 &&
50 (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070051 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
52 format);
53 mState = STATE_ERROR;
54 }
55}
56
57Camera3IOStreamBase::~Camera3IOStreamBase() {
58 disconnectLocked();
59}
60
61bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
62 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070063 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
64 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070065 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
66 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070067 return true;
68 }
69 return false;
70}
71
Igor Murashkine3a9f962013-05-08 18:03:15 -070072void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
73 (void) args;
74 String8 lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070075
Emilian Peev050f5dc2017-05-18 14:43:56 +010076 uint64_t consumerUsage = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070077 status_t res = getEndpointUsage(&consumerUsage);
78 if (res != OK) consumerUsage = 0;
79
Igor Murashkine3a9f962013-05-08 18:03:15 -070080 lines.appendFormat(" State: %d\n", mState);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070081 lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Emilian Peevf4816702020-04-03 15:44:51 -070082 camera_stream::width, camera_stream::height,
83 camera_stream::format, camera_stream::data_space);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000084 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Emilian Peev050f5dc2017-05-18 14:43:56 +010085 lines.appendFormat(" Combined usage: %" PRIu64 ", max HAL buffers: %d\n",
Emilian Peevf4816702020-04-03 15:44:51 -070086 mUsage | consumerUsage, camera_stream::max_buffers);
87 if (strlen(camera_stream::physical_camera_id) > 0) {
88 lines.appendFormat(" Physical camera id: %s\n", camera_stream::physical_camera_id);
Shuzhen Wangc2cba122018-05-17 18:10:24 -070089 }
Colin Crosse5729fa2014-03-21 15:04:25 -070090 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070091 mFrameCount, mLastTimestamp);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000092 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
Zhijun He6adc9cc2014-04-15 14:09:55 -070093 mTotalBufferCount, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -070094 write(fd, lines.string(), lines.size());
Shuzhen Wang686f6442017-06-20 16:16:04 -070095
96 Camera3Stream::dump(fd, args);
Igor Murashkine3a9f962013-05-08 18:03:15 -070097}
98
99status_t Camera3IOStreamBase::configureQueueLocked() {
100 status_t res;
101
102 switch (mState) {
103 case STATE_IN_RECONFIG:
104 res = disconnectLocked();
105 if (res != OK) {
106 return res;
107 }
108 break;
109 case STATE_IN_CONFIG:
110 // OK
111 break;
112 default:
113 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
114 return INVALID_OPERATION;
115 }
116
117 return OK;
118}
119
120size_t Camera3IOStreamBase::getBufferCountLocked() {
121 return mTotalBufferCount;
122}
123
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700124size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700125 return mHandoutOutputBufferCount;
126}
127
128size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
129 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
130}
131
Igor Murashkine3a9f962013-05-08 18:03:15 -0700132status_t Camera3IOStreamBase::disconnectLocked() {
133 switch (mState) {
134 case STATE_IN_RECONFIG:
135 case STATE_CONFIGURED:
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700136 case STATE_ABANDONED:
Igor Murashkine3a9f962013-05-08 18:03:15 -0700137 // OK
138 break;
139 default:
140 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700141 ALOGV("%s: Stream %d: Already disconnected",
142 __FUNCTION__, mId);
143 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700144 }
145
Zhijun He6adc9cc2014-04-15 14:09:55 -0700146 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700147 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700148 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700149 return INVALID_OPERATION;
150 }
151
152 return OK;
153}
154
Emilian Peevf4816702020-04-03 15:44:51 -0700155void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700156 buffer_handle_t *handle,
157 int acquireFence,
158 int releaseFence,
Emilian Peevf4816702020-04-03 15:44:51 -0700159 camera_buffer_status_t status,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700160 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700161 /**
162 * Note that all fences are now owned by HAL.
163 */
164
165 // Handing out a raw pointer to this object. Increment internal refcount.
166 incStrong(this);
167 buffer.stream = this;
168 buffer.buffer = handle;
169 buffer.acquire_fence = acquireFence;
170 buffer.release_fence = releaseFence;
171 buffer.status = status;
172
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700173 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700174 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700175 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700176 /**
177 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
178 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700179 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700180 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700181 sp<StatusTracker> statusTracker = mStatusTracker.promote();
182 if (statusTracker != 0) {
183 statusTracker->markComponentActive(mStatusId);
184 }
185 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700186 mHandoutTotalBufferCount++;
187
188 if (output) {
189 mHandoutOutputBufferCount++;
190 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700191}
192
193status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700194 // Allow dequeue during IN_[RE]CONFIG for registration, in
195 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700196 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700197 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
198 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700199 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
200 __FUNCTION__, mId, mState);
201 return INVALID_OPERATION;
202 }
203
Igor Murashkine3a9f962013-05-08 18:03:15 -0700204 return OK;
205}
206
207status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
208 // Allow buffers to be returned in the error state, to allow for disconnect
209 // and in the in-config states for registration
210 if (mState == STATE_CONSTRUCTED) {
211 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
212 __FUNCTION__, mId, mState);
213 return INVALID_OPERATION;
214 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700215 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700216 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
217 mId);
218 return INVALID_OPERATION;
219 }
220
221 return OK;
222}
223
224status_t Camera3IOStreamBase::returnAnyBufferLocked(
Emilian Peevf4816702020-04-03 15:44:51 -0700225 const camera_stream_buffer &buffer,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700226 nsecs_t timestamp,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700227 bool output,
228 const std::vector<size_t>& surface_ids) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700229 status_t res;
230
231 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
232 // decrementing the internal refcount next. In case this is the last ref, we
233 // might get destructed on the decStrong(), so keep an sp around until the
234 // end of the call - otherwise have to sprinkle the decStrong on all exit
235 // points.
236 sp<Camera3IOStreamBase> keepAlive(this);
237 decStrong(this);
238
239 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
240 return res;
241 }
242
243 sp<Fence> releaseFence;
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700244 res = returnBufferCheckedLocked(buffer, timestamp, output, surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700245 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700246 // Res may be an error, but we still want to decrement our owned count
247 // to enable clean shutdown. So we'll just return the error but otherwise
248 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700249
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700250 if (releaseFence != 0) {
251 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
252 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700253
Zhijun He6adc9cc2014-04-15 14:09:55 -0700254 if (output) {
255 mHandoutOutputBufferCount--;
256 }
257
258 mHandoutTotalBufferCount--;
259 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700261 /**
262 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
263 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700264 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700265 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700266 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
267 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 sp<StatusTracker> statusTracker = mStatusTracker.promote();
269 if (statusTracker != 0) {
270 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
271 }
272 }
273
Igor Murashkine3a9f962013-05-08 18:03:15 -0700274 if (output) {
275 mLastTimestamp = timestamp;
276 }
277
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700278 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700279}
280
281
282
283}; // namespace camera3
284
285}; // namespace android