Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [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 NATIVEOBOE_FIFOCONTROLLERBASE_H |
| 18 | #define NATIVEOBOE_FIFOCONTROLLERBASE_H |
| 19 | |
Don Turner | 6a390fd | 2018-07-27 12:35:02 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 21 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 22 | namespace oboe { |
| 23 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 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 | /** |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 38 | * @param totalFrames capacity of the circular buffer in frames. |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 39 | */ |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 40 | FifoControllerBase(uint32_t totalFrames); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 41 | |
Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 42 | virtual ~FifoControllerBase() = default; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 43 | |
| 44 | /** |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 45 | * The frames available to read will be calculated from the read and write counters. |
| 46 | * The result will be clipped to the capacity of the buffer. |
| 47 | * If the buffer has underflowed then this will return zero. |
| 48 | * @return number of valid frames available to read. |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 49 | */ |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 50 | uint32_t getFullFramesAvailable() const; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * The index in a circular buffer of the next frame to read. |
| 54 | */ |
Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 55 | uint32_t getReadIndex() const; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @param numFrames number of frames to advance the read index |
| 59 | */ |
Thomas Guilbert | 5a73fb9 | 2019-07-12 11:54:45 -0700 | [diff] [blame] | 60 | void advanceReadIndex(uint32_t numFrames); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 61 | |
| 62 | /** |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 63 | * @return maximum number of frames that can be written without exceeding the threshold. |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 64 | */ |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 65 | uint32_t getEmptyFramesAvailable() const; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * The index in a circular buffer of the next frame to write. |
| 69 | */ |
Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 70 | uint32_t getWriteIndex() const; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @param numFrames number of frames to advance the write index |
| 74 | */ |
| 75 | void advanceWriteIndex(uint32_t numFrames); |
| 76 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 77 | uint32_t getFrameCapacity() const { return mTotalFrames; } |
| 78 | |
Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 79 | virtual uint64_t getReadCounter() const = 0; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 80 | virtual void setReadCounter(uint64_t n) = 0; |
Thomas Guilbert | 5a73fb9 | 2019-07-12 11:54:45 -0700 | [diff] [blame] | 81 | virtual void incrementReadCounter(uint64_t n) = 0; |
Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 82 | virtual uint64_t getWriteCounter() const = 0; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 83 | virtual void setWriteCounter(uint64_t n) = 0; |
Thomas Guilbert | 5a73fb9 | 2019-07-12 11:54:45 -0700 | [diff] [blame] | 84 | virtual void incrementWriteCounter(uint64_t n) = 0; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | uint32_t mTotalFrames; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 90 | } // namespace oboe |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 91 | |
| 92 | #endif //NATIVEOBOE_FIFOCONTROLLERBASE_H |