1fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala/*
2fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * Copyright (C) 2013 The Android Open Source Project
3fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
4fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * you may not use this file except in compliance with the License.
6fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * You may obtain a copy of the License at
7fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
8fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
10fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * See the License for the specific language governing permissions and
14fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * limitations under the License.
15fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala */
16fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
17fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#ifndef ANDROID_SERVERS_CAMERA3_STREAM_H
18fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#define ANDROID_SERVERS_CAMERA3_STREAM_H
19fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
20fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#include <gui/Surface.h>
21fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#include <utils/RefBase.h>
22fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#include <utils/String8.h>
23fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#include <utils/String16.h>
242fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin#include <utils/List.h>
25fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
26fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#include "hardware/camera3.h"
27fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
286c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang#include "utils/LatencyHistogram.h"
292fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin#include "Camera3StreamBufferListener.h"
302fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin#include "Camera3StreamInterface.h"
312fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin
32fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvalanamespace android {
33fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
34fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvalanamespace camera3 {
35fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
36fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala/**
37fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * A class for managing a single stream of input or output data from the camera
38fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * device.
39fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
40fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * The stream has an internal state machine to track whether it's
41fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * connected/configured/etc.
42fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
43fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * States:
44fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
45fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *  STATE_ERROR: A serious error has occurred, stream is unusable. Outstanding
46fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    buffers may still be returned.
47fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
48fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *  STATE_CONSTRUCTED: The stream is ready for configuration, but buffers cannot
49fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    be gotten yet. Not connected to any endpoint, no buffers are registered
50fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    with the HAL.
51fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
52fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *  STATE_IN_CONFIG: Configuration has started, but not yet concluded. During this
53fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    time, the usage, max_buffers, and priv fields of camera3_stream returned by
54fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    startConfiguration() may be modified.
55fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
56fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *  STATE_IN_RE_CONFIG: Configuration has started, and the stream has been
57fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    configured before. Need to track separately from IN_CONFIG to avoid
58fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    re-registering buffers with HAL.
59fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
60fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *  STATE_CONFIGURED: Stream is configured, and has registered buffers with the
614d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    HAL (if necessary). The stream's getBuffer/returnBuffer work. The priv
624d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    pointer may still be modified.
634d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *
644d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *  STATE_PREPARING: The stream's buffers are being pre-allocated for use.  On
654d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    older HALs, this is done as part of configuration, but in newer HALs
664d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    buffers may be allocated at time of first use. But some use cases require
674d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    buffer allocation upfront, to minmize disruption due to lengthy allocation
684d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    duration.  In this state, only prepareNextBuffer() and cancelPrepare()
694d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    may be called.
70fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
71fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala * Transition table:
72fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *
73fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    <none>               => STATE_CONSTRUCTED:
74fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When constructed with valid arguments
75fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    <none>               => STATE_ERROR:
76fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When constructed with invalid arguments
77fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_CONSTRUCTED    => STATE_IN_CONFIG:
78fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When startConfiguration() is called
79fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_IN_CONFIG      => STATE_CONFIGURED:
80fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When finishConfiguration() is called
81fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_IN_CONFIG      => STATE_ERROR:
82fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When finishConfiguration() fails to allocate or register buffers.
83fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_CONFIGURED     => STATE_IN_RE_CONFIG:  *
84fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When startConfiguration() is called again, after making sure stream is
85fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        idle with waitUntilIdle().
86fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_IN_RE_CONFIG   => STATE_CONFIGURED:
87fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When finishConfiguration() is called.
88fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_IN_RE_CONFIG   => STATE_ERROR:
89fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When finishConfiguration() fails to allocate or register buffers.
90fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *    STATE_CONFIGURED     => STATE_CONSTRUCTED:
91fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        When disconnect() is called after making sure stream is idle with
92fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala *        waitUntilIdle().
934d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    STATE_CONFIGURED     => STATE_PREPARING:
944d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *        When startPrepare is called before the stream has a buffer
954d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *        queued back into it for the first time.
964d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *    STATE_PREPARING      => STATE_CONFIGURED:
974d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *        When sufficient prepareNextBuffer calls have been made to allocate
984d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala *        all stream buffers, or cancelPrepare is called.
99e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen *    STATE_CONFIGURED     => STATE_ABANDONED:
100e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen *        When the buffer queue of the stream is abandoned.
10113d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *
10213d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin * Status Tracking:
10313d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    Each stream is tracked by StatusTracker as a separate component,
10413d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    depending on the handed out buffer count. The state must be STATE_CONFIGURED
10513d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    in order for the component to be marked.
10613d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *
10713d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    It's marked in one of two ways:
10813d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *
10913d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    - ACTIVE: One or more buffers have been handed out (with #getBuffer).
11013d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    - IDLE: All buffers have been returned (with #returnBuffer), and their
11113d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *          respective release_fence(s) have been signaled.
11213d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *
11313d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    A typical use case is output streams. When the HAL has any buffers
11413d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    dequeued, the stream is marked ACTIVE. When the HAL returns all buffers
11513d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    (e.g. if no capture requests are active), the stream is marked IDLE.
11613d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *    In this use case, the app consumer does not affect the component status.
11713d315eb8c0848ea0584b9fb1d27bab55bc8158bIgor Murashkin *
118fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala */
119fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvalaclass Camera3Stream :
120fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        protected camera3_stream,
1212fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin        public virtual Camera3StreamInterface,
1222fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin        public virtual RefBase {
123fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala  public:
124fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
125fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual ~Camera3Stream();
126fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
127fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    static Camera3Stream*       cast(camera3_stream *stream);
128fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    static const Camera3Stream* cast(const camera3_stream *stream);
129fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
130fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
131fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Get the stream's ID
132fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
133fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    int              getId() const;
134fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
135fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
136125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * Get the output stream set id.
137125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     */
138125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He    int              getStreamSetId() const;
139125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He
140125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He    /**
141fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Get the stream's dimensions and format
142fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
1433d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala    uint32_t          getWidth() const;
1443d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala    uint32_t          getHeight() const;
1453d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala    int               getFormat() const;
1463d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala    android_dataspace getDataSpace() const;
147fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
1480b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala    camera3_stream*   asHalStream() override {
1490b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala        return this;
1500b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala    }
1510b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala
152fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
153fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Start the stream configuration process. Returns a handle to the stream's
154fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * information to be passed into the HAL device's configure_streams call.
155fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
156fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Until finishConfiguration() is called, no other methods on the stream may be
157fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * called. The usage and max_buffers fields of camera3_stream may be modified
158fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * between start/finishConfiguration, but may not be changed after that.
159fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * The priv field of camera3_stream may be modified at any time after
160fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * startConfiguration.
161fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
162fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Returns NULL in case of error starting configuration.
163fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
164fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    camera3_stream*  startConfiguration();
165fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
166fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
167fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Check if the stream is mid-configuration (start has been called, but not
168fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * finish).  Used for lazy completion of configuration.
169fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
170fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    bool             isConfiguring() const;
171fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
172fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
1730b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala     * Completes the stream configuration process. The stream information
1740b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala     * structure returned by startConfiguration() may no longer be modified
1750b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala     * after this call, but can still be read until the destruction of the
1760b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala     * stream.
177fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
178fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Returns:
179fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *   OK on a successful configuration
180fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *   NO_INIT in case of a serious error from the HAL device
181fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *   NO_MEMORY in case of an error registering buffers
1825d677d1f0879d5101e38df480a38228a64d63959Zhijun He     *   INVALID_OPERATION in case connecting to the consumer failed or consumer
1835d677d1f0879d5101e38df480a38228a64d63959Zhijun He     *       doesn't exist yet.
184fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
1850b1cb14c804d7d4343fe91c78578da8db9a678d5Eino-Ville Talvala    status_t         finishConfiguration();
186fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
187fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
1881754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala     * Cancels the stream configuration process. This returns the stream to the
1891754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala     * initial state, allowing it to be configured again later.
1901754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala     * This is done if the HAL rejects the proposed combined stream configuration
1911754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala     */
1921754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala    status_t         cancelConfiguration();
1931754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala
1941754351d9199721e7e7943461689e399ef015260Eino-Ville Talvala    /**
1954d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Determine whether the stream has already become in-use (has received
1964d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * a valid filled buffer), which determines if a stream can still have
1974d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * prepareNextBuffer called on it.
1984d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     */
1994d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    bool             isUnpreparable();
2004d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
2014d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    /**
2024d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Start stream preparation. May only be called in the CONFIGURED state,
203c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk     * when no valid buffers have yet been returned to this stream. Prepares
204c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk     * up to maxCount buffers, or the maximum number of buffers needed by the
205c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk     * pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
2064d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2074d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * If no prepartion is necessary, returns OK and does not transition to
2084d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * PREPARING state. Otherwise, returns NOT_ENOUGH_DATA and transitions
2094d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * to PREPARING.
2104d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2114d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * This call performs no allocation, so is quick to call.
2124d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2134d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Returns:
2144d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    OK if no more buffers need to be preallocated
2154d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    NOT_ENOUGH_DATA if calls to prepareNextBuffer are needed to finish
2164d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *        buffer pre-allocation, and transitions to the PREPARING state.
2174d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    NO_INIT in case of a serious error from the HAL device
2184d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    INVALID_OPERATION if called when not in CONFIGURED state, or a
2194d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *        valid buffer has already been returned to this stream.
2204d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     */
221c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk    status_t         startPrepare(int maxCount);
2224d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
2234d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    /**
2244d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Check if the stream is mid-preparing.
2254d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     */
2264d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    bool             isPreparing() const;
2274d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
2284d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    /**
2294d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Continue stream buffer preparation by allocating the next
2304d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * buffer for this stream.  May only be called in the PREPARED state.
2314d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2324d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Returns OK and transitions to the CONFIGURED state if all buffers
2334d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * are allocated after the call concludes. Otherwise returns NOT_ENOUGH_DATA.
2344d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2354d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * This call allocates one buffer, which may take several milliseconds for
2364d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * large buffers.
2374d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2384d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Returns:
2394d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    OK if no more buffers need to be preallocated, and transitions
2404d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *        to the CONFIGURED state.
2414d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    NOT_ENOUGH_DATA if more calls to prepareNextBuffer are needed to finish
2424d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *        buffer pre-allocation.
2434d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    NO_INIT in case of a serious error from the HAL device
2444d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    INVALID_OPERATION if called when not in CONFIGURED state, or a
2454d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *        valid buffer has already been returned to this stream.
2464d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     */
2474d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    status_t         prepareNextBuffer();
2484d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
2494d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    /**
2504d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Cancel stream preparation early. In case allocation needs to be
2514d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * stopped, this method transitions the stream back to the CONFIGURED state.
2524d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Buffers that have been allocated with prepareNextBuffer remain that way,
2534d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * but a later use of prepareNextBuffer will require just as many
2544d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * calls as if the earlier prepare attempt had not existed.
2554d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *
2564d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     * Returns:
2574d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    OK if cancellation succeeded, and transitions to the CONFIGURED state
2584d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    INVALID_OPERATION if not in the PREPARING state
2594d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     *    NO_INIT in case of a serious error from the HAL device
2604d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala     */
2614d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    status_t        cancelPrepare();
2624d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
2634d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    /**
264b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     * Tear down memory for this stream. This frees all unused gralloc buffers
265b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     * allocated for this stream, but leaves it ready for operation afterward.
266b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     *
267b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     * May only be called in the CONFIGURED state, and keeps the stream in
268b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     * the CONFIGURED state.
269b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     *
270b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     * Returns:
271b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     *    OK if teardown succeeded.
272b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     *    INVALID_OPERATION if not in the CONFIGURED state
273b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     *    NO_INIT in case of a serious error from the HAL device
274b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala     */
275b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala    status_t       tearDown();
276b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala
277b25e3c87724b6147ed1da7c1d6617c39bfce2fbfEino-Ville Talvala    /**
278fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Fill in the camera3_stream_buffer with the next valid buffer for this
279fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * stream, to hand over to the HAL.
280fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
281bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang     * Multiple surfaces could share the same HAL stream, but a request may
282bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang     * be only for a subset of surfaces. In this case, the
283bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang     * Camera3StreamInterface object needs the surface ID information to acquire
284bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang     * buffers for those surfaces.
285bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang     *
286fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * This method may only be called once finishConfiguration has been called.
287fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * For bidirectional streams, this method applies to the output-side
288fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * buffers.
289fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
290fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
291bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang    status_t         getBuffer(camera3_stream_buffer *buffer,
292bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang            const std::vector<size_t>& surface_ids = std::vector<size_t>());
293fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
294fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
295fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Return a buffer to the stream after use by the HAL.
296fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     *
297fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * This method may only be called for buffers provided by getBuffer().
298fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * For bidirectional streams, this method applies to the output-side buffers
299fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
300fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    status_t         returnBuffer(const camera3_stream_buffer &buffer,
301fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala            nsecs_t timestamp);
302fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
303fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
3045a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * Fill in the camera3_stream_buffer with the next valid buffer for this
3055a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * stream, to hand over to the HAL.
3065a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     *
3075a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * This method may only be called once finishConfiguration has been called.
3085a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * For bidirectional streams, this method applies to the input-side
3095a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * buffers.
3105a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     *
3115a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     */
3125a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    status_t         getInputBuffer(camera3_stream_buffer *buffer);
3135a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin
3145a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    /**
3155a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * Return a buffer to the stream after use by the HAL.
3165a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     *
3175a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * This method may only be called for buffers provided by getBuffer().
3185a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     * For bidirectional streams, this method applies to the input-side buffers
3195a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin     */
3205a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    status_t         returnInputBuffer(const camera3_stream_buffer &buffer);
3215a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin
322618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // get the buffer producer of the input buffer queue.
323618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // only apply to input streams.
324618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    status_t         getInputBufferProducer(sp<IGraphicBufferProducer> *producer);
325618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen
3265a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    /**
327fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Whether any of the stream's buffers are currently in use by the HAL,
328fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * including buffers that have been returned but not yet had their
329fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * release fence signaled.
330fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
331fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    bool             hasOutstandingBuffers() const;
332fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
333fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    enum {
334fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        TIMEOUT_NEVER = -1
335fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    };
336f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala
337fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
338f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala     * Set the status tracker to notify about idle transitions
339fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
340f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    virtual status_t setStatusTracker(sp<StatusTracker> statusTracker);
341fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
342fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
343fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Disconnect stream from its non-HAL endpoint. After this,
344fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * start/finishConfiguration must be called before the stream can be used
345fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * again. This cannot be called if the stream has outstanding dequeued
346fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * buffers.
347fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
348fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    status_t         disconnect();
349fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
350fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
351fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Debug dump of the stream's state.
352fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
3536c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang    virtual void     dump(int fd, const Vector<String16> &args) const;
354fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
355f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He    /**
356f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     * Add a camera3 buffer listener. Adding the same listener twice has
357f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     * no effect.
358f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     */
3592fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin    void             addBufferListener(
3602fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin            wp<Camera3StreamBufferListener> listener);
361f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He
362f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He    /**
363f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     * Remove a camera3 buffer listener. Removing the same listener twice
364f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     * or the listener that was never added has no effect.
365f0d962a6737eb8eec002d6804d9ffbe7bee672a0Zhijun He     */
3662fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin    void             removeBufferListener(
3672fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin            const sp<Camera3StreamBufferListener>& listener);
3682fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin
369be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh
370be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh    // Setting listener will remove previous listener (if exists)
371be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh    virtual void     setBufferFreedListener(
372be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh            Camera3StreamBufferFreedListener* listener) override;
373be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh
374e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen    /**
375e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen     * Return if the buffer queue of the stream is abandoned.
376e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen     */
377e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen    bool             isAbandoned() const;
378e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen
379fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala  protected:
380fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    const int mId;
381125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He    /**
382125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * Stream set id, used to indicate which group of this stream belongs to for buffer sharing
383125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * across multiple streams.
384125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     *
385125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * The default value is set to CAMERA3_STREAM_SET_ID_INVALID, which indicates that this stream
386125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * doesn't intend to share buffers with any other streams, and this stream will fall back to
387125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * the existing BufferQueue mechanism to manage the buffer allocations and buffer circulation.
388125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * When a valid stream set id is set, this stream intends to use the Camera3BufferManager to
389125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * manage the buffer allocations; the BufferQueue will only handle the buffer transaction
390125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * between the producer and consumer. For this case, upon successfully registration, the streams
391125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * with the same stream set id will potentially share the buffers allocated by
392125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     * Camera3BufferManager.
393125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He     */
394125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He    const int mSetId;
395125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He
396fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    const String8 mName;
397fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // Zero for formats with fixed buffer size for given dimensions.
398fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    const size_t mMaxSize;
399fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
400fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    enum {
401fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        STATE_ERROR,
402fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        STATE_CONSTRUCTED,
403fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        STATE_IN_CONFIG,
404fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala        STATE_IN_RECONFIG,
4054d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala        STATE_CONFIGURED,
406e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen        STATE_PREPARING,
407e8c535e833ed135895e99ca81aa3b85d80d7cf3cChien-Yu Chen        STATE_ABANDONED
408fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    } mState;
409fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
410fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    mutable Mutex mLock;
411fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
412fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    Camera3Stream(int id, camera3_stream_type type,
4133d82c0d9ed2b3e956ad699a9ca2c8a70c9d24678Eino-Ville Talvala            uint32_t width, uint32_t height, size_t maxSize, int format,
414125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He            android_dataspace dataSpace, camera3_stream_rotation_t rotation,
415125684aba1a11b7adbf5f9d607ee2bcc9449081cZhijun He            int setId);
416fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
417be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh    Camera3StreamBufferFreedListener* mBufferFreedListener;
418be83fa713da45b1c751d33ad69ce0017ebe9f707Yin-Chia Yeh
419fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    /**
420fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     * Interface to be implemented by derived classes
421fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala     */
422fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
423fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // getBuffer / returnBuffer implementations
424fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
425fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // Since camera3_stream_buffer includes a raw pointer to the stream,
426fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // cast to camera3_stream*, implementations must increment the
427fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // refcount of the stream manually in getBufferLocked, and decrement it in
428fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // returnBufferLocked.
429bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang    virtual status_t getBufferLocked(camera3_stream_buffer *buffer,
430bee0f0a5e7aa19aa7c59de03b508985b68bc6b5eShuzhen Wang            const std::vector<size_t>& surface_ids = std::vector<size_t>());
431fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual status_t returnBufferLocked(const camera3_stream_buffer &buffer,
4325a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin            nsecs_t timestamp);
4335a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    virtual status_t getInputBufferLocked(camera3_stream_buffer *buffer);
4345a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin    virtual status_t returnInputBufferLocked(
4355a269fa72b419e7fe4bf6bf9b27eec8782b3a963Igor Murashkin            const camera3_stream_buffer &buffer);
436fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual bool     hasOutstandingBuffersLocked() const = 0;
437618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    // Get the buffer producer of the input buffer queue. Only apply to input streams.
438618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen    virtual status_t getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer);
439618ff8a48a0c895a78f91f5692510c2a809425c3Chien-Yu Chen
440e2172bed7e77ab1d922588cf727818b481400b53Igor Murashkin    // Can return -ENOTCONN when we are already disconnected (not an error)
441fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual status_t disconnectLocked() = 0;
442fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
443fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // Configure the buffer queue interface to the other end of the stream,
444fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // after the HAL has provided usage and max_buffers values. After this call,
445fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // the stream must be ready to produce all buffers for registration with
446fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // HAL.
447fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual status_t configureQueueLocked() = 0;
448fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
449fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    // Get the total number of buffers in the queue
450fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala    virtual size_t   getBufferCountLocked() = 0;
451fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
4526adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    // Get handout output buffer count.
4536adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    virtual size_t   getHandoutOutputBufferCountLocked() = 0;
4546adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He
4556adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    // Get handout input buffer count.
4566adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    virtual size_t   getHandoutInputBufferCountLocked() = 0;
4576adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He
458b2f5b19e5b6e1408a259add23dba91037756a943Eino-Ville Talvala    // Get the usage flags for the other endpoint, or return
459b2f5b19e5b6e1408a259add23dba91037756a943Eino-Ville Talvala    // INVALID_OPERATION if they cannot be obtained.
4604d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    virtual status_t getEndpointUsage(uint32_t *usage) const = 0;
461b2f5b19e5b6e1408a259add23dba91037756a943Eino-Ville Talvala
462f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    // Tracking for idle state
463f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    wp<StatusTracker> mStatusTracker;
464f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    // Status tracker component ID
465f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala    int mStatusId;
466f1e98d857ec377f2c9b916073d40732e6ebb7cedEino-Ville Talvala
4674d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // Tracking for stream prepare - whether this stream can still have
4684d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // prepareNextBuffer called on it.
4694d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    bool mStreamUnpreparable;
4704d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
471fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala  private:
47202bf03287652923b5bb5316667b065423565d6b4Eino-Ville Talvala    uint32_t mOldUsage;
47302bf03287652923b5bb5316667b065423565d6b4Eino-Ville Talvala    uint32_t mOldMaxBuffers;
4746adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    Condition mOutputBufferReturnedSignal;
4756adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    Condition mInputBufferReturnedSignal;
4766adc9ccb2948d9421a0ed4b74f52b909bcec2037Zhijun He    static const nsecs_t kWaitForBufferDuration = 3000000000LL; // 3000 ms
477fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
4782fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin    void fireBufferListenersLocked(const camera3_stream_buffer& buffer,
4792fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin                                  bool acquired, bool output);
4802fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin    List<wp<Camera3StreamBufferListener> > mBufferListenerList;
4812fba584544e8687b526e3388bf7160b696da1dbaIgor Murashkin
4824d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    status_t        cancelPrepareLocked();
4834d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
484e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    // Return whether the buffer is in the list of outstanding buffers.
485e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    bool isOutstandingBuffer(const camera3_stream_buffer& buffer);
486e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen
487e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    // Remove the buffer from the list of outstanding buffers.
488e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    void removeOutstandingBuffer(const camera3_stream_buffer& buffer);
489e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen
4904d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // Tracking for PREPARING state
4914d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
4924d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // State of buffer preallocation. Only true if either prepareNextBuffer
4934d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // has been called sufficient number of times, or stream configuration
4944d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    // had to register buffers with the HAL
4954d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    bool mPrepared;
4964d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
4974d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    Vector<camera3_stream_buffer_t> mPreparedBuffers;
4984d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala    size_t mPreparedBufferIdx;
4994d44cad22ea925a651463f2d51d6586c14d4b787Eino-Ville Talvala
500c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk    // Number of buffers allocated on last prepare call.
50102bf03287652923b5bb5316667b065423565d6b4Eino-Ville Talvala    size_t mLastMaxCount;
502c78ac26e3a65328fc0118f16ee76a800d0687eb7Ruben Brunk
503e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    // Outstanding buffers dequeued from the stream's buffer queue.
504e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen    List<buffer_handle_t> mOutstandingBuffers;
505e02e932d2210bd513bb8283840bbe24b58d28620Chien-Yu Chen
5066c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang    // Latency histogram of the wait time for handout buffer count to drop below
5076c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang    // max_buffers.
5086c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang    static const int32_t kBufferLimitLatencyBinSize = 33; //in ms
5096c2f7aeb10fb2050c27afa0212a1e167ca2809ecShuzhen Wang    CameraLatencyHistogram mBufferLimitLatency;
510fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala}; // class Camera3Stream
511fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
512fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala}; // namespace camera3
513fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
514fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala}; // namespace android
515fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala
516fd58f1a10a749ca72fec1012920d6e94a664cd70Eino-Ville Talvala#endif
517