Camera3Stream.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-Stream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "device3/Camera3Stream.h"
24#include "device3/StatusTracker.h"
25
26#include <cutils/properties.h>
27
28namespace android {
29
30namespace camera3 {
31
32Camera3Stream::~Camera3Stream() {
33    sp<StatusTracker> statusTracker = mStatusTracker.promote();
34    if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
35        statusTracker->removeComponent(mStatusId);
36    }
37}
38
39Camera3Stream* Camera3Stream::cast(camera3_stream *stream) {
40    return static_cast<Camera3Stream*>(stream);
41}
42
43const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) {
44    return static_cast<const Camera3Stream*>(stream);
45}
46
47Camera3Stream::Camera3Stream(int id,
48        camera3_stream_type type,
49        uint32_t width, uint32_t height, size_t maxSize, int format) :
50    camera3_stream(),
51    mId(id),
52    mName(String8::format("Camera3Stream[%d]", id)),
53    mMaxSize(maxSize),
54    mState(STATE_CONSTRUCTED),
55    mStatusId(StatusTracker::NO_STATUS_ID) {
56
57    camera3_stream::stream_type = type;
58    camera3_stream::width = width;
59    camera3_stream::height = height;
60    camera3_stream::format = format;
61    camera3_stream::usage = 0;
62    camera3_stream::max_buffers = 0;
63    camera3_stream::priv = NULL;
64
65    if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) {
66        ALOGE("%s: BLOB format with size == 0", __FUNCTION__);
67        mState = STATE_ERROR;
68    }
69}
70
71int Camera3Stream::getId() const {
72    return mId;
73}
74
75uint32_t Camera3Stream::getWidth() const {
76    return camera3_stream::width;
77}
78
79uint32_t Camera3Stream::getHeight() const {
80    return camera3_stream::height;
81}
82
83int Camera3Stream::getFormat() const {
84    return camera3_stream::format;
85}
86
87camera3_stream* Camera3Stream::startConfiguration() {
88    ATRACE_CALL();
89    Mutex::Autolock l(mLock);
90    status_t res;
91
92    switch (mState) {
93        case STATE_ERROR:
94            ALOGE("%s: In error state", __FUNCTION__);
95            return NULL;
96        case STATE_CONSTRUCTED:
97            // OK
98            break;
99        case STATE_IN_CONFIG:
100        case STATE_IN_RECONFIG:
101            // Can start config again with no trouble; but don't redo
102            // oldUsage/oldMaxBuffers
103            return this;
104        case STATE_CONFIGURED:
105            if (stream_type == CAMERA3_STREAM_INPUT) {
106                ALOGE("%s: Cannot configure an input stream twice",
107                        __FUNCTION__);
108                return NULL;
109            } else if (hasOutstandingBuffersLocked()) {
110                ALOGE("%s: Cannot configure stream; has outstanding buffers",
111                        __FUNCTION__);
112                return NULL;
113            }
114            break;
115        default:
116            ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
117            return NULL;
118    }
119
120    oldUsage = camera3_stream::usage;
121    oldMaxBuffers = camera3_stream::max_buffers;
122
123    res = getEndpointUsage(&(camera3_stream::usage));
124    if (res != OK) {
125        ALOGE("%s: Cannot query consumer endpoint usage!",
126                __FUNCTION__);
127        return NULL;
128    }
129
130    // Stop tracking if currently doing so
131    if (mStatusId != StatusTracker::NO_STATUS_ID) {
132        sp<StatusTracker> statusTracker = mStatusTracker.promote();
133        if (statusTracker != 0) {
134            statusTracker->removeComponent(mStatusId);
135        }
136        mStatusId = StatusTracker::NO_STATUS_ID;
137    }
138
139    if (mState == STATE_CONSTRUCTED) {
140        mState = STATE_IN_CONFIG;
141    } else { // mState == STATE_CONFIGURED
142        LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
143        mState = STATE_IN_RECONFIG;
144    }
145
146    return this;
147}
148
149bool Camera3Stream::isConfiguring() const {
150    Mutex::Autolock l(mLock);
151    return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
152}
153
154status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) {
155    ATRACE_CALL();
156    Mutex::Autolock l(mLock);
157    switch (mState) {
158        case STATE_ERROR:
159            ALOGE("%s: In error state", __FUNCTION__);
160            return INVALID_OPERATION;
161        case STATE_IN_CONFIG:
162        case STATE_IN_RECONFIG:
163            // OK
164            break;
165        case STATE_CONSTRUCTED:
166        case STATE_CONFIGURED:
167            ALOGE("%s: Cannot finish configuration that hasn't been started",
168                    __FUNCTION__);
169            return INVALID_OPERATION;
170        default:
171            ALOGE("%s: Unknown state", __FUNCTION__);
172            return INVALID_OPERATION;
173    }
174
175    // Register for idle tracking
176    sp<StatusTracker> statusTracker = mStatusTracker.promote();
177    if (statusTracker != 0) {
178        mStatusId = statusTracker->addComponent();
179    }
180
181    // Check if the stream configuration is unchanged, and skip reallocation if
182    // so. As documented in hardware/camera3.h:configure_streams().
183    if (mState == STATE_IN_RECONFIG &&
184            oldUsage == camera3_stream::usage &&
185            oldMaxBuffers == camera3_stream::max_buffers) {
186        mState = STATE_CONFIGURED;
187        return OK;
188    }
189
190    status_t res;
191    res = configureQueueLocked();
192    if (res != OK) {
193        ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
194                __FUNCTION__, mId, strerror(-res), res);
195        mState = STATE_ERROR;
196        return res;
197    }
198
199    res = registerBuffersLocked(hal3Device);
200    if (res != OK) {
201        ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)",
202                __FUNCTION__, strerror(-res), res);
203        mState = STATE_ERROR;
204        return res;
205    }
206
207    mState = STATE_CONFIGURED;
208
209    return res;
210}
211
212status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
213    ATRACE_CALL();
214    Mutex::Autolock l(mLock);
215
216    status_t res = getBufferLocked(buffer);
217    if (res == OK) {
218        fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
219    }
220
221    return res;
222}
223
224status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
225        nsecs_t timestamp) {
226    ATRACE_CALL();
227    Mutex::Autolock l(mLock);
228
229    /**
230     * TODO: Check that the state is valid first.
231     *
232     * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
233     * >= HAL3.2 CONFIGURED only
234     *
235     * Do this for getBuffer as well.
236     */
237    status_t res = returnBufferLocked(buffer, timestamp);
238    if (res == OK) {
239        fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
240    }
241
242    return res;
243}
244
245status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
246    ATRACE_CALL();
247    Mutex::Autolock l(mLock);
248
249    status_t res = getInputBufferLocked(buffer);
250    if (res == OK) {
251        fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
252    }
253
254    return res;
255}
256
257status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
258    ATRACE_CALL();
259    Mutex::Autolock l(mLock);
260
261    status_t res = returnInputBufferLocked(buffer);
262    if (res == OK) {
263        fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
264    }
265    return res;
266}
267
268void Camera3Stream::fireBufferListenersLocked(
269        const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
270    List<wp<Camera3StreamBufferListener> >::iterator it, end;
271
272    // TODO: finish implementing
273
274    Camera3StreamBufferListener::BufferInfo info =
275        Camera3StreamBufferListener::BufferInfo();
276    info.mOutput = output;
277    // TODO: rest of fields
278
279    for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
280         it != end;
281         ++it) {
282
283        sp<Camera3StreamBufferListener> listener = it->promote();
284        if (listener != 0) {
285            if (acquired) {
286                listener->onBufferAcquired(info);
287            } else {
288                listener->onBufferReleased(info);
289            }
290        }
291    }
292}
293
294bool Camera3Stream::hasOutstandingBuffers() const {
295    ATRACE_CALL();
296    Mutex::Autolock l(mLock);
297    return hasOutstandingBuffersLocked();
298}
299
300status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
301    Mutex::Autolock l(mLock);
302    sp<StatusTracker> oldTracker = mStatusTracker.promote();
303    if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
304        oldTracker->removeComponent(mStatusId);
305    }
306    mStatusId = StatusTracker::NO_STATUS_ID;
307    mStatusTracker = statusTracker;
308
309    return OK;
310}
311
312status_t Camera3Stream::disconnect() {
313    ATRACE_CALL();
314    Mutex::Autolock l(mLock);
315    ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
316    status_t res = disconnectLocked();
317
318    if (res == -ENOTCONN) {
319        // "Already disconnected" -- not an error
320        return OK;
321    } else {
322        return res;
323    }
324}
325
326status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
327    ATRACE_CALL();
328
329    /**
330     * >= CAMERA_DEVICE_API_VERSION_3_2:
331     *
332     * camera3_device_t->ops->register_stream_buffers() is not called and must
333     * be NULL.
334     */
335    if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
336        ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
337
338        /**
339         * Skip the NULL check if camera.dev.register_stream is 1.
340         *
341         * For development-validation purposes only.
342         *
343         * TODO: Remove the property check before shipping L (b/13914251).
344         */
345        char value[PROPERTY_VALUE_MAX] = { '\0', };
346        property_get("camera.dev.register_stream", value, "0");
347        int propInt = atoi(value);
348
349        if (propInt == 0 && hal3Device->ops->register_stream_buffers != NULL) {
350            ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
351                    "must be set to NULL in camera3_device::ops", __FUNCTION__);
352            return INVALID_OPERATION;
353        } else {
354            ALOGD("%s: Skipping NULL check for deprecated register_stream_buffers");
355        }
356
357        return OK;
358    } else {
359        ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
360    }
361
362    status_t res;
363
364    size_t bufferCount = getBufferCountLocked();
365
366    Vector<buffer_handle_t*> buffers;
367    buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
368
369    camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
370    bufferSet.stream = this;
371    bufferSet.num_buffers = bufferCount;
372    bufferSet.buffers = buffers.editArray();
373
374    Vector<camera3_stream_buffer_t> streamBuffers;
375    streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
376
377    // Register all buffers with the HAL. This means getting all the buffers
378    // from the stream, providing them to the HAL with the
379    // register_stream_buffers() method, and then returning them back to the
380    // stream in the error state, since they won't have valid data.
381    //
382    // Only registered buffers can be sent to the HAL.
383
384    uint32_t bufferIdx = 0;
385    for (; bufferIdx < bufferCount; bufferIdx++) {
386        res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
387        if (res != OK) {
388            ALOGE("%s: Unable to get buffer %d for registration with HAL",
389                    __FUNCTION__, bufferIdx);
390            // Skip registering, go straight to cleanup
391            break;
392        }
393
394        sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
395        fence->waitForever("Camera3Stream::registerBuffers");
396
397        buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
398    }
399    if (bufferIdx == bufferCount) {
400        // Got all buffers, register with HAL
401        ALOGV("%s: Registering %zu buffers with camera HAL",
402                __FUNCTION__, bufferCount);
403        ATRACE_BEGIN("camera3->register_stream_buffers");
404        res = hal3Device->ops->register_stream_buffers(hal3Device,
405                &bufferSet);
406        ATRACE_END();
407    }
408
409    // Return all valid buffers to stream, in ERROR state to indicate
410    // they weren't filled.
411    for (size_t i = 0; i < bufferIdx; i++) {
412        streamBuffers.editItemAt(i).release_fence = -1;
413        streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
414        returnBufferLocked(streamBuffers[i], 0);
415    }
416
417    return res;
418}
419
420status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
421    ALOGE("%s: This type of stream does not support output", __FUNCTION__);
422    return INVALID_OPERATION;
423}
424status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
425                                           nsecs_t) {
426    ALOGE("%s: This type of stream does not support output", __FUNCTION__);
427    return INVALID_OPERATION;
428}
429status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
430    ALOGE("%s: This type of stream does not support input", __FUNCTION__);
431    return INVALID_OPERATION;
432}
433status_t Camera3Stream::returnInputBufferLocked(
434        const camera3_stream_buffer &) {
435    ALOGE("%s: This type of stream does not support input", __FUNCTION__);
436    return INVALID_OPERATION;
437}
438
439void Camera3Stream::addBufferListener(
440        wp<Camera3StreamBufferListener> listener) {
441    Mutex::Autolock l(mLock);
442    mBufferListenerList.push_back(listener);
443}
444
445void Camera3Stream::removeBufferListener(
446        const sp<Camera3StreamBufferListener>& listener) {
447    Mutex::Autolock l(mLock);
448
449    bool erased = true;
450    List<wp<Camera3StreamBufferListener> >::iterator it, end;
451    for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
452         it != end;
453         ) {
454
455        if (*it == listener) {
456            it = mBufferListenerList.erase(it);
457            erased = true;
458        } else {
459            ++it;
460        }
461    }
462
463    if (!erased) {
464        ALOGW("%s: Could not find listener to remove, already removed",
465              __FUNCTION__);
466    }
467}
468
469}; // namespace camera3
470
471}; // namespace android
472