blob: 860ccaded17ff3e412cff3d9b82d4e6705785cbf [file] [log] [blame]
Phil Burkfd911c12017-01-03 17:15:39 -08001/*
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 FIFO_FIFO_BUFFER_H
18#define FIFO_FIFO_BUFFER_H
19
Phil Burk882c5202018-04-23 10:32:45 -070020#include <memory>
Phil Burkfd911c12017-01-03 17:15:39 -080021#include <stdint.h>
22
23#include "FifoControllerBase.h"
24
Phil Burk7f6b40d2017-02-09 13:18:38 -080025namespace android {
26
27/**
28 * Structure that represents a region in a circular buffer that might be at the
29 * end of the array and split in two.
30 */
31struct WrappingBuffer {
32 enum {
33 SIZE = 2
34 };
35 void *data[SIZE];
36 int32_t numFrames[SIZE];
37};
38
Phil Burkfd911c12017-01-03 17:15:39 -080039class FifoBuffer {
40public:
jiabind5bd06a2021-04-27 22:04:08 +000041 explicit FifoBuffer(int32_t bytesPerFrame);
Phil Burkfd911c12017-01-03 17:15:39 -080042
Phil Burk8f4fe502020-07-15 23:54:50 +000043 virtual ~FifoBuffer() = default;
Phil Burkfd911c12017-01-03 17:15:39 -080044
45 int32_t convertFramesToBytes(fifo_frames_t frames);
46
47 fifo_frames_t read(void *destination, fifo_frames_t framesToRead);
48
49 fifo_frames_t write(const void *source, fifo_frames_t framesToWrite);
50
51 fifo_frames_t getThreshold();
Phil Burk7f6b40d2017-02-09 13:18:38 -080052
Phil Burkfd911c12017-01-03 17:15:39 -080053 void setThreshold(fifo_frames_t threshold);
54
55 fifo_frames_t getBufferCapacityInFrames();
56
Phil Burk7f6b40d2017-02-09 13:18:38 -080057 /**
58 * Return pointer to available full frames in data1 and set size in numFrames1.
59 * if the data is split across the end of the FIFO then set data2 and numFrames2.
60 * Other wise set them to null
61 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070062 * @return total full frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080063 */
Phil Burkfd34a932017-07-19 07:03:52 -070064 fifo_frames_t getFullDataAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080065
66 /**
67 * Return pointer to available empty frames in data1 and set size in numFrames1.
68 * if the room is split across the end of the FIFO then set data2 and numFrames2.
69 * Other wise set them to null
70 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070071 * @return total empty frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080072 */
Phil Burkfd34a932017-07-19 07:03:52 -070073 fifo_frames_t getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080074
Phil Burkfd911c12017-01-03 17:15:39 -080075 int32_t getBytesPerFrame() {
76 return mBytesPerFrame;
77 }
78
Phil Burk882c5202018-04-23 10:32:45 -070079 // Proxy methods for the internal FifoController
80
Phil Burkfd911c12017-01-03 17:15:39 -080081 fifo_counter_t getReadCounter() {
82 return mFifo->getReadCounter();
83 }
84
85 void setReadCounter(fifo_counter_t n) {
86 mFifo->setReadCounter(n);
87 }
88
89 fifo_counter_t getWriteCounter() {
90 return mFifo->getWriteCounter();
91 }
92
93 void setWriteCounter(fifo_counter_t n) {
94 mFifo->setWriteCounter(n);
95 }
96
Phil Burk882c5202018-04-23 10:32:45 -070097 void advanceReadIndex(fifo_frames_t numFrames) {
98 mFifo->advanceReadIndex(numFrames);
99 }
100
101 void advanceWriteIndex(fifo_frames_t numFrames) {
102 mFifo->advanceWriteIndex(numFrames);
103 }
104
105 fifo_frames_t getEmptyFramesAvailable() {
106 return mFifo->getEmptyFramesAvailable();
107 }
108
109 fifo_frames_t getFullFramesAvailable() {
110 return mFifo->getFullFramesAvailable();
111 }
112
Phil Burkea04d972017-08-07 12:30:44 -0700113 /*
114 * This is generally only called before or after the buffer is used.
115 */
116 void eraseMemory();
117
Phil Burk3a85be62024-01-11 00:41:36 +0000118 /**
119 * Clear some memory after the write pointer.
120 * This can be used to prevent the reader from accidentally reading stale data
121 * in case it is reading asynchronously.
122 */
123 fifo_frames_t eraseEmptyMemory(fifo_frames_t numFrames);
124
Phil Burk8f4fe502020-07-15 23:54:50 +0000125protected:
126
127 virtual uint8_t *getStorage() const = 0;
Phil Burk7f6b40d2017-02-09 13:18:38 -0800128
129 void fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
130 int32_t framesAvailable, int32_t startIndex);
131
Phil Burk882c5202018-04-23 10:32:45 -0700132 const int32_t mBytesPerFrame;
Phil Burk882c5202018-04-23 10:32:45 -0700133 std::unique_ptr<FifoControllerBase> mFifo{};
Phil Burkfd911c12017-01-03 17:15:39 -0800134};
135
Phil Burk8f4fe502020-07-15 23:54:50 +0000136// Define two subclasses to handle the two ways that storage is allocated.
137
138// Allocate storage internally.
139class FifoBufferAllocated : public FifoBuffer {
140public:
141 FifoBufferAllocated(int32_t bytesPerFrame, fifo_frames_t capacityInFrames);
142
143private:
144
145 uint8_t *getStorage() const override {
146 return mInternalStorage.get();
147 };
148
149 std::unique_ptr<uint8_t[]> mInternalStorage;
150};
151
152// Allocate storage externally and pass it in.
153class FifoBufferIndirect : public FifoBuffer {
154public:
155 // We use raw pointers because the memory may be
156 // in the middle of an allocated block and cannot be deleted directly.
157 FifoBufferIndirect(int32_t bytesPerFrame,
158 fifo_frames_t capacityInFrames,
159 fifo_counter_t* readCounterAddress,
160 fifo_counter_t* writeCounterAddress,
161 void* dataStorageAddress);
162
163private:
164
165 uint8_t *getStorage() const override {
166 return mExternalStorage;
167 };
168
169 uint8_t *mExternalStorage = nullptr;
170};
171
jiabind5bd06a2021-04-27 22:04:08 +0000172} // namespace android
Phil Burk7f6b40d2017-02-09 13:18:38 -0800173
Phil Burkfd911c12017-01-03 17:15:39 -0800174#endif //FIFO_FIFO_BUFFER_H