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