Camera3StreamInterface.h revision c78ac26e3a65328fc0118f16ee76a800d0687eb7
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#ifndef ANDROID_SERVERS_CAMERA3_STREAM_INTERFACE_H
18#define ANDROID_SERVERS_CAMERA3_STREAM_INTERFACE_H
19
20#include <utils/RefBase.h>
21#include "Camera3StreamBufferListener.h"
22
23struct camera3_stream_buffer;
24
25namespace android {
26
27namespace camera3 {
28
29class StatusTracker;
30
31/**
32 * An interface for managing a single stream of input and/or output data from
33 * the camera device.
34 */
35class Camera3StreamInterface : public virtual RefBase {
36  public:
37
38    enum {
39        ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface
40    };
41
42    /**
43     * Get the stream's ID
44     */
45    virtual int      getId() const = 0;
46
47    /**
48     * Get the stream's dimensions and format
49     */
50    virtual uint32_t getWidth() const = 0;
51    virtual uint32_t getHeight() const = 0;
52    virtual int      getFormat() const = 0;
53    virtual android_dataspace getDataSpace() const = 0;
54
55    /**
56     * Start the stream configuration process. Returns a handle to the stream's
57     * information to be passed into the HAL device's configure_streams call.
58     *
59     * Until finishConfiguration() is called, no other methods on the stream may
60     * be called. The usage and max_buffers fields of camera3_stream may be
61     * modified between start/finishConfiguration, but may not be changed after
62     * that. The priv field of camera3_stream may be modified at any time after
63     * startConfiguration.
64     *
65     * Returns NULL in case of error starting configuration.
66     */
67    virtual camera3_stream* startConfiguration() = 0;
68
69    /**
70     * Check if the stream is mid-configuration (start has been called, but not
71     * finish).  Used for lazy completion of configuration.
72     */
73    virtual bool    isConfiguring() const = 0;
74
75    /**
76     * Completes the stream configuration process. During this call, the stream
77     * may call the device's register_stream_buffers() method. The stream
78     * information structure returned by startConfiguration() may no longer be
79     * modified after this call, but can still be read until the destruction of
80     * the stream.
81     *
82     * Returns:
83     *   OK on a successful configuration
84     *   NO_INIT in case of a serious error from the HAL device
85     *   NO_MEMORY in case of an error registering buffers
86     *   INVALID_OPERATION in case connecting to the consumer failed
87     */
88    virtual status_t finishConfiguration(camera3_device *hal3Device) = 0;
89
90    /**
91     * Cancels the stream configuration process. This returns the stream to the
92     * initial state, allowing it to be configured again later.
93     * This is done if the HAL rejects the proposed combined stream configuration
94     */
95    virtual status_t cancelConfiguration() = 0;
96
97    /**
98     * Determine whether the stream has already become in-use (has received
99     * a valid filled buffer), which determines if a stream can still have
100     * prepareNextBuffer called on it.
101     */
102    virtual bool     isUnpreparable() = 0;
103
104    /**
105     * Start stream preparation. May only be called in the CONFIGURED state,
106     * when no valid buffers have yet been returned to this stream. Prepares
107     * up to maxCount buffers, or the maximum number of buffers needed by the
108     * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
109     *
110     * If no prepartion is necessary, returns OK and does not transition to
111     * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
112     * to PREPARING.
113     *
114     * Returns:
115     *    OK if no more buffers need to be preallocated
116     *    NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
117     *        buffer pre-allocation, and transitions to the PREPARING state.
118     *    NO_INIT in case of a serious error from the HAL device
119     *    INVALID_OPERATION if called when not in CONFIGURED state, or a
120     *        valid buffer has already been returned to this stream.
121     */
122    virtual status_t startPrepare(int maxCount) = 0;
123
124    /**
125     * Check if the stream is mid-preparing.
126     */
127    virtual bool     isPreparing() const = 0;
128
129    /**
130     * Continue stream buffer preparation by allocating the next
131     * buffer for this stream.  May only be called in the PREPARED state.
132     *
133     * Returns OK and transitions to the CONFIGURED state if all buffers
134     * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
135     *
136     * Returns:
137     *    OK if no more buffers need to be preallocated, and transitions
138     *        to the CONFIGURED state.
139     *    NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
140     *        buffer pre-allocation.
141     *    NO_INIT in case of a serious error from the HAL device
142     *    INVALID_OPERATION if called when not in CONFIGURED state, or a
143     *        valid buffer has already been returned to this stream.
144     */
145    virtual status_t prepareNextBuffer() = 0;
146
147    /**
148     * Cancel stream preparation early. In case allocation needs to be
149     * stopped, this method transitions the stream back to the CONFIGURED state.
150     * Buffers that have been allocated with prepareNextBuffer remain that way,
151     * but a later use of prepareNextBuffer will require just as many
152     * calls as if the earlier prepare attempt had not existed.
153     *
154     * Returns:
155     *    OK if cancellation succeeded, and transitions to the CONFIGURED state
156     *    INVALID_OPERATION if not in the PREPARING state
157     *    NO_INIT in case of a serious error from the HAL device
158     */
159    virtual status_t cancelPrepare() = 0;
160
161    /**
162     * Tear down memory for this stream. This frees all unused gralloc buffers
163     * allocated for this stream, but leaves it ready for operation afterward.
164     *
165     * May only be called in the CONFIGURED state, and keeps the stream in
166     * the CONFIGURED state.
167     *
168     * Returns:
169     *    OK if teardown succeeded.
170     *    INVALID_OPERATION if not in the CONFIGURED state
171     *    NO_INIT in case of a serious error from the HAL device
172     */
173    virtual status_t tearDown() = 0;
174
175    /**
176     * Fill in the camera3_stream_buffer with the next valid buffer for this
177     * stream, to hand over to the HAL.
178     *
179     * This method may only be called once finishConfiguration has been called.
180     * For bidirectional streams, this method applies to the output-side
181     * buffers.
182     *
183     */
184    virtual status_t getBuffer(camera3_stream_buffer *buffer) = 0;
185
186    /**
187     * Return a buffer to the stream after use by the HAL.
188     *
189     * This method may only be called for buffers provided by getBuffer().
190     * For bidirectional streams, this method applies to the output-side buffers
191     */
192    virtual status_t returnBuffer(const camera3_stream_buffer &buffer,
193            nsecs_t timestamp) = 0;
194
195    /**
196     * Fill in the camera3_stream_buffer with the next valid buffer for this
197     * stream, to hand over to the HAL.
198     *
199     * This method may only be called once finishConfiguration has been called.
200     * For bidirectional streams, this method applies to the input-side
201     * buffers.
202     *
203     */
204    virtual status_t getInputBuffer(camera3_stream_buffer *buffer) = 0;
205
206    /**
207     * Return a buffer to the stream after use by the HAL.
208     *
209     * This method may only be called for buffers provided by getBuffer().
210     * For bidirectional streams, this method applies to the input-side buffers
211     */
212    virtual status_t returnInputBuffer(const camera3_stream_buffer &buffer) = 0;
213
214    /**
215     * Get the buffer producer of the input buffer queue.
216     *
217     * This method only applies to input streams.
218     */
219    virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0;
220
221    /**
222     * Whether any of the stream's buffers are currently in use by the HAL,
223     * including buffers that have been returned but not yet had their
224     * release fence signaled.
225     */
226    virtual bool     hasOutstandingBuffers() const = 0;
227
228    enum {
229        TIMEOUT_NEVER = -1
230    };
231
232    /**
233     * Set the state tracker to use for signaling idle transitions.
234     */
235    virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0;
236
237    /**
238     * Disconnect stream from its non-HAL endpoint. After this,
239     * start/finishConfiguration must be called before the stream can be used
240     * again. This cannot be called if the stream has outstanding dequeued
241     * buffers.
242     */
243    virtual status_t disconnect() = 0;
244
245    /**
246     * Debug dump of the stream's state.
247     */
248    virtual void     dump(int fd, const Vector<String16> &args) const = 0;
249
250    virtual void     addBufferListener(
251            wp<Camera3StreamBufferListener> listener) = 0;
252    virtual void     removeBufferListener(
253            const sp<Camera3StreamBufferListener>& listener) = 0;
254};
255
256} // namespace camera3
257
258} // namespace android
259
260#endif
261