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