blob: ca805952e6eb3bac90480eb988cf20566a5950eb [file] [log] [blame]
Igor Murashkinae500e52013-04-22 14:03:54 -07001/*
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>
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080021
22#include <camera/CameraMetadata.h>
Igor Murashkinae500e52013-04-22 14:03:54 -070023#include "Camera3StreamBufferListener.h"
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -070024#include "Camera3StreamBufferFreedListener.h"
Igor Murashkinae500e52013-04-22 14:03:54 -070025
Igor Murashkinae500e52013-04-22 14:03:54 -070026namespace android {
27
28namespace camera3 {
29
Emilian Peevf4816702020-04-03 15:44:51 -070030typedef enum camera_buffer_status {
31 CAMERA_BUFFER_STATUS_OK = 0,
32 CAMERA_BUFFER_STATUS_ERROR = 1
33} camera_buffer_status_t;
34
35typedef 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
41typedef 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
55typedef 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;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080065
66 std::unordered_set<int32_t> sensor_pixel_modes_used;
Emilian Peevf4816702020-04-03 15:44:51 -070067} camera_stream_t;
68
69typedef 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
Shuzhen Wang83bff122020-11-20 15:51:39 -080077struct Size {
78 uint32_t width;
79 uint32_t height;
80 explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
81};
82
Zhijun He125684a2015-12-26 15:07:30 -080083enum {
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
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070097class StatusTracker;
98
Emilian Peev40ead602017-09-26 15:46:36 +010099// OutputStreamInfo describes the property of a camera stream.
100class OutputStreamInfo {
101 public:
102 int width;
103 int height;
104 int format;
105 android_dataspace dataSpace;
106 uint64_t consumerUsage;
107 bool finalized = false;
Emilian Peevcc0b7952020-01-07 13:54:47 -0800108 bool supportsOffline = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800109 std::unordered_set<int32_t> sensorPixelModesUsed;
Emilian Peev40ead602017-09-26 15:46:36 +0100110 OutputStreamInfo() :
111 width(-1), height(-1), format(-1), dataSpace(HAL_DATASPACE_UNKNOWN),
112 consumerUsage(0) {}
113 OutputStreamInfo(int _width, int _height, int _format, android_dataspace _dataSpace,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800114 uint64_t _consumerUsage, const std::unordered_set<int32_t>& _sensorPixelModesUsed) :
Emilian Peev40ead602017-09-26 15:46:36 +0100115 width(_width), height(_height), format(_format),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800116 dataSpace(_dataSpace), consumerUsage(_consumerUsage),
117 sensorPixelModesUsed(_sensorPixelModesUsed) {}
Emilian Peev40ead602017-09-26 15:46:36 +0100118};
119
Igor Murashkinae500e52013-04-22 14:03:54 -0700120/**
121 * An interface for managing a single stream of input and/or output data from
122 * the camera device.
123 */
124class Camera3StreamInterface : public virtual RefBase {
125 public:
Ruben Brunkc78ac262015-08-13 17:58:46 -0700126
127 enum {
128 ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface
129 };
130
Igor Murashkinae500e52013-04-22 14:03:54 -0700131 /**
132 * Get the stream's ID
133 */
134 virtual int getId() const = 0;
135
136 /**
Zhijun He125684a2015-12-26 15:07:30 -0800137 * Get the output stream set id.
138 */
139 virtual int getStreamSetId() const = 0;
140
141 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700142 * Get the stream's dimensions and format
143 */
144 virtual uint32_t getWidth() const = 0;
145 virtual uint32_t getHeight() const = 0;
146 virtual int getFormat() const = 0;
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700147 virtual android_dataspace getDataSpace() const = 0;
Emilian Peev710c1422017-08-30 11:19:38 +0100148 virtual void setFormatOverride(bool formatOverriden) = 0;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700149 virtual bool isFormatOverridden() const = 0;
150 virtual int getOriginalFormat() const = 0;
151 virtual void setDataSpaceOverride(bool dataSpaceOverriden) = 0;
152 virtual bool isDataSpaceOverridden() const = 0;
153 virtual android_dataspace getOriginalDataSpace() const = 0;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700154 virtual int getMaxHalBuffers() const = 0;
155 virtual int getMaxTotalBuffers() const = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700156
157 /**
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800158 * Offline processing
159 */
160 virtual void setOfflineProcessingSupport(bool support) = 0;
161 virtual bool getOfflineProcessingSupport() const = 0;
162
163 /**
Emilian Peevf4816702020-04-03 15:44:51 -0700164 * Get a handle for the stream, without starting stream configuration.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800165 */
Emilian Peevf4816702020-04-03 15:44:51 -0700166 virtual camera_stream* asHalStream() = 0;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800167
168 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700169 * Start the stream configuration process. Returns a handle to the stream's
Emilian Peevf4816702020-04-03 15:44:51 -0700170 * information to be passed into the device's configure_streams call.
Igor Murashkinae500e52013-04-22 14:03:54 -0700171 *
172 * Until finishConfiguration() is called, no other methods on the stream may
Emilian Peevf4816702020-04-03 15:44:51 -0700173 * be called. The usage and max_buffers fields of camera_stream may be
Igor Murashkinae500e52013-04-22 14:03:54 -0700174 * modified between start/finishConfiguration, but may not be changed after
Emilian Peevf4816702020-04-03 15:44:51 -0700175 * that. The priv field of camera_stream may be modified at any time after
Igor Murashkinae500e52013-04-22 14:03:54 -0700176 * startConfiguration.
177 *
178 * Returns NULL in case of error starting configuration.
179 */
Emilian Peevf4816702020-04-03 15:44:51 -0700180 virtual camera_stream* startConfiguration() = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700181
182 /**
183 * Check if the stream is mid-configuration (start has been called, but not
184 * finish). Used for lazy completion of configuration.
185 */
186 virtual bool isConfiguring() const = 0;
187
188 /**
189 * Completes the stream configuration process. During this call, the stream
190 * may call the device's register_stream_buffers() method. The stream
191 * information structure returned by startConfiguration() may no longer be
192 * modified after this call, but can still be read until the destruction of
193 * the stream.
194 *
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700195 * streamReconfigured: set to true when a stream is being reconfigured.
196 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700197 * Returns:
198 * OK on a successful configuration
199 * NO_INIT in case of a serious error from the HAL device
200 * NO_MEMORY in case of an error registering buffers
201 * INVALID_OPERATION in case connecting to the consumer failed
202 */
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700203 virtual status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700204
205 /**
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700206 * Cancels the stream configuration process. This returns the stream to the
207 * initial state, allowing it to be configured again later.
208 * This is done if the HAL rejects the proposed combined stream configuration
209 */
210 virtual status_t cancelConfiguration() = 0;
211
212 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700213 * Determine whether the stream has already become in-use (has received
214 * a valid filled buffer), which determines if a stream can still have
215 * prepareNextBuffer called on it.
216 */
217 virtual bool isUnpreparable() = 0;
218
219 /**
Emilian Peevf0348ae2021-01-13 13:39:45 -0800220 * Mark the stream as unpreparable.
221 */
222 virtual void markUnpreparable() = 0;
223
224 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700225 * Start stream preparation. May only be called in the CONFIGURED state,
Ruben Brunkc78ac262015-08-13 17:58:46 -0700226 * when no valid buffers have yet been returned to this stream. Prepares
227 * up to maxCount buffers, or the maximum number of buffers needed by the
228 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700229 *
230 * If no prepartion is necessary, returns OK and does not transition to
231 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
232 * to PREPARING.
233 *
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700234 * blockRequest specifies whether prepare will block upcoming capture
235 * request. This flag should only be set to false if the caller guarantees
236 * the whole buffer preparation process is done before capture request
237 * comes in.
238 *
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700239 * Returns:
240 * OK if no more buffers need to be preallocated
241 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
242 * buffer pre-allocation, and transitions to the PREPARING state.
243 * NO_INIT in case of a serious error from the HAL device
244 * INVALID_OPERATION if called when not in CONFIGURED state, or a
245 * valid buffer has already been returned to this stream.
246 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700247 virtual status_t startPrepare(int maxCount, bool blockRequest) = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700248
249 /**
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700250 * Check if the request on a stream is blocked by prepare.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700251 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700252 virtual bool isBlockedByPrepare() const = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700253
254 /**
255 * Continue stream buffer preparation by allocating the next
256 * buffer for this stream. May only be called in the PREPARED state.
257 *
258 * Returns OK and transitions to the CONFIGURED state if all buffers
259 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
260 *
261 * Returns:
262 * OK if no more buffers need to be preallocated, and transitions
263 * to the CONFIGURED state.
264 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
265 * buffer pre-allocation.
266 * NO_INIT in case of a serious error from the HAL device
267 * INVALID_OPERATION if called when not in CONFIGURED state, or a
268 * valid buffer has already been returned to this stream.
269 */
270 virtual status_t prepareNextBuffer() = 0;
271
272 /**
273 * Cancel stream preparation early. In case allocation needs to be
274 * stopped, this method transitions the stream back to the CONFIGURED state.
275 * Buffers that have been allocated with prepareNextBuffer remain that way,
276 * but a later use of prepareNextBuffer will require just as many
277 * calls as if the earlier prepare attempt had not existed.
278 *
279 * Returns:
280 * OK if cancellation succeeded, and transitions to the CONFIGURED state
281 * INVALID_OPERATION if not in the PREPARING state
282 * NO_INIT in case of a serious error from the HAL device
283 */
284 virtual status_t cancelPrepare() = 0;
285
286 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700287 * Tear down memory for this stream. This frees all unused gralloc buffers
288 * allocated for this stream, but leaves it ready for operation afterward.
289 *
290 * May only be called in the CONFIGURED state, and keeps the stream in
291 * the CONFIGURED state.
292 *
293 * Returns:
294 * OK if teardown succeeded.
295 * INVALID_OPERATION if not in the CONFIGURED state
296 * NO_INIT in case of a serious error from the HAL device
297 */
298 virtual status_t tearDown() = 0;
299
300 /**
Emilian Peevf4816702020-04-03 15:44:51 -0700301 * Fill in the camera_stream_buffer with the next valid buffer for this
Igor Murashkinae500e52013-04-22 14:03:54 -0700302 * stream, to hand over to the HAL.
303 *
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800304 * Multiple surfaces could share the same HAL stream, but a request may
305 * be only for a subset of surfaces. In this case, the
306 * Camera3StreamInterface object needs the surface ID information to acquire
307 * buffers for those surfaces. For the case of single surface for a HAL
308 * stream, surface_ids parameter has no effect.
309 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700310 * This method may only be called once finishConfiguration has been called.
311 * For bidirectional streams, this method applies to the output-side
312 * buffers.
313 *
314 */
Emilian Peevf4816702020-04-03 15:44:51 -0700315 virtual status_t getBuffer(camera_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700316 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800317 const std::vector<size_t>& surface_ids = std::vector<size_t>()) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700318
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800319 struct OutstandingBuffer {
320 camera_stream_buffer* outBuffer;
321
322 /**
323 * Multiple surfaces could share the same HAL stream, but a request may
324 * be only for a subset of surfaces. In this case, the
325 * Camera3StreamInterface object needs the surface ID information to acquire
326 * buffers for those surfaces. For the case of single surface for a HAL
327 * stream, surface_ids parameter has no effect.
328 */
329 std::vector<size_t> surface_ids;
330 };
331 /**
332 * Similar to getBuffer() except this method fills multiple buffers.
333 */
334 virtual status_t getBuffers(std::vector<OutstandingBuffer>* buffers,
335 nsecs_t waitBufferTimeout) = 0;
336
Igor Murashkinae500e52013-04-22 14:03:54 -0700337 /**
338 * Return a buffer to the stream after use by the HAL.
339 *
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700340 * Multiple surfaces could share the same HAL stream, but a request may
341 * be only for a subset of surfaces. In this case, the
342 * Camera3StreamInterface object needs the surface ID information to attach
343 * buffers for those surfaces. For the case of single surface for a HAL
344 * stream, surface_ids parameter has no effect.
345 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700346 * This method may only be called for buffers provided by getBuffer().
347 * For bidirectional streams, this method applies to the output-side buffers
348 */
Emilian Peevf4816702020-04-03 15:44:51 -0700349 virtual status_t returnBuffer(const camera_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700350 nsecs_t timestamp, bool timestampIncreasing = true,
Emilian Peev538c90e2018-12-17 18:03:19 +0000351 const std::vector<size_t>& surface_ids = std::vector<size_t>(),
352 uint64_t frameNumber = 0) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700353
354 /**
Emilian Peevf4816702020-04-03 15:44:51 -0700355 * Fill in the camera_stream_buffer with the next valid buffer for this
Igor Murashkinae500e52013-04-22 14:03:54 -0700356 * stream, to hand over to the HAL.
357 *
358 * This method may only be called once finishConfiguration has been called.
359 * For bidirectional streams, this method applies to the input-side
360 * buffers.
361 *
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700362 * Normally this call will block until the handed out buffer count is less than the stream
363 * max buffer count; if respectHalLimit is set to false, this is ignored.
Igor Murashkinae500e52013-04-22 14:03:54 -0700364 */
Shuzhen Wang83bff122020-11-20 15:51:39 -0800365 virtual status_t getInputBuffer(camera_stream_buffer *buffer,
366 Size *size, bool respectHalLimit = true) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700367
368 /**
369 * Return a buffer to the stream after use by the HAL.
370 *
371 * This method may only be called for buffers provided by getBuffer().
372 * For bidirectional streams, this method applies to the input-side buffers
373 */
Emilian Peevf4816702020-04-03 15:44:51 -0700374 virtual status_t returnInputBuffer(const camera_stream_buffer &buffer) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700375
376 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700377 * Get the buffer producer of the input buffer queue.
378 *
379 * This method only applies to input streams.
380 */
381 virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0;
382
383 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700384 * Whether any of the stream's buffers are currently in use by the HAL,
385 * including buffers that have been returned but not yet had their
386 * release fence signaled.
387 */
388 virtual bool hasOutstandingBuffers() const = 0;
389
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700390 /**
391 * Get number of buffers currently handed out to HAL
392 */
393 virtual size_t getOutstandingBuffersCount() const = 0;
394
Igor Murashkinae500e52013-04-22 14:03:54 -0700395 enum {
396 TIMEOUT_NEVER = -1
397 };
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700398
Igor Murashkinae500e52013-04-22 14:03:54 -0700399 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700400 * Set the state tracker to use for signaling idle transitions.
Igor Murashkinae500e52013-04-22 14:03:54 -0700401 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700402 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700403
404 /**
405 * Disconnect stream from its non-HAL endpoint. After this,
406 * start/finishConfiguration must be called before the stream can be used
407 * again. This cannot be called if the stream has outstanding dequeued
408 * buffers.
409 */
410 virtual status_t disconnect() = 0;
411
412 /**
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700413 * Return if the buffer queue of the stream is abandoned.
414 */
415 virtual bool isAbandoned() const = 0;
416
417 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700418 * Debug dump of the stream's state.
419 */
420 virtual void dump(int fd, const Vector<String16> &args) const = 0;
421
422 virtual void addBufferListener(
423 wp<Camera3StreamBufferListener> listener) = 0;
424 virtual void removeBufferListener(
425 const sp<Camera3StreamBufferListener>& listener) = 0;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700426
427 /**
428 * Setting listner will remove previous listener (if exists)
429 * Only allow set listener during stream configuration because stream is guaranteed to be IDLE
430 * at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks.
431 * Client is responsible to keep the listener object alive throughout the lifecycle of this
432 * Camera3Stream.
433 */
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700434 virtual void setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener) = 0;
Emilian Peev538c90e2018-12-17 18:03:19 +0000435
436 /**
437 * Notify buffer stream listeners about incoming request with particular frame number.
438 */
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800439 virtual void fireBufferRequestForFrameNumber(uint64_t frameNumber,
440 const CameraMetadata& settings) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700441};
442
443} // namespace camera3
444
445} // namespace android
446
447#endif