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 FIFO_FIFO_CONTROLLER_BASE_H
18 #define FIFO_FIFO_CONTROLLER_BASE_H
19 
20 #include <stdint.h>
21 
22 namespace android {
23 
24 typedef int64_t fifo_counter_t;
25 typedef int32_t fifo_frames_t;
26 
27 /**
28  * Manage the read/write indices of a circular buffer.
29  *
30  * The caller is responsible for reading and writing the actual data.
31  * Note that the span of available frames may not be contiguous. They
32  * may wrap around from the end to the beginning of the buffer. In that
33  * case the data must be read or written in at least two blocks of frames.
34  *
35  */
36 class FifoControllerBase {
37 
38 public:
39     /**
40      * Constructor for FifoControllerBase
41      * @param capacity Total size of the circular buffer in frames.
42      * @param threshold Number of frames to fill. Must be less than capacity.
43      */
44     FifoControllerBase(fifo_frames_t capacity, fifo_frames_t threshold);
45 
46     virtual ~FifoControllerBase();
47 
48     // Abstract methods to be implemented in subclasses.
49     /**
50      * @return Counter used by the reader of the FIFO.
51      */
52     virtual fifo_counter_t getReadCounter() = 0;
53 
54     /**
55      * This is normally only used internally.
56      * @param count Number of frames that have been read.
57      */
58     virtual void setReadCounter(fifo_counter_t count) = 0;
59 
60     /**
61      * @return Counter used by the reader of the FIFO.
62      */
63     virtual fifo_counter_t getWriteCounter() = 0;
64 
65     /**
66      * This is normally only used internally.
67      * @param count Number of frames that have been read.
68      */
69     virtual void setWriteCounter(fifo_counter_t count) = 0;
70 
71     /**
72      * This may be negative if an unthrottled reader has read beyond the available data.
73      * @return number of valid frames available to read. Never read more than this.
74      */
75     fifo_frames_t getFullFramesAvailable();
76 
77     /**
78      * The index in a circular buffer of the next frame to read.
79      */
80     fifo_frames_t getReadIndex();
81 
82     /**
83      * @param numFrames number of frames to advance the read index
84      */
85     void advanceReadIndex(fifo_frames_t numFrames);
86 
87     /**
88      * @return number of frames that can be written. Never write more than this.
89      */
90     fifo_frames_t getEmptyFramesAvailable();
91 
92     /**
93      * The index in a circular buffer of the next frame to write.
94      */
95     fifo_frames_t getWriteIndex();
96 
97     /**
98      * @param numFrames number of frames to advance the write index
99      */
100     void advanceWriteIndex(fifo_frames_t numFrames);
101 
102     /**
103      * You can request that the buffer not be filled above a maximum
104      * number of frames.
105      *
106      * The threshold will be clipped between zero and the buffer capacity.
107      *
108      * @param threshold effective size of the buffer
109      */
110     void setThreshold(fifo_frames_t threshold);
111 
getThreshold()112     fifo_frames_t getThreshold() const {
113         return mThreshold;
114     }
115 
getCapacity()116     fifo_frames_t getCapacity() const {
117         return mCapacity;
118     }
119 
120 
121 private:
122     fifo_frames_t mCapacity;
123     fifo_frames_t mThreshold;
124 };
125 
126 }  // android
127 
128 #endif // FIFO_FIFO_CONTROLLER_BASE_H
129