Initialize Tizen 2.3
[adaptation/xorg/driver/xserver-xorg-module-xdbg.git] / lib / xdbg_log_fpsdebug.c
1 /**************************************************************************
2
3 xdbg
4
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <sys/types.h>
39 #include <xf86drm.h>
40 #include "xorg-server.h"
41 #include "xdbg.h"
42 #include "xdbg_log_int.h"
43 #include "xdbg_types.h"
44 #include "xdbg_log_fpsdebug.h"
45
46 #ifndef API
47 #define API __attribute__ ((visibility("default")))
48 #endif
49
50 #define FPS_DEBUG_TIMER_INTERVAL    500
51
52 static int init_fpsdebug = 0;
53 static Bool g_on = FALSE;
54
55 struct _fpsDebug
56 {
57     int nPanCount;
58     CARD32 tStart, tCur, tLast;
59     OsTimerPtr fpsTimer;
60     int  connector_type;
61     int  cid;
62 };
63
64 static struct
65 {
66     int type;
67     const char *name;
68 } connector_list[] =
69 {
70        { DRM_MODE_CONNECTOR_Unknown, "unknown" },
71        { DRM_MODE_CONNECTOR_VGA, "VGA" },
72        { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
73        { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
74        { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
75        { DRM_MODE_CONNECTOR_Composite, "composite" },
76        { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
77        { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
78        { DRM_MODE_CONNECTOR_Component, "component" },
79        { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
80        { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
81        { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
82        { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
83        { DRM_MODE_CONNECTOR_TV, "TV" },
84        { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
85        { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
86 };
87
88 static int
89 _get_connector_id (int connector_type)
90 {
91     int num = sizeof (connector_list) / sizeof (connector_list[0]);
92     int i;
93     int cid = 0;
94
95     for (i = 0; i < num; i++)
96     {
97         if (connector_type == connector_list[i].type)
98         {
99             cid = i;
100             break;
101         }
102     }
103
104     return cid;
105 }
106
107
108 static int
109 _fps_print_fps (FpsDebugPtr pFpsDebug, int isTimeOut)
110 {
111     double sec;
112
113     sec = (double) (pFpsDebug->tLast - pFpsDebug->tStart) /1000.0;
114     ErrorF ("[Xorg..V][%s][FB:(%3dframes %3.1ffps)] Dur:%3.3f...%s\n",
115             connector_list[pFpsDebug->cid].name, pFpsDebug->nPanCount, pFpsDebug->nPanCount / sec,
116             sec, isTimeOut ? "[TimeOut]" : "");
117     pFpsDebug->nPanCount = 0;
118
119     return 0;
120 }
121
122 static CARD32
123 _fps_frame_timeout (OsTimerPtr timer, CARD32 now, pointer arg)
124 {
125         _fps_print_fps (arg, TRUE);
126
127     return 0;
128 }
129
130
131 API FpsDebugPtr
132 xDbgLogFpsDebugCreate ()
133 {
134     FpsDebugPtr pFpsDebug = NULL;
135
136     init_fpsdebug = 1;
137
138     pFpsDebug = calloc (1, sizeof (struct _fpsDebug));
139     if (!pFpsDebug)
140     {
141         XDBG_ERROR (MXDBG, "fail to allocate the FpsDebug\n");
142         return NULL;
143     }
144
145     return pFpsDebug;
146 }
147
148 API void
149 xDbgLogFpsDebugDestroy (FpsDebugPtr pFpsDebug)
150 {
151     if (!pFpsDebug)
152         return;
153
154     free (pFpsDebug);
155     pFpsDebug = NULL;
156 }
157
158 API void
159 xDbgLogFpsDebugCount (FpsDebugPtr pFpsDebug, int connector_type)
160 {
161     /* if fpsdebug is off, do not count fps */
162     if (!g_on)
163         return;
164
165     if (connector_type != pFpsDebug->connector_type)
166     {
167         pFpsDebug->connector_type = connector_type;
168         pFpsDebug->cid = _get_connector_id (connector_type);
169     }
170
171     pFpsDebug->tCur = GetTimeInMillis();
172     if (pFpsDebug->nPanCount && pFpsDebug->tStart + FPS_DEBUG_TIMER_INTERVAL <= pFpsDebug->tCur)
173         _fps_print_fps (pFpsDebug, FALSE);
174
175     if (pFpsDebug->nPanCount == 0)
176         pFpsDebug->tStart = pFpsDebug->tLast;
177
178     pFpsDebug->nPanCount++;
179     pFpsDebug->tLast = pFpsDebug->tCur;
180     pFpsDebug->fpsTimer = TimerSet (pFpsDebug->fpsTimer
181                                     , 0, FPS_DEBUG_TIMER_INTERVAL
182                                     , _fps_frame_timeout, pFpsDebug);
183 }
184
185 API void
186 xDbgLogFpsDebug (char *reply, int *len, int on)
187 {
188     if (!init_fpsdebug)
189     {
190         XDBG_REPLY ("fps_debug is not supported.\n");
191         return;
192     }
193
194     if (g_on != on)
195         g_on = on;
196 }
197