sq3: show SQLite error messages on stderr by default
[iv.d.git] / nanovega / perf.d
blob8a279e3411cfa120a7d62e00f1873ab3d110dcf2
1 /*
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*/;
38 private:
40 //import iv.alice;
41 import iv.nanovega.nanovega;
43 public final class PerfGraph {
44 public:
45 enum Style {
46 FPS,
47 MSec,
48 Percent,
51 enum width = 200;
52 enum height = 35;
54 private:
55 enum FilterFadeoff = 0.1f; // 10%
56 float curFilterValue = 0;
58 enum HistorySize = 100;
60 Style gstyle;
61 char[32] name;
62 float[HistorySize] values;
63 string fontname;
64 int head;
66 public:
67 this (const(char)[] aname, Style astyle=Style.FPS, string afontname="sans") {
68 gstyle = astyle;
69 values[] = 0;
70 if (aname.length > name.length) aname = aname[0..name.length];
71 name[] = 0;
72 if (aname.length) name[0..aname.length] = aname[];
73 fontname = afontname;
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 {
91 float avg = 0;
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();
103 vg.beginPath();
104 vg.rect(x, y, width, height);
105 vg.fillColor(nvgRGBA(0, 0, 0, 128));
106 vg.fill();
108 vg.beginPath();
109 vg.moveTo(x, y+height);
110 final switch (gstyle) {
111 case Style.FPS:
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);
117 vg.lineTo(vx, vy);
119 break;
120 case Style.Percent:
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);
126 vg.lineTo(vx, vy);
128 break;
129 case Style.MSec:
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);
135 vg.lineTo(vx, vy);
137 break;
139 vg.lineTo(x+width, y+height);
140 vg.fillColor(nvgRGBA(255, 192, 0, 128));
141 vg.fill();
143 vg.fontFace(fontname);
145 if (name[0] != '\0') {
146 vg.fontSize(14.0f);
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]);
153 char[64] str;
154 final switch (gstyle) {
155 case Style.FPS:
156 vg.fontSize(18.0f);
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]);
162 vg.fontSize(15.0f);
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]);
167 break;
168 case Style.Percent:
169 vg.fontSize(18.0f);
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]);
174 break;
175 case Style.MSec:
176 vg.fontSize(18.0f);
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]);
181 break;