blob: 0d188c413e7f0a54711a87d7f5ed6953e414e615 [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:
41 FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames);
42
Phil Burk7f6b40d2017-02-09 13:18:38 -080043 FifoBuffer(int32_t bytesPerFrame,
44 fifo_frames_t capacityInFrames,
45 fifo_counter_t *readCounterAddress,
46 fifo_counter_t *writeCounterAddress,
47 void *dataStorageAddress);
Phil Burkfd911c12017-01-03 17:15:39 -080048
49 ~FifoBuffer();
50
51 int32_t convertFramesToBytes(fifo_frames_t frames);
52
53 fifo_frames_t read(void *destination, fifo_frames_t framesToRead);
54
55 fifo_frames_t write(const void *source, fifo_frames_t framesToWrite);
56
57 fifo_frames_t getThreshold();
Phil Burk7f6b40d2017-02-09 13:18:38 -080058
Phil Burkfd911c12017-01-03 17:15:39 -080059 void setThreshold(fifo_frames_t threshold);
60
61 fifo_frames_t getBufferCapacityInFrames();
62
Phil Burk7f6b40d2017-02-09 13:18:38 -080063 /**
64 * Return pointer to available full frames in data1 and set size in numFrames1.
65 * if the data is split across the end of the FIFO then set data2 and numFrames2.
66 * Other wise set them to null
67 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070068 * @return total full frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080069 */
Phil Burkfd34a932017-07-19 07:03:52 -070070 fifo_frames_t getFullDataAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080071
72 /**
73 * Return pointer to available empty frames in data1 and set size in numFrames1.
74 * if the room is split across the end of the FIFO then set data2 and numFrames2.
75 * Other wise set them to null
76 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070077 * @return total empty frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080078 */
Phil Burkfd34a932017-07-19 07:03:52 -070079 fifo_frames_t getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080080
Phil Burkfd911c12017-01-03 17:15:39 -080081 int32_t getBytesPerFrame() {
82 return mBytesPerFrame;
83 }
84
Phil Burk882c5202018-04-23 10:32:45 -070085 // Proxy methods for the internal FifoController
86
Phil Burkfd911c12017-01-03 17:15:39 -080087 fifo_counter_t getReadCounter() {
88 return mFifo->getReadCounter();
89 }
90
91 void setReadCounter(fifo_counter_t n) {
92 mFifo->setReadCounter(n);
93 }
94
95 fifo_counter_t getWriteCounter() {
96 return mFifo->getWriteCounter();
97 }
98
99 void setWriteCounter(fifo_counter_t n) {
100 mFifo->setWriteCounter(n);
101 }
102
Phil Burk882c5202018-04-23 10:32:45 -0700103 void advanceReadIndex(fifo_frames_t numFrames) {
104 mFifo->advanceReadIndex(numFrames);
105 }
106
107 void advanceWriteIndex(fifo_frames_t numFrames) {
108 mFifo->advanceWriteIndex(numFrames);
109 }
110
111 fifo_frames_t getEmptyFramesAvailable() {
112 return mFifo->getEmptyFramesAvailable();
113 }
114
115 fifo_frames_t getFullFramesAvailable() {
116 return mFifo->getFullFramesAvailable();
117 }
118
Phil Burkea04d972017-08-07 12:30:44 -0700119 /*
120 * This is generally only called before or after the buffer is used.
121 */
122 void eraseMemory();
123
Phil Burkfd911c12017-01-03 17:15:39 -0800124private:
Phil Burk7f6b40d2017-02-09 13:18:38 -0800125
126 void fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
127 int32_t framesAvailable, int32_t startIndex);
128
Phil Burk882c5202018-04-23 10:32:45 -0700129 const int32_t mBytesPerFrame;
130 // We do not use a std::unique_ptr for mStorage because it is often a pointer to
131 // memory shared between processes and cannot be deleted trivially.
132 uint8_t *mStorage = nullptr;
133 bool mStorageOwned = false; // did this object allocate the storage?
134 std::unique_ptr<FifoControllerBase> mFifo{};
Phil Burkfd911c12017-01-03 17:15:39 -0800135};
136
Phil Burk7f6b40d2017-02-09 13:18:38 -0800137} // android
138
Phil Burkfd911c12017-01-03 17:15:39 -0800139#endif //FIFO_FIFO_BUFFER_H