1/*
2 * Copyright (C) 2013-2018 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_IO_STREAM_BASE_H
18#define ANDROID_SERVERS_CAMERA3_IO_STREAM_BASE_H
19
20#include <utils/RefBase.h>
21#include <gui/Surface.h>
22
23#include "Camera3Stream.h"
24
25namespace android {
26
27namespace camera3 {
28
29/**
30 * A base class for managing a single stream of I/O data from the camera device.
31 */
32class Camera3IOStreamBase :
33        public Camera3Stream {
34  protected:
35    Camera3IOStreamBase(int id, camera3_stream_type_t type,
36            uint32_t width, uint32_t height, size_t maxSize, int format,
37            android_dataspace dataSpace, camera3_stream_rotation_t rotation,
38            const String8& physicalCameraId,
39            int setId = CAMERA3_STREAM_SET_ID_INVALID);
40
41  public:
42
43    virtual ~Camera3IOStreamBase();
44
45    /**
46     * Camera3Stream interface
47     */
48
49    virtual void     dump(int fd, const Vector<String16> &args) const;
50
51  protected:
52    size_t            mTotalBufferCount;
53    // sum of input and output buffers that are currently acquired by HAL
54    size_t            mHandoutTotalBufferCount;
55    // number of output buffers that are currently acquired by HAL. This will be
56    // Redundant when camera3 streams are no longer bidirectional streams.
57    size_t            mHandoutOutputBufferCount;
58    Condition         mBufferReturnedSignal;
59    uint32_t          mFrameCount;
60    // Last received output buffer's timestamp
61    nsecs_t           mLastTimestamp;
62
63    // The merged release fence for all returned buffers
64    sp<Fence>         mCombinedFence;
65
66    status_t         returnAnyBufferLocked(
67            const camera3_stream_buffer &buffer,
68            nsecs_t timestamp,
69            bool output);
70
71    virtual status_t returnBufferCheckedLocked(
72            const camera3_stream_buffer &buffer,
73            nsecs_t timestamp,
74            bool output,
75            /*out*/
76            sp<Fence> *releaseFenceOut) = 0;
77
78    /**
79     * Internal Camera3Stream interface
80     */
81    virtual bool     hasOutstandingBuffersLocked() const;
82
83    virtual size_t   getBufferCountLocked();
84
85    virtual size_t   getHandoutOutputBufferCountLocked();
86
87    virtual size_t   getHandoutInputBufferCountLocked();
88
89    virtual status_t getEndpointUsage(uint64_t *usage) const = 0;
90
91    status_t getBufferPreconditionCheckLocked() const;
92    status_t returnBufferPreconditionCheckLocked() const;
93
94    // State check only
95    virtual status_t configureQueueLocked();
96    // State checks only
97    virtual status_t disconnectLocked();
98
99    // Hand out the buffer to a native location,
100    //   incrementing the internal refcount and dequeued buffer count
101    void handoutBufferLocked(camera3_stream_buffer &buffer,
102                             buffer_handle_t *handle,
103                             int acquire_fence,
104                             int release_fence,
105                             camera3_buffer_status_t status,
106                             bool output);
107
108}; // class Camera3IOStreamBase
109
110} // namespace camera3
111
112} // namespace android
113
114#endif
115