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