blob: 23b1c45811c3bd6c3d17b4a8de8cbdfe059c5be2 [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,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070034 android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070035 Camera3Stream(id, type,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070036 width, height, maxSize, format, dataSpace, rotation),
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
45 if (maxSize > 0 && format != HAL_PIXEL_FORMAT_BLOB) {
46 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
47 format);
48 mState = STATE_ERROR;
49 }
50}
51
52Camera3IOStreamBase::~Camera3IOStreamBase() {
53 disconnectLocked();
54}
55
56bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
57 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070058 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
59 " buffer signal time is %" PRId64,
Zhijun He6adc9cc2014-04-15 14:09:55 -070060 __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
61 if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
Igor Murashkine3a9f962013-05-08 18:03:15 -070062 return true;
63 }
64 return false;
65}
66
Igor Murashkine3a9f962013-05-08 18:03:15 -070067void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
68 (void) args;
69 String8 lines;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070070
71 uint32_t consumerUsage = 0;
72 status_t res = getEndpointUsage(&consumerUsage);
73 if (res != OK) consumerUsage = 0;
74
Igor Murashkine3a9f962013-05-08 18:03:15 -070075 lines.appendFormat(" State: %d\n", mState);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070076 lines.appendFormat(" Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070077 camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070078 camera3_stream::format, camera3_stream::data_space);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000079 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070080 lines.appendFormat(" Combined usage: %d, max HAL buffers: %d\n",
81 camera3_stream::usage | consumerUsage, camera3_stream::max_buffers);
Colin Crosse5729fa2014-03-21 15:04:25 -070082 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070083 mFrameCount, mLastTimestamp);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000084 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
Zhijun He6adc9cc2014-04-15 14:09:55 -070085 mTotalBufferCount, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -070086 write(fd, lines.string(), lines.size());
87}
88
89status_t Camera3IOStreamBase::configureQueueLocked() {
90 status_t res;
91
92 switch (mState) {
93 case STATE_IN_RECONFIG:
94 res = disconnectLocked();
95 if (res != OK) {
96 return res;
97 }
98 break;
99 case STATE_IN_CONFIG:
100 // OK
101 break;
102 default:
103 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
104 return INVALID_OPERATION;
105 }
106
107 return OK;
108}
109
110size_t Camera3IOStreamBase::getBufferCountLocked() {
111 return mTotalBufferCount;
112}
113
Zhijun He6adc9cc2014-04-15 14:09:55 -0700114size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() {
115 return mHandoutOutputBufferCount;
116}
117
118size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
119 return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
120}
121
Igor Murashkine3a9f962013-05-08 18:03:15 -0700122status_t Camera3IOStreamBase::disconnectLocked() {
123 switch (mState) {
124 case STATE_IN_RECONFIG:
125 case STATE_CONFIGURED:
126 // OK
127 break;
128 default:
129 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700130 ALOGV("%s: Stream %d: Already disconnected",
131 __FUNCTION__, mId);
132 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700133 }
134
Zhijun He6adc9cc2014-04-15 14:09:55 -0700135 if (mHandoutTotalBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700136 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Zhijun He6adc9cc2014-04-15 14:09:55 -0700137 __FUNCTION__, mHandoutTotalBufferCount);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700138 return INVALID_OPERATION;
139 }
140
141 return OK;
142}
143
144void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
145 buffer_handle_t *handle,
146 int acquireFence,
147 int releaseFence,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700148 camera3_buffer_status_t status,
149 bool output) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700150 /**
151 * Note that all fences are now owned by HAL.
152 */
153
154 // Handing out a raw pointer to this object. Increment internal refcount.
155 incStrong(this);
156 buffer.stream = this;
157 buffer.buffer = handle;
158 buffer.acquire_fence = acquireFence;
159 buffer.release_fence = releaseFence;
160 buffer.status = status;
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 // Inform tracker about becoming busy
Zhijun He6adc9cc2014-04-15 14:09:55 -0700163 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700164 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700165 /**
166 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
167 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700168 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700169 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700170 sp<StatusTracker> statusTracker = mStatusTracker.promote();
171 if (statusTracker != 0) {
172 statusTracker->markComponentActive(mStatusId);
173 }
174 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700175 mHandoutTotalBufferCount++;
176
177 if (output) {
178 mHandoutOutputBufferCount++;
179 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700180}
181
182status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700183 // Allow dequeue during IN_[RE]CONFIG for registration, in
184 // PREPARING for pre-allocation
Igor Murashkine3a9f962013-05-08 18:03:15 -0700185 if (mState != STATE_CONFIGURED &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700186 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
187 mState != STATE_PREPARING) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700188 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
189 __FUNCTION__, mId, mState);
190 return INVALID_OPERATION;
191 }
192
Igor Murashkine3a9f962013-05-08 18:03:15 -0700193 return OK;
194}
195
196status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
197 // Allow buffers to be returned in the error state, to allow for disconnect
198 // and in the in-config states for registration
199 if (mState == STATE_CONSTRUCTED) {
200 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
201 __FUNCTION__, mId, mState);
202 return INVALID_OPERATION;
203 }
Zhijun He6adc9cc2014-04-15 14:09:55 -0700204 if (mHandoutTotalBufferCount == 0) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700205 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
206 mId);
207 return INVALID_OPERATION;
208 }
209
210 return OK;
211}
212
213status_t Camera3IOStreamBase::returnAnyBufferLocked(
214 const camera3_stream_buffer &buffer,
215 nsecs_t timestamp,
216 bool output) {
217 status_t res;
218
219 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
220 // decrementing the internal refcount next. In case this is the last ref, we
221 // might get destructed on the decStrong(), so keep an sp around until the
222 // end of the call - otherwise have to sprinkle the decStrong on all exit
223 // points.
224 sp<Camera3IOStreamBase> keepAlive(this);
225 decStrong(this);
226
227 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
228 return res;
229 }
230
231 sp<Fence> releaseFence;
232 res = returnBufferCheckedLocked(buffer, timestamp, output,
233 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700234 // Res may be an error, but we still want to decrement our owned count
235 // to enable clean shutdown. So we'll just return the error but otherwise
236 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700237
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700238 if (releaseFence != 0) {
239 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
240 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700241
Zhijun He6adc9cc2014-04-15 14:09:55 -0700242 if (output) {
243 mHandoutOutputBufferCount--;
244 }
245
246 mHandoutTotalBufferCount--;
247 if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700248 mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700249 /**
250 * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
251 * before/after register_stream_buffers during initial configuration
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700252 * or re-configuration, or during prepare pre-allocation
Igor Murashkin13d315e2014-04-03 18:09:04 -0700253 */
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700254 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
255 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256 sp<StatusTracker> statusTracker = mStatusTracker.promote();
257 if (statusTracker != 0) {
258 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
259 }
260 }
261
Igor Murashkine3a9f962013-05-08 18:03:15 -0700262 mBufferReturnedSignal.signal();
263
264 if (output) {
265 mLastTimestamp = timestamp;
266 }
267
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700268 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700269}
270
271
272
273}; // namespace camera3
274
275}; // namespace android