blob: 4824974396294eb983d2f4163aa2f8f9991326a1 [file] [log] [blame]
Igor Murashkine3a9f962013-05-08 18:03:15 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
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-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
32Camera3IOStreamBase::Camera3IOStreamBase(int id, camera3_stream_type_t type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080033 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -080034 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070035 Camera3Stream(id, type,
Zhijun He125684a2015-12-26 15:07:30 -080036 width, height, maxSize, format, dataSpace, rotation, setId),
Igor Murashkine3a9f962013-05-08 18:03:15 -070037 mTotalBufferCount(0),
Zhijun He6adc9cc2014-04-15 14:09:55 -070038 mHandoutTotalBufferCount(0),
39 mHandoutOutputBufferCount(0),
Igor Murashkine3a9f962013-05-08 18:03:15 -070040 mFrameCount(0),
41 mLastTimestamp(0) {
42
43 mCombinedFence = new Fence();
44
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080045 if (maxSize > 0 &&
46 (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070047 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
48 format);
49 mState = STATE_ERROR;
50 }
51}
52
53Camera3IOStreamBase::~Camera3IOStreamBase() {
54 disconnectLocked();
55}
56
57bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
58 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070059 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
60 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070061 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
62 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070063 return true;
64 }
65 return false;
66}
67
Igor Murashkine3a9f962013-05-08 18:03:15 -070068void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
69 (void) args;
70 String8 lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070071
72 uint32_t consumerUsage = 0;
73 status_t res = getEndpointUsage(&consumerUsage);
74 if (res != OK) consumerUsage = 0;
75
Igor Murashkine3a9f962013-05-08 18:03:15 -070076 lines.appendFormat(" State: %d\n", mState);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070077 lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070078 camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070079 camera3_stream::format, camera3_stream::data_space);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000080 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070081 lines.appendFormat(" Combined usage: %d, max HAL buffers: %d\n",
82 camera3_stream::usage | consumerUsage, camera3_stream::max_buffers);
Colin Crosse5729fa2014-03-21 15:04:25 -070083 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070084 mFrameCount, mLastTimestamp);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000085 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
Zhijun He6adc9cc2014-04-15 14:09:55 -070086 mTotalBufferCount, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -070087 write(fd, lines.string(), lines.size());
88}
89
90status_t Camera3IOStreamBase::configureQueueLocked() {
91 status_t res;
92
93 switch (mState) {
94 case STATE_IN_RECONFIG:
95 res = disconnectLocked();
96 if (res != OK) {
97 return res;
98 }
99 break;
100 case STATE_IN_CONFIG:
101 // OK
102 break;
103 default:
104 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
105 return INVALID_OPERATION;
106 }
107
108 return OK;
109}
110
111size_t Camera3IOStreamBase::getBufferCountLocked() {
112 return mTotalBufferCount;
113}
114
Zhijun He6adc9cc2014-04-15 14:09:55 -0700115size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() {
116 return mHandoutOutputBufferCount;
117}
118
119size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
120 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
121}
122
Igor Murashkine3a9f962013-05-08 18:03:15 -0700123status_t Camera3IOStreamBase::disconnectLocked() {
124 switch (mState) {
125 case STATE_IN_RECONFIG:
126 case STATE_CONFIGURED:
127 // OK
128 break;
129 default:
130 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700131 ALOGV("%s: Stream %d: Already disconnected",
132 __FUNCTION__, mId);
133 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700134 }
135
Zhijun He6adc9cc2014-04-15 14:09:55 -0700136 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700137 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700138 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700139 return INVALID_OPERATION;
140 }
141
142 return OK;
143}
144
145void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
146 buffer_handle_t *handle,
147 int acquireFence,
148 int releaseFence,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700149 camera3_buffer_status_t status,
150 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700151 /**
152 * Note that all fences are now owned by HAL.
153 */
154
155 // Handing out a raw pointer to this object. Increment internal refcount.
156 incStrong(this);
157 buffer.stream = this;
158 buffer.buffer = handle;
159 buffer.acquire_fence = acquireFence;
160 buffer.release_fence = releaseFence;
161 buffer.status = status;
162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700163 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700164 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700165 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700166 /**
167 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
168 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700169 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700170 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700171 sp<StatusTracker> statusTracker = mStatusTracker.promote();
172 if (statusTracker != 0) {
173 statusTracker->markComponentActive(mStatusId);
174 }
175 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700176 mHandoutTotalBufferCount++;
177
178 if (output) {
179 mHandoutOutputBufferCount++;
180 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700181}
182
183status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700184 // Allow dequeue during IN_[RE]CONFIG for registration, in
185 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700186 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700187 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
188 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700189 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
190 __FUNCTION__, mId, mState);
191 return INVALID_OPERATION;
192 }
193
Igor Murashkine3a9f962013-05-08 18:03:15 -0700194 return OK;
195}
196
197status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
198 // Allow buffers to be returned in the error state, to allow for disconnect
199 // and in the in-config states for registration
200 if (mState == STATE_CONSTRUCTED) {
201 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
202 __FUNCTION__, mId, mState);
203 return INVALID_OPERATION;
204 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700205 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700206 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
207 mId);
208 return INVALID_OPERATION;
209 }
210
211 return OK;
212}
213
214status_t Camera3IOStreamBase::returnAnyBufferLocked(
215 const camera3_stream_buffer &buffer,
216 nsecs_t timestamp,
217 bool output) {
218 status_t res;
219
220 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
221 // decrementing the internal refcount next. In case this is the last ref, we
222 // might get destructed on the decStrong(), so keep an sp around until the
223 // end of the call - otherwise have to sprinkle the decStrong on all exit
224 // points.
225 sp<Camera3IOStreamBase> keepAlive(this);
226 decStrong(this);
227
228 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
229 return res;
230 }
231
232 sp<Fence> releaseFence;
233 res = returnBufferCheckedLocked(buffer, timestamp, output,
234 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700235 // Res may be an error, but we still want to decrement our owned count
236 // to enable clean shutdown. So we'll just return the error but otherwise
237 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700238
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700239 if (releaseFence != 0) {
240 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
241 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700242
Zhijun He6adc9cc2014-04-15 14:09:55 -0700243 if (output) {
244 mHandoutOutputBufferCount--;
245 }
246
247 mHandoutTotalBufferCount--;
248 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700249 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700250 /**
251 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
252 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700253 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700254 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700255 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
256 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 sp<StatusTracker> statusTracker = mStatusTracker.promote();
258 if (statusTracker != 0) {
259 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
260 }
261 }
262
Igor Murashkine3a9f962013-05-08 18:03:15 -0700263 mBufferReturnedSignal.signal();
264
265 if (output) {
266 mLastTimestamp = timestamp;
267 }
268
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700269 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700270}
271
272
273
274}; // namespace camera3
275
276}; // namespace android