blob: f5a9e27c5955dd7eba5f97819d0a0c507f426040 [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
20#include <stdint.h>
21
22#include "FifoControllerBase.h"
23
Phil Burk7f6b40d2017-02-09 13:18:38 -080024namespace android {
25
26/**
27 * Structure that represents a region in a circular buffer that might be at the
28 * end of the array and split in two.
29 */
30struct WrappingBuffer {
31 enum {
32 SIZE = 2
33 };
34 void *data[SIZE];
35 int32_t numFrames[SIZE];
36};
37
Phil Burkfd911c12017-01-03 17:15:39 -080038class FifoBuffer {
39public:
40 FifoBuffer(int32_t bytesPerFrame, fifo_frames_t capacityInFrames);
41
Phil Burk7f6b40d2017-02-09 13:18:38 -080042 FifoBuffer(int32_t bytesPerFrame,
43 fifo_frames_t capacityInFrames,
44 fifo_counter_t *readCounterAddress,
45 fifo_counter_t *writeCounterAddress,
46 void *dataStorageAddress);
Phil Burkfd911c12017-01-03 17:15:39 -080047
48 ~FifoBuffer();
49
50 int32_t convertFramesToBytes(fifo_frames_t frames);
51
52 fifo_frames_t read(void *destination, fifo_frames_t framesToRead);
53
54 fifo_frames_t write(const void *source, fifo_frames_t framesToWrite);
55
56 fifo_frames_t getThreshold();
Phil Burk7f6b40d2017-02-09 13:18:38 -080057
Phil Burkfd911c12017-01-03 17:15:39 -080058 void setThreshold(fifo_frames_t threshold);
59
60 fifo_frames_t getBufferCapacityInFrames();
61
Phil Burk7f6b40d2017-02-09 13:18:38 -080062 /**
63 * Return pointer to available full frames in data1 and set size in numFrames1.
64 * if the data is split across the end of the FIFO then set data2 and numFrames2.
65 * Other wise set them to null
66 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070067 * @return total full frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080068 */
Phil Burkfd34a932017-07-19 07:03:52 -070069 fifo_frames_t getFullDataAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080070
71 /**
72 * Return pointer to available empty frames in data1 and set size in numFrames1.
73 * if the room is split across the end of the FIFO then set data2 and numFrames2.
74 * Other wise set them to null
75 * @param wrappingBuffer
Phil Burkfd34a932017-07-19 07:03:52 -070076 * @return total empty frames available
Phil Burk7f6b40d2017-02-09 13:18:38 -080077 */
Phil Burkfd34a932017-07-19 07:03:52 -070078 fifo_frames_t getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer);
Phil Burk7f6b40d2017-02-09 13:18:38 -080079
80 /**
81 * Copy data from the FIFO into the buffer.
82 * @param buffer
83 * @param numFrames
84 * @return
85 */
Phil Burkfd911c12017-01-03 17:15:39 -080086 fifo_frames_t readNow(void *buffer, fifo_frames_t numFrames);
87
88 int64_t getNextReadTime(int32_t frameRate);
89
90 int32_t getUnderrunCount() const { return mUnderrunCount; }
91
92 FifoControllerBase *getFifoControllerBase() { return mFifo; }
93
94 int32_t getBytesPerFrame() {
95 return mBytesPerFrame;
96 }
97
98 fifo_counter_t getReadCounter() {
99 return mFifo->getReadCounter();
100 }
101
102 void setReadCounter(fifo_counter_t n) {
103 mFifo->setReadCounter(n);
104 }
105
106 fifo_counter_t getWriteCounter() {
107 return mFifo->getWriteCounter();
108 }
109
110 void setWriteCounter(fifo_counter_t n) {
111 mFifo->setWriteCounter(n);
112 }
113
Phil Burkea04d972017-08-07 12:30:44 -0700114 /*
115 * This is generally only called before or after the buffer is used.
116 */
117 void eraseMemory();
118
Phil Burkfd911c12017-01-03 17:15:39 -0800119private:
Phil Burk7f6b40d2017-02-09 13:18:38 -0800120
121 void fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
122 int32_t framesAvailable, int32_t startIndex);
123
Phil Burkfd911c12017-01-03 17:15:39 -0800124 const fifo_frames_t mFrameCapacity;
Phil Burk7f6b40d2017-02-09 13:18:38 -0800125 const int32_t mBytesPerFrame;
126 uint8_t *mStorage;
127 bool mStorageOwned; // did this object allocate the storage?
Phil Burkfd911c12017-01-03 17:15:39 -0800128 FifoControllerBase *mFifo;
Phil Burk7f6b40d2017-02-09 13:18:38 -0800129 fifo_counter_t mFramesReadCount;
130 fifo_counter_t mFramesUnderrunCount;
131 int32_t mUnderrunCount; // need? just use frames
132 int32_t mLastReadSize;
Phil Burkfd911c12017-01-03 17:15:39 -0800133};
134
Phil Burk7f6b40d2017-02-09 13:18:38 -0800135} // android
136
Phil Burkfd911c12017-01-03 17:15:39 -0800137#endif //FIFO_FIFO_BUFFER_H