• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <camera/CameraMetadata.h>
23 #include "Camera3StreamBufferListener.h"
24 #include "Camera3StreamBufferFreedListener.h"
25 
26 namespace android {
27 
28 namespace camera3 {
29 
30 typedef enum camera_buffer_status {
31     CAMERA_BUFFER_STATUS_OK = 0,
32     CAMERA_BUFFER_STATUS_ERROR = 1
33 } camera_buffer_status_t;
34 
35 typedef enum camera_stream_type {
36     CAMERA_STREAM_OUTPUT = 0,
37     CAMERA_STREAM_INPUT = 1,
38     CAMERA_NUM_STREAM_TYPES
39 } camera_stream_type_t;
40 
41 typedef enum camera_stream_rotation {
42     /* No rotation */
43     CAMERA_STREAM_ROTATION_0 = 0,
44 
45     /* Rotate by 90 degree counterclockwise */
46     CAMERA_STREAM_ROTATION_90 = 1,
47 
48     /* Rotate by 180 degree counterclockwise */
49     CAMERA_STREAM_ROTATION_180 = 2,
50 
51     /* Rotate by 270 degree counterclockwise */
52     CAMERA_STREAM_ROTATION_270 = 3
53 } camera_stream_rotation_t;
54 
55 typedef struct camera_stream {
56     camera_stream_type_t stream_type;
57     uint32_t width;
58     uint32_t height;
59     int format;
60     uint32_t usage;
61     uint32_t max_buffers;
62     android_dataspace_t data_space;
63     camera_stream_rotation_t rotation;
64     const char* physical_camera_id;
65 
66     std::unordered_set<int32_t> sensor_pixel_modes_used;
67 } camera_stream_t;
68 
69 typedef struct camera_stream_buffer {
70     camera_stream_t *stream;
71     buffer_handle_t *buffer;
72     camera_buffer_status_t status;
73     int acquire_fence;
74     int release_fence;
75 } camera_stream_buffer_t;
76 
77 struct Size {
78     uint32_t width;
79     uint32_t height;
widthSize80     explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
81 };
82 
83 enum {
84     /**
85      * This stream set ID indicates that the set ID is invalid, and this stream doesn't intend to
86      * share buffers with any other stream. It is illegal to register this kind of stream to
87      * Camera3BufferManager.
88      */
89     CAMERA3_STREAM_SET_ID_INVALID = -1,
90 
91     /**
92      * Invalid output stream ID.
93      */
94     CAMERA3_STREAM_ID_INVALID = -1,
95 };
96 
97 class StatusTracker;
98 
99 // OutputStreamInfo describes the property of a camera stream.
100 class OutputStreamInfo {
101     public:
102         int width;
103         int height;
104         int format;
105         android_dataspace dataSpace;
106         uint64_t consumerUsage;
107         bool finalized = false;
108         bool supportsOffline = false;
109         std::unordered_set<int32_t> sensorPixelModesUsed;
OutputStreamInfo()110         OutputStreamInfo() :
111             width(-1), height(-1), format(-1), dataSpace(HAL_DATASPACE_UNKNOWN),
112             consumerUsage(0) {}
OutputStreamInfo(int _width,int _height,int _format,android_dataspace _dataSpace,uint64_t _consumerUsage,const std::unordered_set<int32_t> & _sensorPixelModesUsed)113         OutputStreamInfo(int _width, int _height, int _format, android_dataspace _dataSpace,
114                 uint64_t _consumerUsage, const std::unordered_set<int32_t>& _sensorPixelModesUsed) :
115             width(_width), height(_height), format(_format),
116             dataSpace(_dataSpace), consumerUsage(_consumerUsage),
117             sensorPixelModesUsed(_sensorPixelModesUsed) {}
118 };
119 
120 /**
121  * An interface for managing a single stream of input and/or output data from
122  * the camera device.
123  */
124 class Camera3StreamInterface : public virtual RefBase {
125   public:
126 
127     enum {
128         ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface
129     };
130 
131     /**
132      * Get the stream's ID
133      */
134     virtual int      getId() const = 0;
135 
136     /**
137      * Get the output stream set id.
138      */
139     virtual int      getStreamSetId() const = 0;
140 
141     /**
142      * Is this stream part of a multi-resolution stream set
143      */
144     virtual bool     isMultiResolution() const = 0;
145 
146     /**
147      * Get the HAL stream group id for a multi-resolution stream set
148      */
149     virtual int      getHalStreamGroupId() const = 0;
150 
151     /**
152      * Get the stream's dimensions and format
153      */
154     virtual uint32_t getWidth() const = 0;
155     virtual uint32_t getHeight() const = 0;
156     virtual int      getFormat() const = 0;
157     virtual android_dataspace getDataSpace() const = 0;
158     virtual void setFormatOverride(bool formatOverriden) = 0;
159     virtual bool isFormatOverridden() const = 0;
160     virtual int getOriginalFormat() const = 0;
161     virtual void setDataSpaceOverride(bool dataSpaceOverriden) = 0;
162     virtual bool isDataSpaceOverridden() const = 0;
163     virtual android_dataspace getOriginalDataSpace() const = 0;
164     virtual int getMaxHalBuffers() const = 0;
165     virtual int getMaxTotalBuffers() const = 0;
166 
167     /**
168      * Offline processing
169      */
170     virtual void setOfflineProcessingSupport(bool support) = 0;
171     virtual bool getOfflineProcessingSupport() const = 0;
172 
173     /**
174      * Get a handle for the stream, without starting stream configuration.
175      */
176     virtual camera_stream* asHalStream() = 0;
177 
178     /**
179      * Start the stream configuration process. Returns a handle to the stream's
180      * information to be passed into the device's configure_streams call.
181      *
182      * Until finishConfiguration() is called, no other methods on the stream may
183      * be called. The usage and max_buffers fields of camera_stream may be
184      * modified between start/finishConfiguration, but may not be changed after
185      * that. The priv field of camera_stream may be modified at any time after
186      * startConfiguration.
187      *
188      * Returns NULL in case of error starting configuration.
189      */
190     virtual camera_stream* startConfiguration() = 0;
191 
192     /**
193      * Check if the stream is mid-configuration (start has been called, but not
194      * finish).  Used for lazy completion of configuration.
195      */
196     virtual bool    isConfiguring() const = 0;
197 
198     /**
199      * Completes the stream configuration process. During this call, the stream
200      * may call the device's register_stream_buffers() method. The stream
201      * information structure returned by startConfiguration() may no longer be
202      * modified after this call, but can still be read until the destruction of
203      * the stream.
204      *
205      * streamReconfigured: set to true when a stream is being reconfigured.
206      *
207      * Returns:
208      *   OK on a successful configuration
209      *   NO_INIT in case of a serious error from the HAL device
210      *   NO_MEMORY in case of an error registering buffers
211      *   INVALID_OPERATION in case connecting to the consumer failed
212      */
213     virtual status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr) = 0;
214 
215     /**
216      * Cancels the stream configuration process. This returns the stream to the
217      * initial state, allowing it to be configured again later.
218      * This is done if the HAL rejects the proposed combined stream configuration
219      */
220     virtual status_t cancelConfiguration() = 0;
221 
222     /**
223      * Determine whether the stream has already become in-use (has received
224      * a valid filled buffer), which determines if a stream can still have
225      * prepareNextBuffer called on it.
226      */
227     virtual bool     isUnpreparable() = 0;
228 
229     /**
230      * Mark the stream as unpreparable.
231      */
232     virtual void     markUnpreparable() = 0;
233 
234     /**
235      * Start stream preparation. May only be called in the CONFIGURED state,
236      * when no valid buffers have yet been returned to this stream. Prepares
237      * up to maxCount buffers, or the maximum number of buffers needed by the
238      * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
239      *
240      * If no prepartion is necessary, returns OK and does not transition to
241      * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
242      * to PREPARING.
243      *
244      * blockRequest specifies whether prepare will block upcoming capture
245      * request. This flag should only be set to false if the caller guarantees
246      * the whole buffer preparation process is done before capture request
247      * comes in.
248      *
249      * Returns:
250      *    OK if no more buffers need to be preallocated
251      *    NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
252      *        buffer pre-allocation, and transitions to the PREPARING state.
253      *    NO_INIT in case of a serious error from the HAL device
254      *    INVALID_OPERATION if called when not in CONFIGURED state, or a
255      *        valid buffer has already been returned to this stream.
256      */
257     virtual status_t startPrepare(int maxCount, bool blockRequest) = 0;
258 
259     /**
260      * Check if the request on a stream is blocked by prepare.
261      */
262     virtual bool     isBlockedByPrepare() const = 0;
263 
264     /**
265      * Continue stream buffer preparation by allocating the next
266      * buffer for this stream.  May only be called in the PREPARED state.
267      *
268      * Returns OK and transitions to the CONFIGURED state if all buffers
269      * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
270      *
271      * Returns:
272      *    OK if no more buffers need to be preallocated, and transitions
273      *        to the CONFIGURED state.
274      *    NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
275      *        buffer pre-allocation.
276      *    NO_INIT in case of a serious error from the HAL device
277      *    INVALID_OPERATION if called when not in CONFIGURED state, or a
278      *        valid buffer has already been returned to this stream.
279      */
280     virtual status_t prepareNextBuffer() = 0;
281 
282     /**
283      * Cancel stream preparation early. In case allocation needs to be
284      * stopped, this method transitions the stream back to the CONFIGURED state.
285      * Buffers that have been allocated with prepareNextBuffer remain that way,
286      * but a later use of prepareNextBuffer will require just as many
287      * calls as if the earlier prepare attempt had not existed.
288      *
289      * Returns:
290      *    OK if cancellation succeeded, and transitions to the CONFIGURED state
291      *    INVALID_OPERATION if not in the PREPARING state
292      *    NO_INIT in case of a serious error from the HAL device
293      */
294     virtual status_t cancelPrepare() = 0;
295 
296     /**
297      * Tear down memory for this stream. This frees all unused gralloc buffers
298      * allocated for this stream, but leaves it ready for operation afterward.
299      *
300      * May only be called in the CONFIGURED state, and keeps the stream in
301      * the CONFIGURED state.
302      *
303      * Returns:
304      *    OK if teardown succeeded.
305      *    INVALID_OPERATION if not in the CONFIGURED state
306      *    NO_INIT in case of a serious error from the HAL device
307      */
308     virtual status_t tearDown() = 0;
309 
310     /**
311      * Fill in the camera_stream_buffer with the next valid buffer for this
312      * stream, to hand over to the HAL.
313      *
314      * Multiple surfaces could share the same HAL stream, but a request may
315      * be only for a subset of surfaces. In this case, the
316      * Camera3StreamInterface object needs the surface ID information to acquire
317      * buffers for those surfaces. For the case of single surface for a HAL
318      * stream, surface_ids parameter has no effect.
319      *
320      * This method may only be called once finishConfiguration has been called.
321      * For bidirectional streams, this method applies to the output-side
322      * buffers.
323      *
324      */
325     virtual status_t getBuffer(camera_stream_buffer *buffer,
326             nsecs_t waitBufferTimeout,
327             const std::vector<size_t>& surface_ids = std::vector<size_t>()) = 0;
328 
329     struct OutstandingBuffer {
330         camera_stream_buffer* outBuffer;
331 
332         /**
333          * Multiple surfaces could share the same HAL stream, but a request may
334          * be only for a subset of surfaces. In this case, the
335          * Camera3StreamInterface object needs the surface ID information to acquire
336          * buffers for those surfaces. For the case of single surface for a HAL
337          * stream, surface_ids parameter has no effect.
338          */
339         std::vector<size_t> surface_ids;
340     };
341     /**
342      * Similar to getBuffer() except this method fills multiple buffers.
343      */
344     virtual status_t getBuffers(std::vector<OutstandingBuffer>* buffers,
345             nsecs_t waitBufferTimeout) = 0;
346 
347     /**
348      * Return a buffer to the stream after use by the HAL.
349      *
350      * Multiple surfaces could share the same HAL stream, but a request may
351      * be only for a subset of surfaces. In this case, the
352      * Camera3StreamInterface object needs the surface ID information to attach
353      * buffers for those surfaces. For the case of single surface for a HAL
354      * stream, surface_ids parameter has no effect.
355      *
356      * This method may only be called for buffers provided by getBuffer().
357      * For bidirectional streams, this method applies to the output-side buffers
358      */
359     virtual status_t returnBuffer(const camera_stream_buffer &buffer,
360             nsecs_t timestamp, bool timestampIncreasing = true,
361             const std::vector<size_t>& surface_ids = std::vector<size_t>(),
362             uint64_t frameNumber = 0) = 0;
363 
364     /**
365      * Fill in the camera_stream_buffer with the next valid buffer for this
366      * stream, to hand over to the HAL.
367      *
368      * This method may only be called once finishConfiguration has been called.
369      * For bidirectional streams, this method applies to the input-side
370      * buffers.
371      *
372      * Normally this call will block until the handed out buffer count is less than the stream
373      * max buffer count; if respectHalLimit is set to false, this is ignored.
374      */
375     virtual status_t getInputBuffer(camera_stream_buffer *buffer,
376             Size *size, bool respectHalLimit = true) = 0;
377 
378     /**
379      * Return a buffer to the stream after use by the HAL.
380      *
381      * This method may only be called for buffers provided by getBuffer().
382      * For bidirectional streams, this method applies to the input-side buffers
383      */
384     virtual status_t returnInputBuffer(const camera_stream_buffer &buffer) = 0;
385 
386     /**
387      * Get the buffer producer of the input buffer queue.
388      *
389      * This method only applies to input streams.
390      */
391     virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0;
392 
393     /**
394      * Whether any of the stream's buffers are currently in use by the HAL,
395      * including buffers that have been returned but not yet had their
396      * release fence signaled.
397      */
398     virtual bool     hasOutstandingBuffers() const = 0;
399 
400     /**
401      * Get number of buffers currently handed out to HAL
402      */
403     virtual size_t   getOutstandingBuffersCount() const = 0;
404 
405     enum {
406         TIMEOUT_NEVER = -1
407     };
408 
409     /**
410      * Set the state tracker to use for signaling idle transitions.
411      */
412     virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0;
413 
414     /**
415      * Disconnect stream from its non-HAL endpoint. After this,
416      * start/finishConfiguration must be called before the stream can be used
417      * again. This cannot be called if the stream has outstanding dequeued
418      * buffers.
419      */
420     virtual status_t disconnect() = 0;
421 
422     /**
423      * Return if the buffer queue of the stream is abandoned.
424      */
425     virtual bool isAbandoned() const = 0;
426 
427     /**
428      * Debug dump of the stream's state.
429      */
430     virtual void     dump(int fd, const Vector<String16> &args) const = 0;
431 
432     virtual void     addBufferListener(
433             wp<Camera3StreamBufferListener> listener) = 0;
434     virtual void     removeBufferListener(
435             const sp<Camera3StreamBufferListener>& listener) = 0;
436 
437     /**
438      * Setting listner will remove previous listener (if exists)
439      * Only allow set listener during stream configuration because stream is guaranteed to be IDLE
440      * at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks.
441      * Client is responsible to keep the listener object alive throughout the lifecycle of this
442      * Camera3Stream.
443      */
444     virtual void setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener) = 0;
445 
446     /**
447      * Notify buffer stream listeners about incoming request with particular frame number.
448      */
449     virtual void fireBufferRequestForFrameNumber(uint64_t frameNumber,
450             const CameraMetadata& settings) = 0;
451 };
452 
453 } // namespace camera3
454 
455 } // namespace android
456 
457 #endif
458