2 * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
20 /* Invisible Vector Library
21 * ported by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
22 * Understanding is not required. Only obedience.
23 * yes, this D port is GPLed.
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, version 3 of the License ONLY.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 module iv
.nanovega
.perf
/*is aliced*/;
41 import iv
.nanovega
.nanovega
;
43 public final class PerfGraph
{
55 enum FilterFadeoff
= 0.1f; // 10%
56 float curFilterValue
= 0;
58 enum HistorySize
= 100;
62 float[HistorySize
] values
;
67 this (const(char)[] aname
, Style astyle
=Style
.FPS
, string afontname
="sans") {
70 if (aname
.length
> name
.length
) aname
= aname
[0..name
.length
];
72 if (aname
.length
) name
[0..aname
.length
] = aname
[];
76 @property Style
style () const pure nothrow @safe @nogc { pragma(inline
, true); return gstyle
; }
77 @property void style (Style v
) pure nothrow @safe @nogc { pragma(inline
, true); gstyle
= v
; }
79 // frameTime: in seconds
80 void update (float frameTime
) {
81 import std
.math
: isNaN
, isFinite
;
82 if (!isNaN(frameTime
)) {
83 if (!isFinite(frameTime
)) frameTime
= 0;
84 head
= (head
+1)%HistorySize
;
85 values
.ptr
[head
] = frameTime
;
86 curFilterValue
= FilterFadeoff
*frameTime
+(1.0f-FilterFadeoff
)*curFilterValue
;
90 float getAverage () const {
92 foreach (float v
; values
) avg
+= v
;
93 return avg
/cast(float)HistorySize
;
97 void render (NVGContext vg
, float x
, float y
) {
98 import core
.stdc
.stdio
: snprintf
;
100 if (vg
is null) return;
101 float avg
= getAverage();
104 vg
.rect(x
, y
, width
, height
);
105 vg
.fillColor(nvgRGBA(0, 0, 0, 128));
109 vg
.moveTo(x
, y
+height
);
110 final switch (gstyle
) {
112 foreach (int i
; 0..HistorySize
) {
113 float v
= 1.0f/(0.00001f+values
.ptr
[(head
+i
)%HistorySize
]);
114 if (v
> 80.0f) v
= 80.0f;
115 float vx
= x
+(cast(float)i
/(HistorySize
-1))*width
;
116 float vy
= y
+height
-((v
/80.0f)*height
);
121 foreach (int i
; 0..HistorySize
) {
122 float v
= values
.ptr
[(head
+i
)%HistorySize
]*1.0f;
123 if (v
> 100.0f) v
= 100.0f;
124 float vx
= x
+(cast(float)i
/(HistorySize
-1))*width
;
125 float vy
= y
+height
-((v
/100.0f)*height
);
130 foreach (int i
; 0..HistorySize
) {
131 float v
= values
.ptr
[(head
+i
)%HistorySize
]*1000.0f;
132 if (v
> 20.0f) v
= 20.0f;
133 float vx
= x
+(cast(float)i
/(HistorySize
-1))*width
;
134 float vy
= y
+height
-((v
/20.0f)*height
);
139 vg
.lineTo(x
+width
, y
+height
);
140 vg
.fillColor(nvgRGBA(255, 192, 0, 128));
143 vg
.fontFace(fontname
);
145 if (name
[0] != '\0') {
147 vg
.textAlign(NVGTextAlign(NVGTextAlign
.H
.Left
, NVGTextAlign
.V
.Top
));
148 vg
.fillColor(nvgRGBA(240, 240, 240, 192));
149 uint len
= 0; while (len
< name
.length
&& name
.ptr
[len
]) ++len
;
150 vg
.text(x
+3, y
+1, name
.ptr
[0..len
]);
154 final switch (gstyle
) {
157 vg
.textAlign(NVGTextAlign(NVGTextAlign
.H
.Right
, NVGTextAlign
.V
.Top
));
158 vg
.fillColor(nvgRGBA(240, 240, 240, 255));
159 auto len
= snprintf(str.ptr
, str.length
, "%.2f FPS (%.2f)", 1.0f/avg
, 1.0f/curFilterValue
);
160 vg
.text(x
+width
-3, y
+1, str.ptr
[0..len
]);
163 vg
.textAlign(NVGTextAlign(NVGTextAlign
.H
.Right
, NVGTextAlign
.V
.Bottom
));
164 vg
.fillColor(nvgRGBA(240, 240, 240, 160));
165 len
= snprintf(str.ptr
, str.length
, "%.2f ms", avg
*1000.0f);
166 vg
.text(x
+width
-3, y
+height
-1, str.ptr
[0..len
]);
170 vg
.textAlign(NVGTextAlign(NVGTextAlign
.H
.Right
, NVGTextAlign
.V
.Top
));
171 vg
.fillColor(nvgRGBA(240, 240, 240, 255));
172 auto len
= snprintf(str.ptr
, str.length
, "%.1f %%", avg
*1.0f);
173 vg
.text(x
+width
-3, y
+1, str.ptr
[0..len
]);
177 vg
.textAlign(NVGTextAlign(NVGTextAlign
.H
.Right
, NVGTextAlign
.V
.Top
));
178 vg
.fillColor(nvgRGBA(240, 240, 240, 255));
179 auto len
= snprintf(str.ptr
, str.length
, "%.2f ms", avg
*1000.0f);
180 vg
.text(x
+width
-3, y
+1, str.ptr
[0..len
]);