1 /*
2  * Copyright 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 INPUT_SURFACE_WRAPPER_H_
18 #define INPUT_SURFACE_WRAPPER_H_
19 
20 #include <codec2/hidl/client.h>
21 #include <system/graphics.h>
22 
23 namespace android {
24 
25 /**
26  * Wrapper interface around InputSurface.
27  */
28 class InputSurfaceWrapper {
29 public:
InputSurfaceWrapper()30     InputSurfaceWrapper()
31         : mDataSpace(HAL_DATASPACE_UNKNOWN),
32           mPixelFormat(PIXEL_FORMAT_UNKNOWN) {
33     }
34 
35     virtual ~InputSurfaceWrapper() = default;
36 
37     /**
38      * Connect the surface with |comp|. A surface can
39      * connect to at most one component at a time.
40      *
41      * \return OK               successfully connected to |comp|
42      * \return ALREADY_EXISTS   already connected to another component.
43      */
44     virtual status_t connect(
45             const std::shared_ptr<Codec2Client::Component> &comp) = 0;
46 
47     /**
48      * Disconnect the surface from the component if any.
49      */
50     virtual void disconnect() = 0;
51 
52     /**
53      * Start pushing buffers to the surface.
54      */
55     virtual status_t start() = 0;
56 
57     /**
58      * Ref: GraphicBufferSource::signalEndOfInputStream.
59      */
60     virtual status_t signalEndOfInputStream() = 0;
61 
62     /// Input Surface configuration
63     struct Config {
64         // IN PARAMS (GBS)
65         float mMinFps = 0.0; // minimum fps (repeat frame to achieve this)
66         float mMaxFps = 0.0; // max fps (via frame drop)
67         float mCaptureFps = 0.0; // capture fps
68         float mCodedFps = 0.0;   // coded fps
69         bool mSuspended = false; // suspended
70         int64_t mTimeOffsetUs = 0; // time offset (input => codec)
71         int64_t mSuspendAtUs = 0; // suspend/resume time
72         int64_t mStartAtUs = 0; // start time
73         bool mStopped = false; // stopped
74         int64_t mStopAtUs = 0; // stop time
75 
76         // OUT PARAMS (GBS)
77         int64_t mInputDelayUs = 0; // delay between encoder input and surface input
78 
79         // IN PARAMS (CODEC WRAPPER)
80         float mFixedAdjustedFps = 0.0; // fixed fps via PTS manipulation
81         float mMinAdjustedFps = 0.0; // minimum fps via PTS manipulation
82         uint64_t mUsage = 0; // consumer usage
83         int mPriority = INT_MAX; // priority of queue thread (if any); INT_MAX for no-op
84     };
85 
86     /**
87      * Configures input surface.
88      *
89      * \param config configuration. This can be updated during this call to provide output
90      *               parameters, but not to provide configured parameters (to avoid continually
91      *               reconfiguring)
92      */
93     virtual status_t configure(Config &config) = 0;
94 
95     /**
96      * Configures desired data space.
97      *
98      * \param dataSpace desired data space
99      */
setDataSpace(android_dataspace dataSpace)100     inline void setDataSpace(android_dataspace dataSpace) {
101         mDataSpace = dataSpace;
102     }
103 
104     /**
105      * Notify that the input buffer reference is no longer needed.
106      * Clean up C2Work related references if necessary. No-op by default.
107      *
108      * \param index index of input work.
109      */
onInputBufferDone(c2_cntr64_t)110     virtual void onInputBufferDone(c2_cntr64_t /* index */) {}
111 
112     /**
113      * Signal one input buffer as emptied.
114      * No-op by default.
115      */
onInputBufferEmptied()116     virtual void onInputBufferEmptied() {}
117 
118     /**
119      * Returns dataspace information from GraphicBufferSource.
120      */
getDataspace()121     virtual android_dataspace getDataspace() { return mDataSpace; }
122 
123     /**
124      * Returns pixel format information from GraphicBufferSource.
125      */
getPixelFormat()126     virtual uint32_t getPixelFormat() { return mPixelFormat; }
127 
128 protected:
129     android_dataspace mDataSpace;
130     uint32_t mPixelFormat;
131 };
132 
133 }  // namespace android
134 
135 #endif  // INPUT_SURFACE_WRAPPER_H_
136