• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <cstring>
18 #include <unistd.h>
19 
20 
21 #define LOG_TAG "FifoBuffer"
22 //#define LOG_NDEBUG 0
23 #include <utils/Log.h>
24 
25 #include <algorithm>
26 #include <memory>
27 
28 #include "FifoControllerBase.h"
29 #include "FifoController.h"
30 #include "FifoControllerIndirect.h"
31 #include "FifoBuffer.h"
32 
33 using android::FifoBuffer;
34 using android::FifoBufferAllocated;
35 using android::FifoBufferIndirect;
36 using android::fifo_frames_t;
37 
FifoBuffer(int32_t bytesPerFrame)38 FifoBuffer::FifoBuffer(int32_t bytesPerFrame)
39         : mBytesPerFrame(bytesPerFrame) {}
40 
FifoBufferAllocated(int32_t bytesPerFrame,fifo_frames_t capacityInFrames)41 FifoBufferAllocated::FifoBufferAllocated(int32_t bytesPerFrame, fifo_frames_t capacityInFrames)
42         : FifoBuffer(bytesPerFrame)
43 {
44     mFifo = std::make_unique<FifoController>(capacityInFrames, capacityInFrames);
45     // allocate buffer
46     int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
47     mInternalStorage = std::make_unique<uint8_t[]>(bytesPerBuffer);
48     ALOGV("%s() capacityInFrames = %d, bytesPerFrame = %d",
49           __func__, capacityInFrames, bytesPerFrame);
50 }
51 
FifoBufferIndirect(int32_t bytesPerFrame,fifo_frames_t capacityInFrames,fifo_counter_t * readIndexAddress,fifo_counter_t * writeIndexAddress,void * dataStorageAddress)52 FifoBufferIndirect::FifoBufferIndirect( int32_t   bytesPerFrame,
53                         fifo_frames_t   capacityInFrames,
54                         fifo_counter_t *readIndexAddress,
55                         fifo_counter_t *writeIndexAddress,
56                         void *  dataStorageAddress
57                         )
58         : FifoBuffer(bytesPerFrame)
59         , mExternalStorage(static_cast<uint8_t *>(dataStorageAddress))
60 {
61     mFifo = std::make_unique<FifoControllerIndirect>(capacityInFrames,
62                                        capacityInFrames,
63                                        readIndexAddress,
64                                        writeIndexAddress);
65 }
66 
convertFramesToBytes(fifo_frames_t frames)67 int32_t FifoBuffer::convertFramesToBytes(fifo_frames_t frames) {
68     return frames * mBytesPerFrame;
69 }
70 
fillWrappingBuffer(WrappingBuffer * wrappingBuffer,int32_t framesAvailable,int32_t startIndex)71 void FifoBuffer::fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
72                                     int32_t framesAvailable,
73                                     int32_t startIndex) {
74     wrappingBuffer->data[1] = nullptr;
75     wrappingBuffer->numFrames[1] = 0;
76     uint8_t *storage = getStorage();
77     if (framesAvailable > 0) {
78         fifo_frames_t capacity = mFifo->getCapacity();
79         uint8_t *source = &storage[convertFramesToBytes(startIndex)];
80         // Does the available data cross the end of the FIFO?
81         if ((startIndex + framesAvailable) > capacity) {
82             wrappingBuffer->data[0] = source;
83             fifo_frames_t firstFrames = capacity - startIndex;
84             wrappingBuffer->numFrames[0] = firstFrames;
85             wrappingBuffer->data[1] = &storage[0];
86             wrappingBuffer->numFrames[1] = framesAvailable - firstFrames;
87         } else {
88             wrappingBuffer->data[0] = source;
89             wrappingBuffer->numFrames[0] = framesAvailable;
90         }
91     } else {
92         wrappingBuffer->data[0] = nullptr;
93         wrappingBuffer->numFrames[0] = 0;
94     }
95 }
96 
getFullDataAvailable(WrappingBuffer * wrappingBuffer)97 fifo_frames_t FifoBuffer::getFullDataAvailable(WrappingBuffer *wrappingBuffer) {
98     // The FIFO might be overfull so clip to capacity.
99     fifo_frames_t framesAvailable = std::min(mFifo->getFullFramesAvailable(),
100                                              mFifo->getCapacity());
101     fifo_frames_t startIndex = mFifo->getReadIndex();
102     fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
103     return framesAvailable;
104 }
105 
getEmptyRoomAvailable(WrappingBuffer * wrappingBuffer)106 fifo_frames_t FifoBuffer::getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer) {
107     // The FIFO might have underrun so clip to capacity.
108     fifo_frames_t framesAvailable = std::min(mFifo->getEmptyFramesAvailable(),
109                                              mFifo->getCapacity());
110     fifo_frames_t startIndex = mFifo->getWriteIndex();
111     fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
112     return framesAvailable;
113 }
114 
read(void * buffer,fifo_frames_t numFrames)115 fifo_frames_t FifoBuffer::read(void *buffer, fifo_frames_t numFrames) {
116     WrappingBuffer wrappingBuffer;
117     uint8_t *destination = (uint8_t *) buffer;
118     fifo_frames_t framesLeft = numFrames;
119 
120     getFullDataAvailable(&wrappingBuffer);
121 
122     // Read data in one or two parts.
123     int partIndex = 0;
124     while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
125         fifo_frames_t framesToRead = framesLeft;
126         fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
127         if (framesAvailable > 0) {
128             if (framesToRead > framesAvailable) {
129                 framesToRead = framesAvailable;
130             }
131             int32_t numBytes = convertFramesToBytes(framesToRead);
132             memcpy(destination, wrappingBuffer.data[partIndex], numBytes);
133 
134             destination += numBytes;
135             framesLeft -= framesToRead;
136         } else {
137             break;
138         }
139         partIndex++;
140     }
141     fifo_frames_t framesRead = numFrames - framesLeft;
142     mFifo->advanceReadIndex(framesRead);
143     return framesRead;
144 }
145 
write(const void * buffer,fifo_frames_t numFrames)146 fifo_frames_t FifoBuffer::write(const void *buffer, fifo_frames_t numFrames) {
147     WrappingBuffer wrappingBuffer;
148     uint8_t *source = (uint8_t *) buffer;
149     fifo_frames_t framesLeft = numFrames;
150 
151     getEmptyRoomAvailable(&wrappingBuffer);
152 
153     // Write data in one or two parts.
154     int partIndex = 0;
155     while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
156         fifo_frames_t framesToWrite = framesLeft;
157         fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
158         if (framesAvailable > 0) {
159             if (framesToWrite > framesAvailable) {
160                 framesToWrite = framesAvailable;
161             }
162             int32_t numBytes = convertFramesToBytes(framesToWrite);
163             memcpy(wrappingBuffer.data[partIndex], source, numBytes);
164 
165             source += numBytes;
166             framesLeft -= framesToWrite;
167         } else {
168             break;
169         }
170         partIndex++;
171     }
172     fifo_frames_t framesWritten = numFrames - framesLeft;
173     mFifo->advanceWriteIndex(framesWritten);
174     return framesWritten;
175 }
176 
getThreshold()177 fifo_frames_t FifoBuffer::getThreshold() {
178     return mFifo->getThreshold();
179 }
180 
setThreshold(fifo_frames_t threshold)181 void FifoBuffer::setThreshold(fifo_frames_t threshold) {
182     mFifo->setThreshold(threshold);
183 }
184 
getBufferCapacityInFrames()185 fifo_frames_t FifoBuffer::getBufferCapacityInFrames() {
186     return mFifo->getCapacity();
187 }
188 
eraseMemory()189 void FifoBuffer::eraseMemory() {
190     int32_t numBytes = convertFramesToBytes(getBufferCapacityInFrames());
191     if (numBytes > 0) {
192         memset(getStorage(), 0, (size_t) numBytes);
193     }
194 }
195 
eraseEmptyMemory(fifo_frames_t numFrames)196 fifo_frames_t FifoBuffer::eraseEmptyMemory(fifo_frames_t numFrames) {
197     WrappingBuffer wrappingBuffer;
198     fifo_frames_t framesLeft = numFrames;
199 
200     getEmptyRoomAvailable(&wrappingBuffer);
201 
202     // Erase data in one or two parts.
203     int partIndex = 0;
204     while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) {
205         fifo_frames_t framesToWrite = framesLeft;
206         fifo_frames_t framesAvailable = wrappingBuffer.numFrames[partIndex];
207         if (framesAvailable > 0) {
208             if (framesToWrite > framesAvailable) {
209                 framesToWrite = framesAvailable;
210             }
211             int32_t numBytes = convertFramesToBytes(framesToWrite);
212             memset(wrappingBuffer.data[partIndex], 0, numBytes);
213             framesLeft -= framesToWrite;
214         } else {
215             break;
216         }
217         partIndex++;
218     }
219     return numFrames - framesLeft; // number erased
220 }
221