blob: a4c2b41a5e5ea9adef4e44ae8264a5db9f0cd638 [file] [log] [blame]
Don Turnerca6f91a2017-09-05 14:52:39 +01001/*
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 Turner6a390fd2018-07-27 12:35:02 +010020#include <stdint.h>
Don Turnerca6f91a2017-09-05 14:52:39 +010021
Don Turner3bf32ae2017-11-27 13:25:05 +000022namespace oboe {
23
Don Turnerca6f91a2017-09-05 14:52:39 +010024/**
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
34class FifoControllerBase {
35
36public:
37 /**
Phil Burk919cd422019-08-12 17:26:50 -070038 * @param totalFrames capacity of the circular buffer in frames.
Don Turnerca6f91a2017-09-05 14:52:39 +010039 */
Phil Burk919cd422019-08-12 17:26:50 -070040 FifoControllerBase(uint32_t totalFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010041
Thomas Guilbertf2636012019-07-10 12:16:08 -070042 virtual ~FifoControllerBase() = default;
Don Turnerca6f91a2017-09-05 14:52:39 +010043
44 /**
Phil Burk919cd422019-08-12 17:26:50 -070045 * 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 Turnerca6f91a2017-09-05 14:52:39 +010049 */
Phil Burk919cd422019-08-12 17:26:50 -070050 uint32_t getFullFramesAvailable() const;
Don Turnerca6f91a2017-09-05 14:52:39 +010051
52 /**
53 * The index in a circular buffer of the next frame to read.
54 */
Thomas Guilbertf2636012019-07-10 12:16:08 -070055 uint32_t getReadIndex() const;
Don Turnerca6f91a2017-09-05 14:52:39 +010056
57 /**
58 * @param numFrames number of frames to advance the read index
59 */
Thomas Guilbert5a73fb92019-07-12 11:54:45 -070060 void advanceReadIndex(uint32_t numFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010061
62 /**
Phil Burk919cd422019-08-12 17:26:50 -070063 * @return maximum number of frames that can be written without exceeding the threshold.
Don Turnerca6f91a2017-09-05 14:52:39 +010064 */
Phil Burk919cd422019-08-12 17:26:50 -070065 uint32_t getEmptyFramesAvailable() const;
Don Turnerca6f91a2017-09-05 14:52:39 +010066
67 /**
68 * The index in a circular buffer of the next frame to write.
69 */
Thomas Guilbertf2636012019-07-10 12:16:08 -070070 uint32_t getWriteIndex() const;
Don Turnerca6f91a2017-09-05 14:52:39 +010071
72 /**
73 * @param numFrames number of frames to advance the write index
74 */
75 void advanceWriteIndex(uint32_t numFrames);
76
Don Turnerca6f91a2017-09-05 14:52:39 +010077 uint32_t getFrameCapacity() const { return mTotalFrames; }
78
Thomas Guilbertf2636012019-07-10 12:16:08 -070079 virtual uint64_t getReadCounter() const = 0;
Don Turnerca6f91a2017-09-05 14:52:39 +010080 virtual void setReadCounter(uint64_t n) = 0;
Thomas Guilbert5a73fb92019-07-12 11:54:45 -070081 virtual void incrementReadCounter(uint64_t n) = 0;
Thomas Guilbertf2636012019-07-10 12:16:08 -070082 virtual uint64_t getWriteCounter() const = 0;
Don Turnerca6f91a2017-09-05 14:52:39 +010083 virtual void setWriteCounter(uint64_t n) = 0;
Thomas Guilbert5a73fb92019-07-12 11:54:45 -070084 virtual void incrementWriteCounter(uint64_t n) = 0;
Don Turnerca6f91a2017-09-05 14:52:39 +010085
86private:
87 uint32_t mTotalFrames;
Don Turnerca6f91a2017-09-05 14:52:39 +010088};
89
Don Turner3bf32ae2017-11-27 13:25:05 +000090} // namespace oboe
Don Turnerca6f91a2017-09-05 14:52:39 +010091
92#endif //NATIVEOBOE_FIFOCONTROLLERBASE_H