blob: 66e247f5757f7d6dbb6560aac8dba91e3410d2e6 [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#define LOG_TAG "FifoControllerBase"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include "FifoControllerBase.h"
23
Phil Burk7f6b40d2017-02-09 13:18:38 -080024using namespace android; // TODO just import names needed
25
Phil Burkfd911c12017-01-03 17:15:39 -080026FifoControllerBase::FifoControllerBase(fifo_frames_t capacity, fifo_frames_t threshold)
27 : mCapacity(capacity)
28 , mThreshold(threshold)
29{
30}
31
32FifoControllerBase::~FifoControllerBase() {
33}
34
35fifo_frames_t FifoControllerBase::getFullFramesAvailable() {
36 return (fifo_frames_t) (getWriteCounter() - getReadCounter());
37}
38
39fifo_frames_t FifoControllerBase::getReadIndex() {
40 // % works with non-power of two sizes
Phil Burk58f7ff52018-12-03 14:16:46 -080041 return (fifo_frames_t) ((uint64_t)getReadCounter() % mCapacity);
Phil Burkfd911c12017-01-03 17:15:39 -080042}
43
44void FifoControllerBase::advanceReadIndex(fifo_frames_t numFrames) {
45 setReadCounter(getReadCounter() + numFrames);
46}
47
48fifo_frames_t FifoControllerBase::getEmptyFramesAvailable() {
49 return (int32_t)(mThreshold - getFullFramesAvailable());
50}
51
52fifo_frames_t FifoControllerBase::getWriteIndex() {
53 // % works with non-power of two sizes
Phil Burk58f7ff52018-12-03 14:16:46 -080054 return (fifo_frames_t) ((uint64_t)getWriteCounter() % mCapacity);
Phil Burkfd911c12017-01-03 17:15:39 -080055}
56
57void FifoControllerBase::advanceWriteIndex(fifo_frames_t numFrames) {
58 setWriteCounter(getWriteCounter() + numFrames);
59}
60
61void FifoControllerBase::setThreshold(fifo_frames_t threshold) {
Phil Burk5c4f8262017-11-17 12:16:22 -080062 if (threshold > mCapacity) {
63 threshold = mCapacity;
64 } else if (threshold < 0) {
65 threshold = 0;
66 }
Phil Burkfd911c12017-01-03 17:15:39 -080067 mThreshold = threshold;
68}