blob: 73f501a57b0fa8b6d606b343e483fbbfe2552785 [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
26struct camera3_stream_buffer;
27
28namespace android {
29
30namespace camera3 {
31
Zhijun He125684a2015-12-26 15:07:30 -080032enum {
33 /**
34 * This stream set ID indicates that the set ID is invalid, and this stream doesn't intend to
35 * share buffers with any other stream. It is illegal to register this kind of stream to
36 * Camera3BufferManager.
37 */
38 CAMERA3_STREAM_SET_ID_INVALID = -1,
39
40 /**
41 * Invalid output stream ID.
42 */
43 CAMERA3_STREAM_ID_INVALID = -1,
44};
45
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070046class StatusTracker;
47
Emilian Peev40ead602017-09-26 15:46:36 +010048// OutputStreamInfo describes the property of a camera stream.
49class OutputStreamInfo {
50 public:
51 int width;
52 int height;
53 int format;
54 android_dataspace dataSpace;
55 uint64_t consumerUsage;
56 bool finalized = false;
57 OutputStreamInfo() :
58 width(-1), height(-1), format(-1), dataSpace(HAL_DATASPACE_UNKNOWN),
59 consumerUsage(0) {}
60 OutputStreamInfo(int _width, int _height, int _format, android_dataspace _dataSpace,
61 uint64_t _consumerUsage) :
62 width(_width), height(_height), format(_format),
63 dataSpace(_dataSpace), consumerUsage(_consumerUsage) {}
64};
65
Igor Murashkinae500e52013-04-22 14:03:54 -070066/**
67 * An interface for managing a single stream of input and/or output data from
68 * the camera device.
69 */
70class Camera3StreamInterface : public virtual RefBase {
71 public:
Ruben Brunkc78ac262015-08-13 17:58:46 -070072
73 enum {
74 ALLOCATE_PIPELINE_MAX = 0, // Allocate max buffers used by a given surface
75 };
76
Igor Murashkinae500e52013-04-22 14:03:54 -070077 /**
78 * Get the stream's ID
79 */
80 virtual int getId() const = 0;
81
82 /**
Zhijun He125684a2015-12-26 15:07:30 -080083 * Get the output stream set id.
84 */
85 virtual int getStreamSetId() const = 0;
86
87 /**
Igor Murashkinae500e52013-04-22 14:03:54 -070088 * Get the stream's dimensions and format
89 */
90 virtual uint32_t getWidth() const = 0;
91 virtual uint32_t getHeight() const = 0;
92 virtual int getFormat() const = 0;
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -070093 virtual android_dataspace getDataSpace() const = 0;
Emilian Peev710c1422017-08-30 11:19:38 +010094 virtual void setFormatOverride(bool formatOverriden) = 0;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -070095 virtual bool isFormatOverridden() const = 0;
96 virtual int getOriginalFormat() const = 0;
97 virtual void setDataSpaceOverride(bool dataSpaceOverriden) = 0;
98 virtual bool isDataSpaceOverridden() const = 0;
99 virtual android_dataspace getOriginalDataSpace() const = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700100
101 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800102 * Get a HAL3 handle for the stream, without starting stream configuration.
103 */
104 virtual camera3_stream* asHalStream() = 0;
105
106 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700107 * Start the stream configuration process. Returns a handle to the stream's
108 * information to be passed into the HAL device's configure_streams call.
109 *
110 * Until finishConfiguration() is called, no other methods on the stream may
111 * be called. The usage and max_buffers fields of camera3_stream may be
112 * modified between start/finishConfiguration, but may not be changed after
113 * that. The priv field of camera3_stream may be modified at any time after
114 * startConfiguration.
115 *
116 * Returns NULL in case of error starting configuration.
117 */
118 virtual camera3_stream* startConfiguration() = 0;
119
120 /**
121 * Check if the stream is mid-configuration (start has been called, but not
122 * finish). Used for lazy completion of configuration.
123 */
124 virtual bool isConfiguring() const = 0;
125
126 /**
127 * Completes the stream configuration process. During this call, the stream
128 * may call the device's register_stream_buffers() method. The stream
129 * information structure returned by startConfiguration() may no longer be
130 * modified after this call, but can still be read until the destruction of
131 * the stream.
132 *
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700133 * streamReconfigured: set to true when a stream is being reconfigured.
134 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700135 * Returns:
136 * OK on a successful configuration
137 * NO_INIT in case of a serious error from the HAL device
138 * NO_MEMORY in case of an error registering buffers
139 * INVALID_OPERATION in case connecting to the consumer failed
140 */
Yin-Chia Yeh573a2702019-04-17 10:08:55 -0700141 virtual status_t finishConfiguration(/*out*/bool* streamReconfigured = nullptr) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700142
143 /**
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700144 * Cancels the stream configuration process. This returns the stream to the
145 * initial state, allowing it to be configured again later.
146 * This is done if the HAL rejects the proposed combined stream configuration
147 */
148 virtual status_t cancelConfiguration() = 0;
149
150 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700151 * Determine whether the stream has already become in-use (has received
152 * a valid filled buffer), which determines if a stream can still have
153 * prepareNextBuffer called on it.
154 */
155 virtual bool isUnpreparable() = 0;
156
157 /**
158 * Start stream preparation. May only be called in the CONFIGURED state,
Ruben Brunkc78ac262015-08-13 17:58:46 -0700159 * when no valid buffers have yet been returned to this stream. Prepares
160 * up to maxCount buffers, or the maximum number of buffers needed by the
161 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700162 *
163 * If no prepartion is necessary, returns OK and does not transition to
164 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
165 * to PREPARING.
166 *
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700167 * blockRequest specifies whether prepare will block upcoming capture
168 * request. This flag should only be set to false if the caller guarantees
169 * the whole buffer preparation process is done before capture request
170 * comes in.
171 *
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700172 * Returns:
173 * OK if no more buffers need to be preallocated
174 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
175 * buffer pre-allocation, and transitions to the PREPARING state.
176 * NO_INIT in case of a serious error from the HAL device
177 * INVALID_OPERATION if called when not in CONFIGURED state, or a
178 * valid buffer has already been returned to this stream.
179 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700180 virtual status_t startPrepare(int maxCount, bool blockRequest) = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700181
182 /**
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700183 * Check if the request on a stream is blocked by prepare.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700184 */
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700185 virtual bool isBlockedByPrepare() const = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700186
187 /**
188 * Continue stream buffer preparation by allocating the next
189 * buffer for this stream. May only be called in the PREPARED state.
190 *
191 * Returns OK and transitions to the CONFIGURED state if all buffers
192 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
193 *
194 * Returns:
195 * OK if no more buffers need to be preallocated, and transitions
196 * to the CONFIGURED state.
197 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
198 * buffer pre-allocation.
199 * NO_INIT in case of a serious error from the HAL device
200 * INVALID_OPERATION if called when not in CONFIGURED state, or a
201 * valid buffer has already been returned to this stream.
202 */
203 virtual status_t prepareNextBuffer() = 0;
204
205 /**
206 * Cancel stream preparation early. In case allocation needs to be
207 * stopped, this method transitions the stream back to the CONFIGURED state.
208 * Buffers that have been allocated with prepareNextBuffer remain that way,
209 * but a later use of prepareNextBuffer will require just as many
210 * calls as if the earlier prepare attempt had not existed.
211 *
212 * Returns:
213 * OK if cancellation succeeded, and transitions to the CONFIGURED state
214 * INVALID_OPERATION if not in the PREPARING state
215 * NO_INIT in case of a serious error from the HAL device
216 */
217 virtual status_t cancelPrepare() = 0;
218
219 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700220 * Tear down memory for this stream. This frees all unused gralloc buffers
221 * allocated for this stream, but leaves it ready for operation afterward.
222 *
223 * May only be called in the CONFIGURED state, and keeps the stream in
224 * the CONFIGURED state.
225 *
226 * Returns:
227 * OK if teardown succeeded.
228 * INVALID_OPERATION if not in the CONFIGURED state
229 * NO_INIT in case of a serious error from the HAL device
230 */
231 virtual status_t tearDown() = 0;
232
233 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700234 * Fill in the camera3_stream_buffer with the next valid buffer for this
235 * stream, to hand over to the HAL.
236 *
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800237 * Multiple surfaces could share the same HAL stream, but a request may
238 * be only for a subset of surfaces. In this case, the
239 * Camera3StreamInterface object needs the surface ID information to acquire
240 * buffers for those surfaces. For the case of single surface for a HAL
241 * stream, surface_ids parameter has no effect.
242 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700243 * This method may only be called once finishConfiguration has been called.
244 * For bidirectional streams, this method applies to the output-side
245 * buffers.
246 *
247 */
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800248 virtual status_t getBuffer(camera3_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700249 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800250 const std::vector<size_t>& surface_ids = std::vector<size_t>()) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700251
252 /**
253 * Return a buffer to the stream after use by the HAL.
254 *
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700255 * Multiple surfaces could share the same HAL stream, but a request may
256 * be only for a subset of surfaces. In this case, the
257 * Camera3StreamInterface object needs the surface ID information to attach
258 * buffers for those surfaces. For the case of single surface for a HAL
259 * stream, surface_ids parameter has no effect.
260 *
Igor Murashkinae500e52013-04-22 14:03:54 -0700261 * This method may only be called for buffers provided by getBuffer().
262 * For bidirectional streams, this method applies to the output-side buffers
263 */
264 virtual status_t returnBuffer(const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700265 nsecs_t timestamp, bool timestampIncreasing = true,
Emilian Peev538c90e2018-12-17 18:03:19 +0000266 const std::vector<size_t>& surface_ids = std::vector<size_t>(),
267 uint64_t frameNumber = 0) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700268
269 /**
270 * Fill in the camera3_stream_buffer with the next valid buffer for this
271 * stream, to hand over to the HAL.
272 *
273 * This method may only be called once finishConfiguration has been called.
274 * For bidirectional streams, this method applies to the input-side
275 * buffers.
276 *
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700277 * Normally this call will block until the handed out buffer count is less than the stream
278 * max buffer count; if respectHalLimit is set to false, this is ignored.
Igor Murashkinae500e52013-04-22 14:03:54 -0700279 */
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700280 virtual status_t getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit = true) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700281
282 /**
283 * Return a buffer to the stream after use by the HAL.
284 *
285 * This method may only be called for buffers provided by getBuffer().
286 * For bidirectional streams, this method applies to the input-side buffers
287 */
288 virtual status_t returnInputBuffer(const camera3_stream_buffer &buffer) = 0;
289
290 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700291 * Get the buffer producer of the input buffer queue.
292 *
293 * This method only applies to input streams.
294 */
295 virtual status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer) = 0;
296
297 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700298 * Whether any of the stream's buffers are currently in use by the HAL,
299 * including buffers that have been returned but not yet had their
300 * release fence signaled.
301 */
302 virtual bool hasOutstandingBuffers() const = 0;
303
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700304 /**
305 * Get number of buffers currently handed out to HAL
306 */
307 virtual size_t getOutstandingBuffersCount() const = 0;
308
Igor Murashkinae500e52013-04-22 14:03:54 -0700309 enum {
310 TIMEOUT_NEVER = -1
311 };
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312
Igor Murashkinae500e52013-04-22 14:03:54 -0700313 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314 * Set the state tracker to use for signaling idle transitions.
Igor Murashkinae500e52013-04-22 14:03:54 -0700315 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700317
318 /**
319 * Disconnect stream from its non-HAL endpoint. After this,
320 * start/finishConfiguration must be called before the stream can be used
321 * again. This cannot be called if the stream has outstanding dequeued
322 * buffers.
323 */
324 virtual status_t disconnect() = 0;
325
326 /**
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700327 * Return if the buffer queue of the stream is abandoned.
328 */
329 virtual bool isAbandoned() const = 0;
330
331 /**
Igor Murashkinae500e52013-04-22 14:03:54 -0700332 * Debug dump of the stream's state.
333 */
334 virtual void dump(int fd, const Vector<String16> &args) const = 0;
335
336 virtual void addBufferListener(
337 wp<Camera3StreamBufferListener> listener) = 0;
338 virtual void removeBufferListener(
339 const sp<Camera3StreamBufferListener>& listener) = 0;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700340
341 /**
342 * Setting listner will remove previous listener (if exists)
343 * Only allow set listener during stream configuration because stream is guaranteed to be IDLE
344 * at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks.
345 * Client is responsible to keep the listener object alive throughout the lifecycle of this
346 * Camera3Stream.
347 */
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700348 virtual void setBufferFreedListener(wp<Camera3StreamBufferFreedListener> listener) = 0;
Emilian Peev538c90e2018-12-17 18:03:19 +0000349
350 /**
351 * Notify buffer stream listeners about incoming request with particular frame number.
352 */
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800353 virtual void fireBufferRequestForFrameNumber(uint64_t frameNumber,
354 const CameraMetadata& settings) = 0;
Igor Murashkinae500e52013-04-22 14:03:54 -0700355};
356
357} // namespace camera3
358
359} // namespace android
360
361#endif