blob: 5c118822c27fd3e385bc888a8137e4c6efda002e [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#include <cstring>
18#include <unistd.h>
19
Phil Burkc0c70e32017-02-09 13:18:38 -080020
Phil Burkfd911c12017-01-03 17:15:39 -080021#define LOG_TAG "FifoBuffer"
22//#define LOG_NDEBUG 0
23#include <utils/Log.h>
24
Phil Burkf7da0b92018-04-20 17:24:38 -070025#include <algorithm>
Phil Burk882c5202018-04-23 10:32:45 -070026#include <memory>
Phil Burkf7da0b92018-04-20 17:24:38 -070027
Phil Burkfd911c12017-01-03 17:15:39 -080028#include "FifoControllerBase.h"
29#include "FifoController.h"
30#include "FifoControllerIndirect.h"
31#include "FifoBuffer.h"
32
Phil Burk882c5202018-04-23 10:32:45 -070033using android::FifoBuffer;
Phil Burk8f4fe502020-07-15 23:54:50 +000034using android::FifoBufferAllocated;
35using android::FifoBufferIndirect;
Phil Burk882c5202018-04-23 10:32:45 -070036using android::fifo_frames_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080037
Phil Burk8f4fe502020-07-15 23:54:50 +000038FifoBuffer::FifoBuffer(int32_t bytesPerFrame)
39 : mBytesPerFrame(bytesPerFrame) {}
40
41FifoBufferAllocated::FifoBufferAllocated(int32_t bytesPerFrame, fifo_frames_t capacityInFrames)
42 : FifoBuffer(bytesPerFrame)
Phil Burkfd911c12017-01-03 17:15:39 -080043{
Phil Burk882c5202018-04-23 10:32:45 -070044 mFifo = std::make_unique<FifoController>(capacityInFrames, capacityInFrames);
Phil Burkfd911c12017-01-03 17:15:39 -080045 // allocate buffer
46 int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
Phil Burk8f4fe502020-07-15 23:54:50 +000047 mInternalStorage = std::make_unique<uint8_t[]>(bytesPerBuffer);
Phil Burk882c5202018-04-23 10:32:45 -070048 ALOGV("%s() capacityInFrames = %d, bytesPerFrame = %d",
49 __func__, capacityInFrames, bytesPerFrame);
Phil Burkfd911c12017-01-03 17:15:39 -080050}
51
Phil Burk8f4fe502020-07-15 23:54:50 +000052FifoBufferIndirect::FifoBufferIndirect( int32_t bytesPerFrame,
Phil Burkfd911c12017-01-03 17:15:39 -080053 fifo_frames_t capacityInFrames,
Phil Burk8f4fe502020-07-15 23:54:50 +000054 fifo_counter_t *readIndexAddress,
55 fifo_counter_t *writeIndexAddress,
Phil Burkfd911c12017-01-03 17:15:39 -080056 void * dataStorageAddress
57 )
Phil Burk8f4fe502020-07-15 23:54:50 +000058 : FifoBuffer(bytesPerFrame)
59 , mExternalStorage(static_cast<uint8_t *>(dataStorageAddress))
Phil Burkfd911c12017-01-03 17:15:39 -080060{
Phil Burk882c5202018-04-23 10:32:45 -070061 mFifo = std::make_unique<FifoControllerIndirect>(capacityInFrames,
Phil Burkfd911c12017-01-03 17:15:39 -080062 capacityInFrames,
63 readIndexAddress,
64 writeIndexAddress);
Phil Burkfd911c12017-01-03 17:15:39 -080065}
66
Phil Burkfd911c12017-01-03 17:15:39 -080067int32_t FifoBuffer::convertFramesToBytes(fifo_frames_t frames) {
68 return frames * mBytesPerFrame;
69}
70
Phil Burkc0c70e32017-02-09 13:18:38 -080071void FifoBuffer::fillWrappingBuffer(WrappingBuffer *wrappingBuffer,
72 int32_t framesAvailable,
73 int32_t startIndex) {
74 wrappingBuffer->data[1] = nullptr;
75 wrappingBuffer->numFrames[1] = 0;
Phil Burk8f4fe502020-07-15 23:54:50 +000076 uint8_t *storage = getStorage();
Phil Burkc0c70e32017-02-09 13:18:38 -080077 if (framesAvailable > 0) {
Phil Burk882c5202018-04-23 10:32:45 -070078 fifo_frames_t capacity = mFifo->getCapacity();
Phil Burk8f4fe502020-07-15 23:54:50 +000079 uint8_t *source = &storage[convertFramesToBytes(startIndex)];
Phil Burkc0c70e32017-02-09 13:18:38 -080080 // Does the available data cross the end of the FIFO?
Phil Burk882c5202018-04-23 10:32:45 -070081 if ((startIndex + framesAvailable) > capacity) {
Phil Burkc0c70e32017-02-09 13:18:38 -080082 wrappingBuffer->data[0] = source;
Phil Burk882c5202018-04-23 10:32:45 -070083 fifo_frames_t firstFrames = capacity - startIndex;
Phil Burkf7da0b92018-04-20 17:24:38 -070084 wrappingBuffer->numFrames[0] = firstFrames;
Phil Burk8f4fe502020-07-15 23:54:50 +000085 wrappingBuffer->data[1] = &storage[0];
Phil Burkf7da0b92018-04-20 17:24:38 -070086 wrappingBuffer->numFrames[1] = framesAvailable - firstFrames;
Phil Burkc0c70e32017-02-09 13:18:38 -080087 } else {
88 wrappingBuffer->data[0] = source;
89 wrappingBuffer->numFrames[0] = framesAvailable;
90 }
Phil Burkfd911c12017-01-03 17:15:39 -080091 } else {
Phil Burkc0c70e32017-02-09 13:18:38 -080092 wrappingBuffer->data[0] = nullptr;
93 wrappingBuffer->numFrames[0] = 0;
Phil Burkfd911c12017-01-03 17:15:39 -080094 }
Phil Burkfd911c12017-01-03 17:15:39 -080095}
96
Phil Burkfd34a932017-07-19 07:03:52 -070097fifo_frames_t FifoBuffer::getFullDataAvailable(WrappingBuffer *wrappingBuffer) {
Phil Burkf7da0b92018-04-20 17:24:38 -070098 // The FIFO might be overfull so clip to capacity.
Phil Burk882c5202018-04-23 10:32:45 -070099 fifo_frames_t framesAvailable = std::min(mFifo->getFullFramesAvailable(),
100 mFifo->getCapacity());
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 fifo_frames_t startIndex = mFifo->getReadIndex();
102 fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
Phil Burkfd34a932017-07-19 07:03:52 -0700103 return framesAvailable;
Phil Burkc0c70e32017-02-09 13:18:38 -0800104}
105
Phil Burkfd34a932017-07-19 07:03:52 -0700106fifo_frames_t FifoBuffer::getEmptyRoomAvailable(WrappingBuffer *wrappingBuffer) {
Phil Burkf7da0b92018-04-20 17:24:38 -0700107 // The FIFO might have underrun so clip to capacity.
Phil Burk882c5202018-04-23 10:32:45 -0700108 fifo_frames_t framesAvailable = std::min(mFifo->getEmptyFramesAvailable(),
109 mFifo->getCapacity());
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 fifo_frames_t startIndex = mFifo->getWriteIndex();
111 fillWrappingBuffer(wrappingBuffer, framesAvailable, startIndex);
Phil Burkfd34a932017-07-19 07:03:52 -0700112 return framesAvailable;
Phil Burkc0c70e32017-02-09 13:18:38 -0800113}
Phil Burkfd911c12017-01-03 17:15:39 -0800114
Phil Burkc0c70e32017-02-09 13:18:38 -0800115fifo_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;
Phil Burkfd911c12017-01-03 17:15:39 -0800119
Phil Burkc0c70e32017-02-09 13:18:38 -0800120 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];
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 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;
Phil Burk71f35bb2017-04-13 16:05:07 -0700136 } else {
137 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 }
139 partIndex++;
140 }
141 fifo_frames_t framesRead = numFrames - framesLeft;
142 mFifo->advanceReadIndex(framesRead);
143 return framesRead;
144}
145
146fifo_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 // Read 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;
Phil Burk71f35bb2017-04-13 16:05:07 -0700167 } else {
168 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800169 }
170 partIndex++;
171 }
172 fifo_frames_t framesWritten = numFrames - framesLeft;
173 mFifo->advanceWriteIndex(framesWritten);
174 return framesWritten;
Phil Burkfd911c12017-01-03 17:15:39 -0800175}
176
Phil Burkfd911c12017-01-03 17:15:39 -0800177fifo_frames_t FifoBuffer::getThreshold() {
178 return mFifo->getThreshold();
179}
180
181void FifoBuffer::setThreshold(fifo_frames_t threshold) {
182 mFifo->setThreshold(threshold);
183}
184
185fifo_frames_t FifoBuffer::getBufferCapacityInFrames() {
186 return mFifo->getCapacity();
187}
188
Phil Burkea04d972017-08-07 12:30:44 -0700189void FifoBuffer::eraseMemory() {
190 int32_t numBytes = convertFramesToBytes(getBufferCapacityInFrames());
191 if (numBytes > 0) {
Phil Burk8f4fe502020-07-15 23:54:50 +0000192 memset(getStorage(), 0, (size_t) numBytes);
Phil Burkea04d972017-08-07 12:30:44 -0700193 }
194}