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