blob: d3419475367e7da546eab205ecdf153438ef336f [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
Phil Burk919cd422019-08-12 17:26:50 -070017#include <algorithm>
Phil Burk9446ed62020-07-13 11:26:55 -070018#include <cassert>
19#include <stdint.h>
Don Turnerca6f91a2017-09-05 14:52:39 +010020
Phil Burk9446ed62020-07-13 11:26:55 -070021#include "FifoControllerBase.h"
Don Turnerca6f91a2017-09-05 14:52:39 +010022
Don Turner3bf32ae2017-11-27 13:25:05 +000023namespace oboe {
24
Phil Burk919cd422019-08-12 17:26:50 -070025FifoControllerBase::FifoControllerBase(uint32_t capacityInFrames)
26 : mTotalFrames(capacityInFrames)
Don Turnerca6f91a2017-09-05 14:52:39 +010027{
Phil Burk919cd422019-08-12 17:26:50 -070028 // Avoid ridiculously large buffers and the arithmetic wraparound issues that can follow.
29 assert(capacityInFrames <= (UINT32_MAX / 4));
Don Turnerca6f91a2017-09-05 14:52:39 +010030}
31
Phil Burk919cd422019-08-12 17:26:50 -070032uint32_t FifoControllerBase::getFullFramesAvailable() const {
33 uint64_t writeCounter = getWriteCounter();
34 uint64_t readCounter = getReadCounter();
35 if (readCounter > writeCounter) {
36 return 0;
37 }
38 uint64_t delta = writeCounter - readCounter;
39 if (delta >= mTotalFrames) {
40 return mTotalFrames;
41 }
42 // delta is now guaranteed to fit within the range of a uint32_t
43 return static_cast<uint32_t>(delta);
Don Turnerca6f91a2017-09-05 14:52:39 +010044}
45
Thomas Guilbertf2636012019-07-10 12:16:08 -070046uint32_t FifoControllerBase::getReadIndex() const {
Phil Burk919cd422019-08-12 17:26:50 -070047 // % works with non-power of two sizes
gfane55f9832018-07-25 16:13:34 -070048 return static_cast<uint32_t>(getReadCounter() % mTotalFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010049}
50
Thomas Guilbert5a73fb92019-07-12 11:54:45 -070051void FifoControllerBase::advanceReadIndex(uint32_t numFrames) {
52 incrementReadCounter(numFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010053}
54
Phil Burk919cd422019-08-12 17:26:50 -070055uint32_t FifoControllerBase::getEmptyFramesAvailable() const {
56 return static_cast<uint32_t>(mTotalFrames - getFullFramesAvailable());
Don Turnerca6f91a2017-09-05 14:52:39 +010057}
58
Thomas Guilbertf2636012019-07-10 12:16:08 -070059uint32_t FifoControllerBase::getWriteIndex() const {
Phil Burk919cd422019-08-12 17:26:50 -070060 // % works with non-power of two sizes
61 return static_cast<uint32_t>(getWriteCounter() % mTotalFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010062}
63
64void FifoControllerBase::advanceWriteIndex(uint32_t numFrames) {
Thomas Guilbert5a73fb92019-07-12 11:54:45 -070065 incrementWriteCounter(numFrames);
Don Turnerca6f91a2017-09-05 14:52:39 +010066}
67
Phil Burkf61cea32018-12-17 17:30:21 -080068} // namespace oboe