xref: /aosp_15_r20/external/oboe/include/oboe/FifoControllerBase.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2015 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 NATIVEOBOE_FIFOCONTROLLERBASE_H
18 #define NATIVEOBOE_FIFOCONTROLLERBASE_H
19 
20 #include <stdint.h>
21 
22 namespace oboe {
23 
24 /**
25  * Manage the read/write indices of a circular buffer.
26  *
27  * The caller is responsible for reading and writing the actual data.
28  * Note that the span of available frames may not be contiguous. They
29  * may wrap around from the end to the beginning of the buffer. In that
30  * case the data must be read or written in at least two blocks of frames.
31  *
32  */
33 
34 class FifoControllerBase {
35 
36 public:
37    /**
38 	 * Construct a `FifoControllerBase`.
39 	 *
40 	 * @param totalFrames capacity of the circular buffer in frames
41 	 */
42     FifoControllerBase(uint32_t totalFrames);
43 
44     virtual ~FifoControllerBase() = default;
45 
46     /**
47      * The frames available to read will be calculated from the read and write counters.
48      * The result will be clipped to the capacity of the buffer.
49      * If the buffer has underflowed then this will return zero.
50      *
51      * @return number of valid frames available to read.
52      */
53     uint32_t getFullFramesAvailable() const;
54 
55 	/**
56      * The index in a circular buffer of the next frame to read.
57      *
58      * @return read index position
59      */
60     uint32_t getReadIndex() const;
61 
62    /**
63 	* Advance read index from a number of frames.
64 	* Equivalent of incrementReadCounter(numFrames).
65 	*
66 	* @param numFrames number of frames to advance the read index
67 	*/
68     void advanceReadIndex(uint32_t numFrames);
69 
70 	/**
71 	 * Get the number of frame that are not written yet.
72 	 *
73 	 * @return maximum number of frames that can be written without exceeding the threshold
74 	 */
75     uint32_t getEmptyFramesAvailable() const;
76 
77     /**
78 	 * The index in a circular buffer of the next frame to write.
79 	 *
80 	 * @return index of the next frame to write
81 	 */
82     uint32_t getWriteIndex() const;
83 
84 	/**
85      * Advance write index from a number of frames.
86      * Equivalent of incrementWriteCounter(numFrames).
87      *
88      * @param numFrames number of frames to advance the write index
89      */
90     void advanceWriteIndex(uint32_t numFrames);
91 
92 	/**
93 	 * Get the frame capacity of the fifo.
94 	 *
95 	 * @return frame capacity
96 	 */
getFrameCapacity()97     uint32_t getFrameCapacity() const { return mTotalFrames; }
98 
99     virtual uint64_t getReadCounter() const = 0;
100     virtual void setReadCounter(uint64_t n) = 0;
101     virtual void incrementReadCounter(uint64_t n) = 0;
102     virtual uint64_t getWriteCounter() const = 0;
103     virtual void setWriteCounter(uint64_t n) = 0;
104     virtual void incrementWriteCounter(uint64_t n) = 0;
105 
106 private:
107     uint32_t mTotalFrames;
108 };
109 
110 } // namespace oboe
111 
112 #endif //NATIVEOBOE_FIFOCONTROLLERBASE_H
113