blob: d662cc2c86864becc749a0f7421e370032d37370 [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,
33 uint32_t width, uint32_t height, size_t maxSize, int format) :
34 Camera3Stream(id, type,
35 width, height, maxSize, format),
36 mTotalBufferCount(0),
37 mDequeuedBufferCount(0),
38 mFrameCount(0),
39 mLastTimestamp(0) {
40
41 mCombinedFence = new Fence();
42
43 if (maxSize > 0 && format != HAL_PIXEL_FORMAT_BLOB) {
44 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
45 format);
46 mState = STATE_ERROR;
47 }
48}
49
50Camera3IOStreamBase::~Camera3IOStreamBase() {
51 disconnectLocked();
52}
53
54bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
55 nsecs_t signalTime = mCombinedFence->getSignalTime();
Colin Crosse5729fa2014-03-21 15:04:25 -070056 ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
57 " buffer signal time is %" PRId64,
Igor Murashkine3a9f962013-05-08 18:03:15 -070058 __FUNCTION__, mId, mDequeuedBufferCount, signalTime);
59 if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) {
60 return true;
61 }
62 return false;
63}
64
Igor Murashkine3a9f962013-05-08 18:03:15 -070065void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
66 (void) args;
67 String8 lines;
68 lines.appendFormat(" State: %d\n", mState);
69 lines.appendFormat(" Dims: %d x %d, format 0x%x\n",
70 camera3_stream::width, camera3_stream::height,
71 camera3_stream::format);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000072 lines.appendFormat(" Max size: %zu\n", mMaxSize);
Igor Murashkine3a9f962013-05-08 18:03:15 -070073 lines.appendFormat(" Usage: %d, max HAL buffers: %d\n",
74 camera3_stream::usage, camera3_stream::max_buffers);
Colin Crosse5729fa2014-03-21 15:04:25 -070075 lines.appendFormat(" Frames produced: %d, last timestamp: %" PRId64 " ns\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070076 mFrameCount, mLastTimestamp);
Kévin PETIT377b2ec2014-02-03 12:35:36 +000077 lines.appendFormat(" Total buffers: %zu, currently dequeued: %zu\n",
Igor Murashkine3a9f962013-05-08 18:03:15 -070078 mTotalBufferCount, mDequeuedBufferCount);
79 write(fd, lines.string(), lines.size());
80}
81
82status_t Camera3IOStreamBase::configureQueueLocked() {
83 status_t res;
84
85 switch (mState) {
86 case STATE_IN_RECONFIG:
87 res = disconnectLocked();
88 if (res != OK) {
89 return res;
90 }
91 break;
92 case STATE_IN_CONFIG:
93 // OK
94 break;
95 default:
96 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
97 return INVALID_OPERATION;
98 }
99
100 return OK;
101}
102
103size_t Camera3IOStreamBase::getBufferCountLocked() {
104 return mTotalBufferCount;
105}
106
107status_t Camera3IOStreamBase::disconnectLocked() {
108 switch (mState) {
109 case STATE_IN_RECONFIG:
110 case STATE_CONFIGURED:
111 // OK
112 break;
113 default:
114 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700115 ALOGV("%s: Stream %d: Already disconnected",
116 __FUNCTION__, mId);
117 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700118 }
119
120 if (mDequeuedBufferCount > 0) {
Colin Crosse5729fa2014-03-21 15:04:25 -0700121 ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
Igor Murashkine3a9f962013-05-08 18:03:15 -0700122 __FUNCTION__, mDequeuedBufferCount);
123 return INVALID_OPERATION;
124 }
125
126 return OK;
127}
128
129void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
130 buffer_handle_t *handle,
131 int acquireFence,
132 int releaseFence,
133 camera3_buffer_status_t status) {
134 /**
135 * Note that all fences are now owned by HAL.
136 */
137
138 // Handing out a raw pointer to this object. Increment internal refcount.
139 incStrong(this);
140 buffer.stream = this;
141 buffer.buffer = handle;
142 buffer.acquire_fence = acquireFence;
143 buffer.release_fence = releaseFence;
144 buffer.status = status;
145
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700146 // Inform tracker about becoming busy
147 if (mDequeuedBufferCount == 0 && mState != STATE_IN_CONFIG &&
148 mState != STATE_IN_RECONFIG) {
149 sp<StatusTracker> statusTracker = mStatusTracker.promote();
150 if (statusTracker != 0) {
151 statusTracker->markComponentActive(mStatusId);
152 }
153 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700154 mDequeuedBufferCount++;
155}
156
157status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
158 // Allow dequeue during IN_[RE]CONFIG for registration
159 if (mState != STATE_CONFIGURED &&
160 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
161 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
162 __FUNCTION__, mId, mState);
163 return INVALID_OPERATION;
164 }
165
166 // Only limit dequeue amount when fully configured
167 if (mState == STATE_CONFIGURED &&
168 mDequeuedBufferCount == camera3_stream::max_buffers) {
169 ALOGE("%s: Stream %d: Already dequeued maximum number of simultaneous"
170 " buffers (%d)", __FUNCTION__, mId,
171 camera3_stream::max_buffers);
172 return INVALID_OPERATION;
173 }
174
175 return OK;
176}
177
178status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
179 // Allow buffers to be returned in the error state, to allow for disconnect
180 // and in the in-config states for registration
181 if (mState == STATE_CONSTRUCTED) {
182 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
183 __FUNCTION__, mId, mState);
184 return INVALID_OPERATION;
185 }
186 if (mDequeuedBufferCount == 0) {
187 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
188 mId);
189 return INVALID_OPERATION;
190 }
191
192 return OK;
193}
194
195status_t Camera3IOStreamBase::returnAnyBufferLocked(
196 const camera3_stream_buffer &buffer,
197 nsecs_t timestamp,
198 bool output) {
199 status_t res;
200
201 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
202 // decrementing the internal refcount next. In case this is the last ref, we
203 // might get destructed on the decStrong(), so keep an sp around until the
204 // end of the call - otherwise have to sprinkle the decStrong on all exit
205 // points.
206 sp<Camera3IOStreamBase> keepAlive(this);
207 decStrong(this);
208
209 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
210 return res;
211 }
212
213 sp<Fence> releaseFence;
214 res = returnBufferCheckedLocked(buffer, timestamp, output,
215 &releaseFence);
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700216 // Res may be an error, but we still want to decrement our owned count
217 // to enable clean shutdown. So we'll just return the error but otherwise
218 // carry on
Igor Murashkine3a9f962013-05-08 18:03:15 -0700219
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700220 if (releaseFence != 0) {
221 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
222 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700223
224 mDequeuedBufferCount--;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225 if (mDequeuedBufferCount == 0 && mState != STATE_IN_CONFIG &&
226 mState != STATE_IN_RECONFIG) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700227 ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
228 mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229 sp<StatusTracker> statusTracker = mStatusTracker.promote();
230 if (statusTracker != 0) {
231 statusTracker->markComponentIdle(mStatusId, mCombinedFence);
232 }
233 }
234
Igor Murashkine3a9f962013-05-08 18:03:15 -0700235 mBufferReturnedSignal.signal();
236
237 if (output) {
238 mLastTimestamp = timestamp;
239 }
240
Eino-Ville Talvala07d21692013-09-24 18:04:19 -0700241 return res;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700242}
243
244
245
246}; // namespace camera3
247
248}; // namespace android