blob: 0850566e681f055421a4b70c45e8986275b9525d [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
21// This is needed for stdint.h to define INT64_MAX in C++
22#define __STDC_LIMIT_MACROS
23
24#include <utils/Log.h>
25#include <utils/Trace.h>
26#include "Camera3IOStreamBase.h"
27
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();
56 ALOGV("%s: Stream %d: Has %d outstanding buffers,"
57 " buffer signal time is %lld",
58 __FUNCTION__, mId, mDequeuedBufferCount, signalTime);
59 if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) {
60 return true;
61 }
62 return false;
63}
64
65status_t Camera3IOStreamBase::waitUntilIdle(nsecs_t timeout) {
66 status_t res;
67 {
68 Mutex::Autolock l(mLock);
69 while (mDequeuedBufferCount > 0) {
70 if (timeout != TIMEOUT_NEVER) {
71 nsecs_t startTime = systemTime();
72 res = mBufferReturnedSignal.waitRelative(mLock, timeout);
73 if (res == TIMED_OUT) {
74 return res;
75 } else if (res != OK) {
76 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
77 __FUNCTION__, strerror(-res), res);
78 return res;
79 }
80 nsecs_t deltaTime = systemTime() - startTime;
81 if (timeout <= deltaTime) {
82 timeout = 0;
83 } else {
84 timeout -= deltaTime;
85 }
86 } else {
87 res = mBufferReturnedSignal.wait(mLock);
88 if (res != OK) {
89 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
90 __FUNCTION__, strerror(-res), res);
91 return res;
92 }
93 }
94 }
95 }
96
97 // No lock
98
99 unsigned int timeoutMs;
100 if (timeout == TIMEOUT_NEVER) {
101 timeoutMs = Fence::TIMEOUT_NEVER;
102 } else if (timeout == 0) {
103 timeoutMs = 0;
104 } else {
105 // Round up to wait at least 1 ms
106 timeoutMs = (timeout + 999999) / 1000000;
107 }
108
109 return mCombinedFence->wait(timeoutMs);
110}
111
112void Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const {
113 (void) args;
114 String8 lines;
115 lines.appendFormat(" State: %d\n", mState);
116 lines.appendFormat(" Dims: %d x %d, format 0x%x\n",
117 camera3_stream::width, camera3_stream::height,
118 camera3_stream::format);
119 lines.appendFormat(" Max size: %d\n", mMaxSize);
120 lines.appendFormat(" Usage: %d, max HAL buffers: %d\n",
121 camera3_stream::usage, camera3_stream::max_buffers);
122 lines.appendFormat(" Frames produced: %d, last timestamp: %lld ns\n",
123 mFrameCount, mLastTimestamp);
124 lines.appendFormat(" Total buffers: %d, currently dequeued: %d\n",
125 mTotalBufferCount, mDequeuedBufferCount);
126 write(fd, lines.string(), lines.size());
127}
128
129status_t Camera3IOStreamBase::configureQueueLocked() {
130 status_t res;
131
132 switch (mState) {
133 case STATE_IN_RECONFIG:
134 res = disconnectLocked();
135 if (res != OK) {
136 return res;
137 }
138 break;
139 case STATE_IN_CONFIG:
140 // OK
141 break;
142 default:
143 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
144 return INVALID_OPERATION;
145 }
146
147 return OK;
148}
149
150size_t Camera3IOStreamBase::getBufferCountLocked() {
151 return mTotalBufferCount;
152}
153
154status_t Camera3IOStreamBase::disconnectLocked() {
155 switch (mState) {
156 case STATE_IN_RECONFIG:
157 case STATE_CONFIGURED:
158 // OK
159 break;
160 default:
161 // No connection, nothing to do
Igor Murashkine2172be2013-05-28 15:31:39 -0700162 ALOGV("%s: Stream %d: Already disconnected",
163 __FUNCTION__, mId);
164 return -ENOTCONN;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700165 }
166
167 if (mDequeuedBufferCount > 0) {
168 ALOGE("%s: Can't disconnect with %d buffers still dequeued!",
169 __FUNCTION__, mDequeuedBufferCount);
170 return INVALID_OPERATION;
171 }
172
173 return OK;
174}
175
176void Camera3IOStreamBase::handoutBufferLocked(camera3_stream_buffer &buffer,
177 buffer_handle_t *handle,
178 int acquireFence,
179 int releaseFence,
180 camera3_buffer_status_t status) {
181 /**
182 * Note that all fences are now owned by HAL.
183 */
184
185 // Handing out a raw pointer to this object. Increment internal refcount.
186 incStrong(this);
187 buffer.stream = this;
188 buffer.buffer = handle;
189 buffer.acquire_fence = acquireFence;
190 buffer.release_fence = releaseFence;
191 buffer.status = status;
192
193 mDequeuedBufferCount++;
194}
195
196status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
197 // Allow dequeue during IN_[RE]CONFIG for registration
198 if (mState != STATE_CONFIGURED &&
199 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
200 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
201 __FUNCTION__, mId, mState);
202 return INVALID_OPERATION;
203 }
204
205 // Only limit dequeue amount when fully configured
206 if (mState == STATE_CONFIGURED &&
207 mDequeuedBufferCount == camera3_stream::max_buffers) {
208 ALOGE("%s: Stream %d: Already dequeued maximum number of simultaneous"
209 " buffers (%d)", __FUNCTION__, mId,
210 camera3_stream::max_buffers);
211 return INVALID_OPERATION;
212 }
213
214 return OK;
215}
216
217status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
218 // Allow buffers to be returned in the error state, to allow for disconnect
219 // and in the in-config states for registration
220 if (mState == STATE_CONSTRUCTED) {
221 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
222 __FUNCTION__, mId, mState);
223 return INVALID_OPERATION;
224 }
225 if (mDequeuedBufferCount == 0) {
226 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
227 mId);
228 return INVALID_OPERATION;
229 }
230
231 return OK;
232}
233
234status_t Camera3IOStreamBase::returnAnyBufferLocked(
235 const camera3_stream_buffer &buffer,
236 nsecs_t timestamp,
237 bool output) {
238 status_t res;
239
240 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
241 // decrementing the internal refcount next. In case this is the last ref, we
242 // might get destructed on the decStrong(), so keep an sp around until the
243 // end of the call - otherwise have to sprinkle the decStrong on all exit
244 // points.
245 sp<Camera3IOStreamBase> keepAlive(this);
246 decStrong(this);
247
248 if ((res = returnBufferPreconditionCheckLocked()) != OK) {
249 return res;
250 }
251
252 sp<Fence> releaseFence;
253 res = returnBufferCheckedLocked(buffer, timestamp, output,
254 &releaseFence);
255 if (res != OK) {
256 return res;
257 }
258
259 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
260
261 mDequeuedBufferCount--;
262 mBufferReturnedSignal.signal();
263
264 if (output) {
265 mLastTimestamp = timestamp;
266 }
267
268 return OK;
269}
270
271
272
273}; // namespace camera3
274
275}; // namespace android