Camera3IOStreamBase.cpp revision 13d315eb8c0848ea0584b9fb1d27bab55bc8158b
1/*
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#include <inttypes.h>
22
23#include <utils/Log.h>
24#include <utils/Trace.h>
25#include "device3/Camera3IOStreamBase.h"
26#include "device3/StatusTracker.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 %zu outstanding buffers,"
57            " buffer signal time is %" PRId64,
58            __FUNCTION__, mId, mDequeuedBufferCount, signalTime);
59    if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) {
60        return true;
61    }
62    return false;
63}
64
65void 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);
72    lines.appendFormat("      Max size: %zu\n", mMaxSize);
73    lines.appendFormat("      Usage: %d, max HAL buffers: %d\n",
74            camera3_stream::usage, camera3_stream::max_buffers);
75    lines.appendFormat("      Frames produced: %d, last timestamp: %" PRId64 " ns\n",
76            mFrameCount, mLastTimestamp);
77    lines.appendFormat("      Total buffers: %zu, currently dequeued: %zu\n",
78            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
115            ALOGV("%s: Stream %d: Already disconnected",
116                  __FUNCTION__, mId);
117            return -ENOTCONN;
118    }
119
120    if (mDequeuedBufferCount > 0) {
121        ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
122                __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
146    // Inform tracker about becoming busy
147    if (mDequeuedBufferCount == 0 && mState != STATE_IN_CONFIG &&
148            mState != STATE_IN_RECONFIG) {
149        /**
150         * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
151         * before/after register_stream_buffers during initial configuration
152         * or re-configuration.
153         *
154         * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2
155         */
156        sp<StatusTracker> statusTracker = mStatusTracker.promote();
157        if (statusTracker != 0) {
158            statusTracker->markComponentActive(mStatusId);
159        }
160    }
161    mDequeuedBufferCount++;
162}
163
164status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
165    // Allow dequeue during IN_[RE]CONFIG for registration
166    if (mState != STATE_CONFIGURED &&
167            mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
168        ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
169                __FUNCTION__, mId, mState);
170        return INVALID_OPERATION;
171    }
172
173    // Only limit dequeue amount when fully configured
174    if (mState == STATE_CONFIGURED &&
175            mDequeuedBufferCount == camera3_stream::max_buffers) {
176        ALOGE("%s: Stream %d: Already dequeued maximum number of simultaneous"
177                " buffers (%d)", __FUNCTION__, mId,
178                camera3_stream::max_buffers);
179        return INVALID_OPERATION;
180    }
181
182    return OK;
183}
184
185status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
186    // Allow buffers to be returned in the error state, to allow for disconnect
187    // and in the in-config states for registration
188    if (mState == STATE_CONSTRUCTED) {
189        ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
190                __FUNCTION__, mId, mState);
191        return INVALID_OPERATION;
192    }
193    if (mDequeuedBufferCount == 0) {
194        ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
195                mId);
196        return INVALID_OPERATION;
197    }
198
199    return OK;
200}
201
202status_t Camera3IOStreamBase::returnAnyBufferLocked(
203        const camera3_stream_buffer &buffer,
204        nsecs_t timestamp,
205        bool output) {
206    status_t res;
207
208    // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
209    // decrementing the internal refcount next. In case this is the last ref, we
210    // might get destructed on the decStrong(), so keep an sp around until the
211    // end of the call - otherwise have to sprinkle the decStrong on all exit
212    // points.
213    sp<Camera3IOStreamBase> keepAlive(this);
214    decStrong(this);
215
216    if ((res = returnBufferPreconditionCheckLocked()) != OK) {
217        return res;
218    }
219
220    sp<Fence> releaseFence;
221    res = returnBufferCheckedLocked(buffer, timestamp, output,
222                                    &releaseFence);
223    // Res may be an error, but we still want to decrement our owned count
224    // to enable clean shutdown. So we'll just return the error but otherwise
225    // carry on
226
227    if (releaseFence != 0) {
228        mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
229    }
230
231    mDequeuedBufferCount--;
232    if (mDequeuedBufferCount == 0 && mState != STATE_IN_CONFIG &&
233            mState != STATE_IN_RECONFIG) {
234        /**
235         * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
236         * before/after register_stream_buffers during initial configuration
237         * or re-configuration.
238         *
239         * TODO: IN_CONFIG and IN_RECONFIG checks only make sense for <HAL3.2
240         */
241        ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
242                mId);
243        sp<StatusTracker> statusTracker = mStatusTracker.promote();
244        if (statusTracker != 0) {
245            statusTracker->markComponentIdle(mStatusId, mCombinedFence);
246        }
247    }
248
249    mBufferReturnedSignal.signal();
250
251    if (output) {
252        mLastTimestamp = timestamp;
253    }
254
255    return res;
256}
257
258
259
260}; // namespace camera3
261
262}; // namespace android
263