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 | |
Don Turner | 2786fb5 | 2017-11-27 19:08:25 +0000 | [diff] [blame] | 17 | #ifndef OBOE_FIFOPROCESSOR_H |
| 18 | #define OBOE_FIFOPROCESSOR_H |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 19 | |
Phil Burk | 9446ed6 | 2020-07-13 11:26:55 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <stdint.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 | #include "oboe/Definitions.h" |
| 24 | |
Phil Burk | 9446ed6 | 2020-07-13 11:26:55 -0700 | [diff] [blame] | 25 | #include "FifoControllerBase.h" |
| 26 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 27 | namespace oboe { |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 28 | |
| 29 | class FifoBuffer { |
| 30 | public: |
| 31 | FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames); |
| 32 | |
| 33 | FifoBuffer(uint32_t bytesPerFrame, |
| 34 | uint32_t capacityInFrames, |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 35 | std::atomic<uint64_t> *readCounterAddress, |
| 36 | std::atomic<uint64_t> *writeCounterAddress, |
Phil Burk | f61cea3 | 2018-12-17 17:30:21 -0800 | [diff] [blame] | 37 | uint8_t *dataStorageAddress); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 38 | |
| 39 | ~FifoBuffer(); |
| 40 | |
| 41 | int32_t convertFramesToBytes(int32_t frames); |
| 42 | |
Phil Burk | b3e90b5 | 2018-10-03 08:29:24 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Read framesToRead or, if not enough, then read as many as are available. |
| 45 | * @param destination |
| 46 | * @param framesToRead number of frames requested |
| 47 | * @return number of frames actually read |
| 48 | */ |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 49 | int32_t read(void *destination, int32_t framesToRead); |
| 50 | |
| 51 | int32_t write(const void *source, int32_t framesToWrite); |
| 52 | |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 53 | uint32_t getBufferCapacityInFrames() const; |
| 54 | |
Phil Burk | b3e90b5 | 2018-10-03 08:29:24 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Calls read(). If all of the frames cannot be read then the remainder of the buffer |
Phil Burk | 919cd42 | 2019-08-12 17:26:50 -0700 | [diff] [blame] | 57 | * is set to zero. |
Phil Burk | b3e90b5 | 2018-10-03 08:29:24 -0700 | [diff] [blame] | 58 | * |
| 59 | * @param destination |
| 60 | * @param framesToRead number of frames requested |
| 61 | * @return number of frames actually read |
| 62 | */ |
Phil Burk | 2b9390d | 2018-10-03 09:06:58 -0700 | [diff] [blame] | 63 | int32_t readNow(void *destination, int32_t numFrames); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 64 | |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 65 | uint32_t getFullFramesAvailable() { |
| 66 | return mFifo->getFullFramesAvailable(); |
| 67 | } |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 68 | |
| 69 | uint32_t getBytesPerFrame() const { |
| 70 | return mBytesPerFrame; |
| 71 | } |
| 72 | |
| 73 | uint64_t getReadCounter() const { |
| 74 | return mFifo->getReadCounter(); |
| 75 | } |
| 76 | |
| 77 | void setReadCounter(uint64_t n) { |
| 78 | mFifo->setReadCounter(n); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uint64_t getWriteCounter() { |
| 82 | return mFifo->getWriteCounter(); |
| 83 | } |
| 84 | void setWriteCounter(uint64_t n) { |
| 85 | mFifo->setWriteCounter(n); |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 89 | uint32_t mBytesPerFrame; |
| 90 | uint8_t* mStorage; |
| 91 | bool mStorageOwned; // did this object allocate the storage? |
Phil Burk | 08a5e0f | 2019-08-23 16:28:21 -0700 | [diff] [blame] | 92 | std::unique_ptr<FifoControllerBase> mFifo; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 93 | uint64_t mFramesReadCount; |
| 94 | uint64_t mFramesUnderrunCount; |
Don Turner | ca6f91a | 2017-09-05 14:52:39 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 97 | } // namespace oboe |
| 98 | |
Don Turner | 2786fb5 | 2017-11-27 19:08:25 +0000 | [diff] [blame] | 99 | #endif //OBOE_FIFOPROCESSOR_H |