blob: 37548f0cfb9c4d796db9d6ea456156badc3fdd27 [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:
Phil Burk8f4fe502020-07-15 23:54:50 +000041 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 Burk8f4fe502020-07-15 23:54:50 +0000118protected:
119
120 virtual uint8_t *getStorage() const = 0;
Phil Burk7f6b40d2017-02-09 13:18:38 -0800121
122 void fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
123 int32_t framesAvailable, int32_t startIndex);
124
Phil Burk882c5202018-04-23 10:32:45 -0700125 const int32_t mBytesPerFrame;
Phil Burk882c5202018-04-23 10:32:45 -0700126 std::unique_ptr<FifoControllerBase> mFifo{};
Phil Burkfd911c12017-01-03 17:15:39 -0800127};
128
Phil Burk8f4fe502020-07-15 23:54:50 +0000129// Define two subclasses to handle the two ways that storage is allocated.
130
131// Allocate storage internally.
132class FifoBufferAllocated : public FifoBuffer {
133public:
134 FifoBufferAllocated(int32_t bytesPerFrame, fifo_frames_t capacityInFrames);
135
136private:
137
138 uint8_t *getStorage() const override {
139 return mInternalStorage.get();
140 };
141
142 std::unique_ptr<uint8_t[]> mInternalStorage;
143};
144
145// Allocate storage externally and pass it in.
146class FifoBufferIndirect : public FifoBuffer {
147public:
148 // We use raw pointers because the memory may be
149 // in the middle of an allocated block and cannot be deleted directly.
150 FifoBufferIndirect(int32_t bytesPerFrame,
151 fifo_frames_t capacityInFrames,
152 fifo_counter_t* readCounterAddress,
153 fifo_counter_t* writeCounterAddress,
154 void* dataStorageAddress);
155
156private:
157
158 uint8_t *getStorage() const override {
159 return mExternalStorage;
160 };
161
162 uint8_t *mExternalStorage = nullptr;
163};
164
Phil Burk7f6b40d2017-02-09 13:18:38 -0800165} // android
166
Phil Burkfd911c12017-01-03 17:15:39 -0800167#endif //FIFO_FIFO_BUFFER_H