GwenProfileWindow.cpp 8.2 KB

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