| 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 |  | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 17 | #include <algorithm> | 
| Phil Burk | 9446ed6 | 2020-07-13 11:26:55 -0700 | [diff] [blame] | 18 | #include <cassert> | 
 | 19 | #include <stdint.h> | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 20 |  | 
| Phil Burk | 9446ed6 | 2020-07-13 11:26:55 -0700 | [diff] [blame] | 21 | #include "FifoControllerBase.h" | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 22 |  | 
| Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 23 | namespace oboe { | 
 | 24 |  | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 25 | FifoControllerBase::FifoControllerBase(uint32_t capacityInFrames) | 
 | 26 |         : mTotalFrames(capacityInFrames) | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 27 | { | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 28 |     // Avoid ridiculously large buffers and the arithmetic wraparound issues that can follow. | 
 | 29 |     assert(capacityInFrames <= (UINT32_MAX / 4)); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 30 | } | 
 | 31 |  | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 32 | uint32_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 Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 44 | } | 
 | 45 |  | 
| Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 46 | uint32_t FifoControllerBase::getReadIndex() const { | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 47 |     // % works with non-power of two sizes | 
| gfan | e55f983 | 2018-07-25 16:13:34 -0700 | [diff] [blame] | 48 |     return static_cast<uint32_t>(getReadCounter() % mTotalFrames); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 49 | } | 
 | 50 |  | 
| Thomas Guilbert | 5a73fb9 | 2019-07-12 11:54:45 -0700 | [diff] [blame] | 51 | void FifoControllerBase::advanceReadIndex(uint32_t numFrames) { | 
 | 52 |     incrementReadCounter(numFrames); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 53 | } | 
 | 54 |  | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 55 | uint32_t FifoControllerBase::getEmptyFramesAvailable() const { | 
 | 56 |     return static_cast<uint32_t>(mTotalFrames - getFullFramesAvailable()); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 57 | } | 
 | 58 |  | 
| Thomas Guilbert | f263601 | 2019-07-10 12:16:08 -0700 | [diff] [blame] | 59 | uint32_t FifoControllerBase::getWriteIndex() const { | 
| Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 60 |     // % works with non-power of two sizes | 
 | 61 |     return static_cast<uint32_t>(getWriteCounter() % mTotalFrames); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 62 | } | 
 | 63 |  | 
 | 64 | void FifoControllerBase::advanceWriteIndex(uint32_t numFrames) { | 
| Thomas Guilbert | 5a73fb9 | 2019-07-12 11:54:45 -0700 | [diff] [blame] | 65 |     incrementWriteCounter(numFrames); | 
| Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 66 | } | 
 | 67 |  | 
| Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 68 | } // namespace oboe |