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 #define LOG_TAG "FifoControllerBase"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <stdint.h>
22 #include "FifoControllerBase.h"
23
24 using namespace android; // TODO just import names needed
25
FifoControllerBase(fifo_frames_t capacity,fifo_frames_t threshold)26 FifoControllerBase::FifoControllerBase(fifo_frames_t capacity, fifo_frames_t threshold)
27 : mCapacity(capacity)
28 , mThreshold(threshold)
29 {
30 }
31
getFullFramesAvailable()32 fifo_frames_t FifoControllerBase::getFullFramesAvailable() {
33 fifo_frames_t temp = 0;
34 __builtin_sub_overflow(getWriteCounter(), getReadCounter(), &temp);
35 return temp;
36 }
37
getReadIndex()38 fifo_frames_t FifoControllerBase::getReadIndex() {
39 // % works with non-power of two sizes
40 return (fifo_frames_t) ((uint64_t)getReadCounter() % mCapacity);
41 }
42
advanceReadIndex(fifo_frames_t numFrames)43 void FifoControllerBase::advanceReadIndex(fifo_frames_t numFrames) {
44 fifo_counter_t temp = 0;
45 __builtin_add_overflow(getReadCounter(), numFrames, &temp);
46 setReadCounter(temp);
47 }
48
getEmptyFramesAvailable()49 fifo_frames_t FifoControllerBase::getEmptyFramesAvailable() {
50 return (int32_t)(mThreshold - getFullFramesAvailable());
51 }
52
getWriteIndex()53 fifo_frames_t FifoControllerBase::getWriteIndex() {
54 // % works with non-power of two sizes
55 return (fifo_frames_t) ((uint64_t)getWriteCounter() % mCapacity);
56 }
57
advanceWriteIndex(fifo_frames_t numFrames)58 void FifoControllerBase::advanceWriteIndex(fifo_frames_t numFrames) {
59 fifo_counter_t temp = 0;
60 __builtin_add_overflow(getWriteCounter(), numFrames, &temp);
61 setWriteCounter(temp);
62 }
63
setThreshold(fifo_frames_t threshold)64 void FifoControllerBase::setThreshold(fifo_frames_t threshold) {
65 if (threshold > mCapacity) {
66 threshold = mCapacity;
67 } else if (threshold < 0) {
68 threshold = 0;
69 }
70 mThreshold = threshold;
71 }
72