1/*
2 * Copyright (C) 2011 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
18package android.filterpacks.performance;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.Frame;
23import android.filterfw.core.FrameFormat;
24import android.filterfw.core.GenerateFieldPort;
25import android.filterfw.format.ObjectFormat;
26import android.os.SystemClock;
27
28/**
29 * @hide
30 */
31public class ThroughputFilter extends Filter {
32
33    @GenerateFieldPort(name = "period", hasDefault = true)
34    private int mPeriod = 5;
35
36    private long mLastTime = 0;
37
38    private int mTotalFrameCount = 0;
39    private int mPeriodFrameCount = 0;
40
41    private FrameFormat mOutputFormat;
42
43    public ThroughputFilter(String name) {
44        super(name);
45    }
46
47    @Override
48    public void setupPorts() {
49        // Add input ports
50        addInputPort("frame");
51
52        // Add output ports
53        mOutputFormat = ObjectFormat.fromClass(Throughput.class, FrameFormat.TARGET_SIMPLE);
54        addOutputBasedOnInput("frame", "frame");
55        addOutputPort("throughput", mOutputFormat);
56    }
57
58    @Override
59    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
60        return inputFormat;
61    }
62
63    @Override
64    public void open(FilterContext env) {
65        mTotalFrameCount = 0;
66        mPeriodFrameCount = 0;
67        mLastTime = 0;
68    }
69
70    @Override
71    public void process(FilterContext context) {
72        // Pass through input frame
73        Frame input = pullInput("frame");
74        pushOutput("frame", input);
75
76        // Update stats
77        ++mTotalFrameCount;
78        ++mPeriodFrameCount;
79
80        // Check clock
81        if (mLastTime == 0) {
82            mLastTime = SystemClock.elapsedRealtime();
83        }
84        long curTime = SystemClock.elapsedRealtime();
85
86        // Output throughput info if time period is up
87        if ((curTime - mLastTime) >= (mPeriod * 1000)) {
88            FrameFormat inputFormat = input.getFormat();
89            int pixelCount = inputFormat.getWidth() * inputFormat.getHeight();
90            Throughput throughput = new Throughput(mTotalFrameCount,
91                                                   mPeriodFrameCount,
92                                                   mPeriod,
93                                                   pixelCount);
94            Frame throughputFrame = context.getFrameManager().newFrame(mOutputFormat);
95            throughputFrame.setObjectValue(throughput);
96            pushOutput("throughput", throughputFrame);
97            mLastTime = curTime;
98            mPeriodFrameCount = 0;
99        }
100    }
101
102
103}
104