blob: 471b39388f181db46fcf2d9ceb089c80fe937f01 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
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_H
18#define ANDROID_SERVERS_CAMERA3_STREAM_H
19
20#include <gui/Surface.h>
21#include <utils/RefBase.h>
22#include <utils/String8.h>
23#include <utils/String16.h>
Igor Murashkin2fba5842013-04-22 14:03:54 -070024#include <utils/List.h>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080025
26#include "hardware/camera3.h"
27
Igor Murashkin2fba5842013-04-22 14:03:54 -070028#include "Camera3StreamBufferListener.h"
29#include "Camera3StreamInterface.h"
30
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080031namespace android {
32
33namespace camera3 {
34
35/**
36 * A class for managing a single stream of input or output data from the camera
37 * device.
38 *
39 * The stream has an internal state machine to track whether it's
40 * connected/configured/etc.
41 *
42 * States:
43 *
44 * STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding
45 * buffers may still be returned.
46 *
47 * STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot
48 * be gotten yet. Not connected to any endpoint, no buffers are registered
49 * with the HAL.
50 *
51 * STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this
52 * time, the usage, max_buffers, and priv fields of camera3_stream returned by
53 * startConfiguration() may be modified.
54 *
55 * STATE_IN_RE_CONFIG: Configuration has started, and the stream has been
56 * configured before. Need to track separately from IN_CONFIG to avoid
57 * re-registering buffers with HAL.
58 *
59 * STATE_CONFIGURED: Stream is configured, and has registered buffers with the
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070060 * HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv
61 * pointer may still be modified.
62 *
63 * STATE_PREPARING: The stream's buffers are being pre-allocated for use. On
64 * older HALs, this is done as part of configuration, but in newer HALs
65 * buffers may be allocated at time of first use. But some use cases require
66 * buffer allocation upfront, to minmize disruption due to lengthy allocation
67 * duration. In this state, only prepareNextBuffer() and cancelPrepare()
68 * may be called.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080069 *
70 * Transition table:
71 *
72 * <none> => STATE_CONSTRUCTED:
73 * When constructed with valid arguments
74 * <none> => STATE_ERROR:
75 * When constructed with invalid arguments
76 * STATE_CONSTRUCTED => STATE_IN_CONFIG:
77 * When startConfiguration() is called
78 * STATE_IN_CONFIG => STATE_CONFIGURED:
79 * When finishConfiguration() is called
80 * STATE_IN_CONFIG => STATE_ERROR:
81 * When finishConfiguration() fails to allocate or register buffers.
82 * STATE_CONFIGURED => STATE_IN_RE_CONFIG: *
83 * When startConfiguration() is called again, after making sure stream is
84 * idle with waitUntilIdle().
85 * STATE_IN_RE_CONFIG => STATE_CONFIGURED:
86 * When finishConfiguration() is called.
87 * STATE_IN_RE_CONFIG => STATE_ERROR:
88 * When finishConfiguration() fails to allocate or register buffers.
89 * STATE_CONFIGURED => STATE_CONSTRUCTED:
90 * When disconnect() is called after making sure stream is idle with
91 * waitUntilIdle().
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070092 * STATE_CONFIGURED => STATE_PREPARING:
93 * When startPrepare is called before the stream has a buffer
94 * queued back into it for the first time.
95 * STATE_PREPARING => STATE_CONFIGURED:
96 * When sufficient prepareNextBuffer calls have been made to allocate
97 * all stream buffers, or cancelPrepare is called.
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070098 * STATE_CONFIGURED => STATE_ABANDONED:
99 * When the buffer queue of the stream is abandoned.
Igor Murashkin13d315e2014-04-03 18:09:04 -0700100 *
101 * Status Tracking:
102 * Each stream is tracked by StatusTracker as a separate component,
103 * depending on the handed out buffer count. The state must be STATE_CONFIGURED
104 * in order for the component to be marked.
105 *
106 * It's marked in one of two ways:
107 *
108 * - ACTIVE: One or more buffers have been handed out (with #getBuffer).
109 * - IDLE: All buffers have been returned (with #returnBuffer), and their
110 * respective release_fence(s) have been signaled.
111 *
112 * A typical use case is output streams. When the HAL has any buffers
113 * dequeued, the stream is marked ACTIVE. When the HAL returns all buffers
114 * (e.g. if no capture requests are active), the stream is marked IDLE.
115 * In this use case, the app consumer does not affect the component status.
116 *
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800117 */
118class Camera3Stream :
119 protected camera3_stream,
Igor Murashkin2fba5842013-04-22 14:03:54 -0700120 public virtual Camera3StreamInterface,
121 public virtual RefBase {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800122 public:
123
124 virtual ~Camera3Stream();
125
126 static Camera3Stream* cast(camera3_stream *stream);
127 static const Camera3Stream* cast(const camera3_stream *stream);
128
129 /**
130 * Get the stream's ID
131 */
132 int getId() const;
133
134 /**
Zhijun He125684a2015-12-26 15:07:30 -0800135 * Get the output stream set id.
136 */
137 int getStreamSetId() const;
138
139 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800140 * Get the stream's dimensions and format
141 */
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800142 uint32_t getWidth() const;
143 uint32_t getHeight() const;
144 int getFormat() const;
145 android_dataspace getDataSpace() const;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800146
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800147 camera3_stream* asHalStream() override {
148 return this;
149 }
150
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800151 /**
152 * Start the stream configuration process. Returns a handle to the stream's
153 * information to be passed into the HAL device's configure_streams call.
154 *
155 * Until finishConfiguration() is called, no other methods on the stream may be
156 * called. The usage and max_buffers fields of camera3_stream may be modified
157 * between start/finishConfiguration, but may not be changed after that.
158 * The priv field of camera3_stream may be modified at any time after
159 * startConfiguration.
160 *
161 * Returns NULL in case of error starting configuration.
162 */
163 camera3_stream* startConfiguration();
164
165 /**
166 * Check if the stream is mid-configuration (start has been called, but not
167 * finish). Used for lazy completion of configuration.
168 */
169 bool isConfiguring() const;
170
171 /**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800172 * Completes the stream configuration process. The stream information
173 * structure returned by startConfiguration() may no longer be modified
174 * after this call, but can still be read until the destruction of the
175 * stream.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800176 *
177 * Returns:
178 * OK on a successful configuration
179 * NO_INIT in case of a serious error from the HAL device
180 * NO_MEMORY in case of an error registering buffers
Zhijun He5d677d12016-05-29 16:52:39 -0700181 * INVALID_OPERATION in case connecting to the consumer failed or consumer
182 * doesn't exist yet.
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800183 */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800184 status_t finishConfiguration();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185
186 /**
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700187 * Cancels the stream configuration process. This returns the stream to the
188 * initial state, allowing it to be configured again later.
189 * This is done if the HAL rejects the proposed combined stream configuration
190 */
191 status_t cancelConfiguration();
192
193 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700194 * Determine whether the stream has already become in-use (has received
195 * a valid filled buffer), which determines if a stream can still have
196 * prepareNextBuffer called on it.
197 */
198 bool isUnpreparable();
199
200 /**
201 * Start stream preparation. May only be called in the CONFIGURED state,
Ruben Brunkc78ac262015-08-13 17:58:46 -0700202 * when no valid buffers have yet been returned to this stream. Prepares
203 * up to maxCount buffers, or the maximum number of buffers needed by the
204 * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700205 *
206 * If no prepartion is necessary, returns OK and does not transition to
207 * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
208 * to PREPARING.
209 *
210 * This call performs no allocation, so is quick to call.
211 *
212 * Returns:
213 * OK if no more buffers need to be preallocated
214 * NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
215 * buffer pre-allocation, and transitions to the PREPARING state.
216 * NO_INIT in case of a serious error from the HAL device
217 * INVALID_OPERATION if called when not in CONFIGURED state, or a
218 * valid buffer has already been returned to this stream.
219 */
Ruben Brunkc78ac262015-08-13 17:58:46 -0700220 status_t startPrepare(int maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700221
222 /**
223 * Check if the stream is mid-preparing.
224 */
225 bool isPreparing() const;
226
227 /**
228 * Continue stream buffer preparation by allocating the next
229 * buffer for this stream. May only be called in the PREPARED state.
230 *
231 * Returns OK and transitions to the CONFIGURED state if all buffers
232 * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
233 *
234 * This call allocates one buffer, which may take several milliseconds for
235 * large buffers.
236 *
237 * Returns:
238 * OK if no more buffers need to be preallocated, and transitions
239 * to the CONFIGURED state.
240 * NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
241 * buffer pre-allocation.
242 * NO_INIT in case of a serious error from the HAL device
243 * INVALID_OPERATION if called when not in CONFIGURED state, or a
244 * valid buffer has already been returned to this stream.
245 */
246 status_t prepareNextBuffer();
247
248 /**
249 * Cancel stream preparation early. In case allocation needs to be
250 * stopped, this method transitions the stream back to the CONFIGURED state.
251 * Buffers that have been allocated with prepareNextBuffer remain that way,
252 * but a later use of prepareNextBuffer will require just as many
253 * calls as if the earlier prepare attempt had not existed.
254 *
255 * Returns:
256 * OK if cancellation succeeded, and transitions to the CONFIGURED state
257 * INVALID_OPERATION if not in the PREPARING state
258 * NO_INIT in case of a serious error from the HAL device
259 */
260 status_t cancelPrepare();
261
262 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700263 * Tear down memory for this stream. This frees all unused gralloc buffers
264 * allocated for this stream, but leaves it ready for operation afterward.
265 *
266 * May only be called in the CONFIGURED state, and keeps the stream in
267 * the CONFIGURED state.
268 *
269 * Returns:
270 * OK if teardown succeeded.
271 * INVALID_OPERATION if not in the CONFIGURED state
272 * NO_INIT in case of a serious error from the HAL device
273 */
274 status_t tearDown();
275
276 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800277 * Fill in the camera3_stream_buffer with the next valid buffer for this
278 * stream, to hand over to the HAL.
279 *
280 * This method may only be called once finishConfiguration has been called.
281 * For bidirectional streams, this method applies to the output-side
282 * buffers.
283 *
284 */
285 status_t getBuffer(camera3_stream_buffer *buffer);
286
287 /**
288 * Return a buffer to the stream after use by the HAL.
289 *
290 * This method may only be called for buffers provided by getBuffer().
291 * For bidirectional streams, this method applies to the output-side buffers
292 */
293 status_t returnBuffer(const camera3_stream_buffer &buffer,
294 nsecs_t timestamp);
295
296 /**
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700297 * Fill in the camera3_stream_buffer with the next valid buffer for this
298 * stream, to hand over to the HAL.
299 *
300 * This method may only be called once finishConfiguration has been called.
301 * For bidirectional streams, this method applies to the input-side
302 * buffers.
303 *
304 */
305 status_t getInputBuffer(camera3_stream_buffer *buffer);
306
307 /**
308 * Return a buffer to the stream after use by the HAL.
309 *
310 * This method may only be called for buffers provided by getBuffer().
311 * For bidirectional streams, this method applies to the input-side buffers
312 */
313 status_t returnInputBuffer(const camera3_stream_buffer &buffer);
314
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700315 // get the buffer producer of the input buffer queue.
316 // only apply to input streams.
317 status_t getInputBufferProducer(sp<IGraphicBufferProducer> *producer);
318
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700319 /**
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800320 * Whether any of the stream's buffers are currently in use by the HAL,
321 * including buffers that have been returned but not yet had their
322 * release fence signaled.
323 */
324 bool hasOutstandingBuffers() const;
325
326 enum {
327 TIMEOUT_NEVER = -1
328 };
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700329
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800330 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700331 * Set the status tracker to notify about idle transitions
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800332 */
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700333 virtual status_t setStatusTracker(sp<StatusTracker> statusTracker);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334
335 /**
336 * Disconnect stream from its non-HAL endpoint. After this,
337 * start/finishConfiguration must be called before the stream can be used
338 * again. This cannot be called if the stream has outstanding dequeued
339 * buffers.
340 */
341 status_t disconnect();
342
343 /**
344 * Debug dump of the stream's state.
345 */
346 virtual void dump(int fd, const Vector<String16> &args) const = 0;
347
Zhijun Hef0d962a2014-06-30 10:24:11 -0700348 /**
349 * Add a camera3 buffer listener. Adding the same listener twice has
350 * no effect.
351 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700352 void addBufferListener(
353 wp<Camera3StreamBufferListener> listener);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700354
355 /**
356 * Remove a camera3 buffer listener. Removing the same listener twice
357 * or the listener that was never added has no effect.
358 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700359 void removeBufferListener(
360 const sp<Camera3StreamBufferListener>& listener);
361
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700362 /**
363 * Return if the buffer queue of the stream is abandoned.
364 */
365 bool isAbandoned() const;
366
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800367 protected:
368 const int mId;
Zhijun He125684a2015-12-26 15:07:30 -0800369 /**
370 * Stream set id, used to indicate which group of this stream belongs to for buffer sharing
371 * across multiple streams.
372 *
373 * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream
374 * doesn't intend to share buffers with any other streams, and this stream will fall back to
375 * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation.
376 * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to
377 * manage the buffer allocations; the BufferQueue will only handle the buffer transaction
378 * between the producer and consumer. For this case, upon successfully registration, the streams
379 * with the same stream set id will potentially share the buffers allocated by
380 * Camera3BufferManager.
381 */
382 const int mSetId;
383
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800384 const String8 mName;
385 // Zero for formats with fixed buffer size for given dimensions.
386 const size_t mMaxSize;
387
388 enum {
389 STATE_ERROR,
390 STATE_CONSTRUCTED,
391 STATE_IN_CONFIG,
392 STATE_IN_RECONFIG,
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700393 STATE_CONFIGURED,
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700394 STATE_PREPARING,
395 STATE_ABANDONED
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800396 } mState;
397
398 mutable Mutex mLock;
399
400 Camera3Stream(int id, camera3_stream_type type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800401 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800402 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
403 int setId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800404
405 /**
406 * Interface to be implemented by derived classes
407 */
408
409 // getBuffer / returnBuffer implementations
410
411 // Since camera3_stream_buffer includes a raw pointer to the stream,
412 // cast to camera3_stream*, implementations must increment the
413 // refcount of the stream manually in getBufferLocked, and decrement it in
414 // returnBufferLocked.
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700415 virtual status_t getBufferLocked(camera3_stream_buffer *buffer);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800416 virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer,
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700417 nsecs_t timestamp);
418 virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer);
419 virtual status_t returnInputBufferLocked(
420 const camera3_stream_buffer &buffer);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800421 virtual bool hasOutstandingBuffersLocked() const = 0;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700422 // Get the buffer producer of the input buffer queue. Only apply to input streams.
423 virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer);
424
Igor Murashkine2172be2013-05-28 15:31:39 -0700425 // Can return -ENOTCONN when we are already disconnected (not an error)
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800426 virtual status_t disconnectLocked() = 0;
427
428 // Configure the buffer queue interface to the other end of the stream,
429 // after the HAL has provided usage and max_buffers values. After this call,
430 // the stream must be ready to produce all buffers for registration with
431 // HAL.
432 virtual status_t configureQueueLocked() = 0;
433
434 // Get the total number of buffers in the queue
435 virtual size_t getBufferCountLocked() = 0;
436
Zhijun He6adc9cc2014-04-15 14:09:55 -0700437 // Get handout output buffer count.
438 virtual size_t getHandoutOutputBufferCountLocked() = 0;
439
440 // Get handout input buffer count.
441 virtual size_t getHandoutInputBufferCountLocked() = 0;
442
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700443 // Get the usage flags for the other endpoint, or return
444 // INVALID_OPERATION if they cannot be obtained.
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700445 virtual status_t getEndpointUsage(uint32_t *usage) const = 0;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700446
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700447 // Tracking for idle state
448 wp<StatusTracker> mStatusTracker;
449 // Status tracker component ID
450 int mStatusId;
451
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700452 // Tracking for stream prepare - whether this stream can still have
453 // prepareNextBuffer called on it.
454 bool mStreamUnpreparable;
455
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800456 private:
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800457 uint32_t mOldUsage;
458 uint32_t mOldMaxBuffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700459 Condition mOutputBufferReturnedSignal;
460 Condition mInputBufferReturnedSignal;
461 static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800462
463 // Gets all buffers from endpoint and registers them with the HAL.
464 status_t registerBuffersLocked(camera3_device *hal3Device);
465
Igor Murashkin2fba5842013-04-22 14:03:54 -0700466 void fireBufferListenersLocked(const camera3_stream_buffer& buffer,
467 bool acquired, bool output);
468 List<wp<Camera3StreamBufferListener> > mBufferListenerList;
469
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700470 status_t cancelPrepareLocked();
471
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700472 // Return whether the buffer is in the list of outstanding buffers.
473 bool isOutstandingBuffer(const camera3_stream_buffer& buffer);
474
475 // Remove the buffer from the list of outstanding buffers.
476 void removeOutstandingBuffer(const camera3_stream_buffer& buffer);
477
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700478 // Tracking for PREPARING state
479
480 // State of buffer preallocation. Only true if either prepareNextBuffer
481 // has been called sufficient number of times, or stream configuration
482 // had to register buffers with the HAL
483 bool mPrepared;
484
485 Vector<camera3_stream_buffer_t> mPreparedBuffers;
486 size_t mPreparedBufferIdx;
487
Ruben Brunkc78ac262015-08-13 17:58:46 -0700488 // Number of buffers allocated on last prepare call.
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800489 size_t mLastMaxCount;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700490
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700491 // Outstanding buffers dequeued from the stream's buffer queue.
492 List<buffer_handle_t> mOutstandingBuffers;
493
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800494}; // class Camera3Stream
495
496}; // namespace camera3
497
498}; // namespace android
499
500#endif