1befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed/*
25070c694011ddc30673ec68a9d1d74dc713c49f2Duy Truong * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
4befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * Redistribution and use in source and binary forms, with or without
5befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * modification, are permitted provided that the following conditions are
6befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * met:
7befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *   * Redistributions of source code must retain the above copyright
8befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     notice, this list of conditions and the following disclaimer.
9befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *   * Redistributions in binary form must reproduce the above
10befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     copyright notice, this list of conditions and the following
11befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     disclaimer in the documentation and/or other materials provided
12befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     with the distribution.
135070c694011ddc30673ec68a9d1d74dc713c49f2Duy Truong *   * Neither the name of The Linux Foundation nor the names of its
14befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     contributors may be used to endorse or promote products derived
15befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *     from this software without specific prior written permission.
16befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed *
17befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed */
29befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
30befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#ifndef INCLUDE_PROFILER
31befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#define INCLUDE_PROFILER
32befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
33befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#include <stdio.h>
34befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#include <utils/Singleton.h>
35befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#include <cutils/properties.h>
36befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#include <cutils/log.h>
37befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
38befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#ifndef DEBUG_CALC_FPS
39befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#define CALC_FPS() ((void)0)
40befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#define CALC_INIT() ((void)0)
41befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#else
420f859f6f0db0e0c05d8c319d8d597ad2731513aeNaseer Ahmed#define CALC_FPS() qdutils::CalcFps::getInstance().Fps()
430f859f6f0db0e0c05d8c319d8d597ad2731513aeNaseer Ahmed#define CALC_INIT() qdutils::CalcFps::getInstance().Init()
440f859f6f0db0e0c05d8c319d8d597ad2731513aeNaseer Ahmedusing namespace android;
450f859f6f0db0e0c05d8c319d8d597ad2731513aeNaseer Ahmednamespace qdutils {
46befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmedclass CalcFps : public Singleton<CalcFps> {
47befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    public:
48befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    CalcFps();
49befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    ~CalcFps();
50befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
51befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    void Init();
52befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    void Fps();
53befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
54befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    private:
55befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    static const unsigned int MAX_FPS_CALC_PERIOD_IN_FRAMES = 128;
56befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    static const unsigned int MAX_FRAMEARRIVAL_STEPS = 50;
57befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    static const unsigned int MAX_DEBUG_FPS_LEVEL = 2;
58befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
59befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    struct debug_fps_metadata_t {
60befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /*fps calculation based on time or number of frames*/
61befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        enum DfmType {
62befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed            DFM_FRAMES = 0,
63befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed            DFM_TIME   = 1,
64befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        };
65befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
66befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        DfmType type;
67befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
68befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* indicates how much time do we wait till we calculate FPS */
69befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        unsigned long time_period;
70befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
71befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /*indicates how much time elapsed since we report fps*/
72befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        float time_elapsed;
73befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
74befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* indicates how many frames do we wait till we calculate FPS */
75befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        unsigned int period;
76befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* current frame, will go upto period, and then reset */
77befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        unsigned int curr_frame;
78befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* frame will arrive at a multiple of 16666 us at the display.
79befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           This indicates how many steps to consider for our calculations.
80befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           For example, if framearrival_steps = 10, then the frame that arrived
81befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           after 166660 us or more will be ignored.
82befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           */
83befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        unsigned int framearrival_steps;
84befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* ignorethresh_us = framearrival_steps * 16666 */
85befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        nsecs_t      ignorethresh_us;
86befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* used to calculate the actual frame arrival step, the times might not be
87befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           accurate
88befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed           */
89befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        unsigned int margin_us;
90befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
91befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        /* actual data storage */
92befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        nsecs_t      framearrivals[MAX_FPS_CALC_PERIOD_IN_FRAMES];
93befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed        nsecs_t      accum_framearrivals[MAX_FRAMEARRIVAL_STEPS];
94befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    };
95befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
96befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    private:
97befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    void populate_debug_fps_metadata(void);
98befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    void print_fps(float fps);
99befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    void calc_fps(nsecs_t currtime_us);
100befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
101befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    private:
102befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    debug_fps_metadata_t debug_fps_metadata;
103befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed    unsigned int debug_fps_level;
104befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed};
1050f859f6f0db0e0c05d8c319d8d597ad2731513aeNaseer Ahmed};//namespace qdutils
106befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#endif
107befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed
108befc466069680cb8a65b1f22ad44723d949128c8Naseer Ahmed#endif // INCLUDE_PROFILER
109