1 #include "GwenProfileWindow.h"
2 #include "gwenUserInterface.h"
3 #include "gwenInternalData.h"
4 #include "LinearMath/btQuickprof.h"
5 
6 #ifndef BT_NO_PROFILE
7 
8 class MyProfileWindow : public Gwen::Controls::WindowControl
9 {
10 	//		Gwen::Controls::TabControl*	m_TabControl;
11 	//Gwen::Controls::ListBox*	m_TextOutput;
12 	unsigned int m_iFrames;
13 	float m_fLastSecond;
14 
15 	Gwen::Controls::TreeNode* m_node;
16 	Gwen::Controls::TreeControl* m_ctrl;
17 
18 protected:
onButtonA(Gwen::Controls::Base * pControl)19 	void onButtonA(Gwen::Controls::Base* pControl)
20 	{
21 		//		OpenTissue::glut::toggleIdle();
22 	}
23 
SliderMoved(Gwen::Controls::Base * pControl)24 	void SliderMoved(Gwen::Controls::Base* pControl)
25 	{
26 		//	Gwen::Controls::Slider* pSlider = (Gwen::Controls::Slider*)pControl;
27 		//this->m_app->scaleYoungModulus(pSlider->GetValue());
28 		//	printf("Slider Value: %.2f", pSlider->GetValue() );
29 	}
30 
OnCheckChangedStiffnessWarping(Gwen::Controls::Base * pControl)31 	void OnCheckChangedStiffnessWarping(Gwen::Controls::Base* pControl)
32 	{
33 		//	Gwen::Controls::CheckBox* labeled = (Gwen::Controls::CheckBox* )pControl;
34 		//		bool checked = labeled->IsChecked();
35 		//m_app->m_stiffness_warp_on  = checked;
36 	}
37 
38 public:
39 	CProfileIterator* profIter;
40 
41 	class MyMenuItems3* m_menuItems;
MyProfileWindow(Gwen::Controls::Base * pParent)42 	MyProfileWindow(Gwen::Controls::Base* pParent)
43 		: Gwen::Controls::WindowControl(pParent),
44 		  profIter(0)
45 	{
46 		SetTitle(L"Time Profiler");
47 
48 		SetSize(450, 450);
49 		this->SetPos(10, 400);
50 
51 		//		this->Dock( Gwen::Pos::Bottom);
52 
53 		{
54 			m_ctrl = new Gwen::Controls::TreeControl(this);
55 			m_node = m_ctrl->AddNode(L"Total Parent Time");
56 
57 			//Gwen::Controls::TreeNode* pNode = ctrl->AddNode( L"Node Two" );
58 			//pNode->AddNode( L"Node Two Inside" );
59 			//pNode->AddNode( L"Eyes" );
60 			//pNode->AddNode( L"Brown" )->AddNode( L"Node Two Inside" )->AddNode( L"Eyes" )->AddNode( L"Brown" );
61 			//Gwen::Controls::TreeNode* node = ctrl->AddNode( L"Node Three" );
62 
63 			//m_ctrl->Dock(Gwen::Pos::Bottom);
64 
65 			m_ctrl->ExpandAll();
66 			m_ctrl->SetKeyboardInputEnabled(true);
67 			m_ctrl->SetBounds(this->GetInnerBounds().x, this->GetInnerBounds().y, this->GetInnerBounds().w, this->GetInnerBounds().h);
68 		}
69 	}
70 
~MyProfileWindow()71 	virtual ~MyProfileWindow()
72 	{
73 		delete m_node;
74 		delete m_ctrl;
75 	}
76 
dumpRecursive(CProfileIterator * profileIterator,Gwen::Controls::TreeNode * parentNode)77 	float dumpRecursive(CProfileIterator* profileIterator, Gwen::Controls::TreeNode* parentNode)
78 	{
79 		profileIterator->First();
80 		if (profileIterator->Is_Done())
81 			return 0.f;
82 
83 		float accumulated_time = 0, parent_time = profileIterator->Is_Root() ? CProfileManager::Get_Time_Since_Reset() : profileIterator->Get_Current_Parent_Total_Time();
84 		int i;
85 		int frames_since_reset = CProfileManager::Get_Frame_Count_Since_Reset();
86 		if (0 == frames_since_reset)
87 			return 0.f;
88 
89 		//printf("Profiling: %s (total running time: %.3f ms) ---\n",	profileIterator->Get_Current_Parent_Name(), parent_time );
90 		float totalTime = 0.f;
91 
92 		int numChildren = 0;
93 		Gwen::UnicodeString txt;
94 		std::vector<Gwen::Controls::TreeNode*> nodes;
95 
96 		for (i = 0; !profileIterator->Is_Done(); i++, profileIterator->Next())
97 		{
98 			numChildren++;
99 			float current_total_time = profileIterator->Get_Current_Total_Time();
100 			accumulated_time += current_total_time;
101 			double fraction = parent_time > SIMD_EPSILON ? (current_total_time / parent_time) * 100 : 0.f;
102 
103 			Gwen::String name(profileIterator->Get_Current_Name());
104 #ifdef _WIN32
105 			Gwen::UnicodeString uname = Gwen::Utility::StringToUnicode(name);
106 
107 			txt = Gwen::Utility::Format(L"%s (%.2f %%) :: %.3f ms / frame (%d calls)", uname.c_str(), fraction, (current_total_time / (double)frames_since_reset), profileIterator->Get_Current_Total_Calls());
108 
109 #else
110 			txt = Gwen::Utility::Format(L"%s (%.2f %%) :: %.3f ms / frame (%d calls)", name.c_str(), fraction, (current_total_time / (double)frames_since_reset), profileIterator->Get_Current_Total_Calls());
111 
112 #endif
113 
114 			Gwen::Controls::TreeNode* childNode = (Gwen::Controls::TreeNode*)profileIterator->Get_Current_UserPointer();
115 			if (!childNode)
116 			{
117 				childNode = parentNode->AddNode(L"");
118 				profileIterator->Set_Current_UserPointer(childNode);
119 			}
120 			childNode->SetText(txt);
121 			nodes.push_back(childNode);
122 
123 			totalTime += current_total_time;
124 			//recurse into children
125 		}
126 
127 		for (i = 0; i < numChildren; i++)
128 		{
129 			profileIterator->Enter_Child(i);
130 			Gwen::Controls::TreeNode* curNode = nodes[i];
131 
132 			dumpRecursive(profileIterator, curNode);
133 
134 			profileIterator->Enter_Parent();
135 		}
136 		return accumulated_time;
137 	}
138 
UpdateText(CProfileIterator * profileIterator,bool idle)139 	void UpdateText(CProfileIterator* profileIterator, bool idle)
140 	{
141 		//	static bool update=true;
142 
143 		m_ctrl->SetBounds(0, 0, this->GetInnerBounds().w, this->GetInnerBounds().h);
144 
145 		//		if (!update)
146 		//			return;
147 		//	update=false;
148 
149 		static int test = 1;
150 		test++;
151 
152 		static double time_since_reset = 0.f;
153 		if (!idle)
154 		{
155 			time_since_reset = CProfileManager::Get_Time_Since_Reset();
156 		}
157 
158 		//Gwen::UnicodeString txt = Gwen::Utility::Format( L"FEM Settings  %i fps", test );
159 		{
160 			//recompute profiling data, and store profile strings
161 
162 			//   char blockTime[128];
163 
164 			// double totalTime = 0;
165 
166 			//  int frames_since_reset = CProfileManager::Get_Frame_Count_Since_Reset();
167 
168 			profileIterator->First();
169 
170 			double parent_time = profileIterator->Is_Root() ? time_since_reset : profileIterator->Get_Current_Parent_Total_Time();
171 
172 			//   Gwen::Controls::TreeNode* curParent = m_node;
173 
174 			double accumulated_time = dumpRecursive(profileIterator, m_node);
175 
176 			const char* name = profileIterator->Get_Current_Parent_Name();
177 #ifdef _WIN32
178 			Gwen::UnicodeString uname = Gwen::Utility::StringToUnicode(name);
179 			Gwen::UnicodeString txt = Gwen::Utility::Format(L"Profiling: %s total time: %.3f ms, unaccounted %.3f %% :: %.3f ms", uname.c_str(), parent_time,
180 															parent_time > SIMD_EPSILON ? ((parent_time - accumulated_time) / parent_time) * 100 : 0.f, parent_time - accumulated_time);
181 #else
182 			Gwen::UnicodeString txt = Gwen::Utility::Format(L"Profiling: %s total time: %.3f ms, unaccounted %.3f %% :: %.3f ms", name, parent_time,
183 															parent_time > SIMD_EPSILON ? ((parent_time - accumulated_time) / parent_time) * 100 : 0.f, parent_time - accumulated_time);
184 #endif
185 			//sprintf(blockTime,"--- Profiling: %s (total running time: %.3f ms) ---",	profileIterator->Get_Current_Parent_Name(), parent_time );
186 			//displayProfileString(xOffset,yStart,blockTime);
187 			m_node->SetText(txt);
188 
189 			//printf("%s (%.3f %%) :: %.3f ms\n", "Unaccounted:",);
190 		}
191 
192 		static int counter = 10;
193 		if (counter)
194 		{
195 			counter--;
196 			m_ctrl->ExpandAll();
197 		}
198 	}
PrintText(const Gwen::UnicodeString & str)199 	void PrintText(const Gwen::UnicodeString& str)
200 	{
201 	}
202 
Render(Gwen::Skin::Base * skin)203 	void Render(Gwen::Skin::Base* skin)
204 	{
205 		m_iFrames++;
206 
207 		if (m_fLastSecond < Gwen::Platform::GetTimeInSeconds())
208 		{
209 			SetTitle(Gwen::Utility::Format(L"Profiler  %i fps", m_iFrames));
210 
211 			m_fLastSecond = Gwen::Platform::GetTimeInSeconds() + 1.0f;
212 			m_iFrames = 0;
213 		}
214 
215 		Gwen::Controls::WindowControl::Render(skin);
216 	}
217 };
218 
219 class MyMenuItems3 : public Gwen::Controls::Base
220 {
221 public:
222 	class MyProfileWindow* m_profWindow;
MyMenuItems3()223 	MyMenuItems3() : Gwen::Controls::Base(0)
224 	{
225 	}
~MyMenuItems3()226 	virtual ~MyMenuItems3() {}
227 
MenuItemSelect(Gwen::Controls::Base * pControl)228 	void MenuItemSelect(Gwen::Controls::Base* pControl)
229 	{
230 		if (m_profWindow->Hidden())
231 		{
232 			m_profWindow->SetHidden(false);
233 		}
234 		else
235 		{
236 			m_profWindow->SetHidden(true);
237 		}
238 	}
239 };
240 
setupProfileWindow(GwenInternalData * data)241 MyProfileWindow* setupProfileWindow(GwenInternalData* data)
242 {
243 	MyMenuItems3* menuItems = new MyMenuItems3;
244 
245 	MyProfileWindow* profWindow = new MyProfileWindow(data->pCanvas);
246 	//profWindow->SetHidden(true);
247 
248 	profWindow->m_menuItems = menuItems;
249 	profWindow->profIter = CProfileManager::Get_Iterator();
250 	data->m_viewMenu->GetMenu()->AddItem(L"Profiler", menuItems, (Gwen::Event::Handler::Function)&MyMenuItems3::MenuItemSelect);
251 
252 	menuItems->m_profWindow = profWindow;
253 
254 	return profWindow;
255 }
256 
processProfileData(MyProfileWindow * profWindow,bool idle)257 void processProfileData(MyProfileWindow* profWindow, bool idle)
258 {
259 	if (profWindow)
260 	{
261 		if (profWindow->profIter)
262 		{
263 			profWindow->UpdateText(profWindow->profIter, idle);
264 		}
265 	}
266 }
267 
isProfileWindowVisible(MyProfileWindow * window)268 bool isProfileWindowVisible(MyProfileWindow* window)
269 {
270 	return !window->Hidden();
271 }
272 
profileWindowSetVisible(MyProfileWindow * window,bool visible)273 void profileWindowSetVisible(MyProfileWindow* window, bool visible)
274 {
275 	window->SetHidden(!visible);
276 }
destroyProfileWindow(MyProfileWindow * window)277 void destroyProfileWindow(MyProfileWindow* window)
278 {
279 	CProfileManager::Release_Iterator(window->profIter);
280 	delete window->m_menuItems;
281 	delete window;
282 	CProfileManager::CleanupMemory();
283 }
284 
285 #endif  //BT_NO_PROFILE
286