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