DepthSliceUI.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. namespace UnityEditor.Performance.ProfileAnalyzer
  6. {
  7. [Serializable]
  8. public class DepthSliceUI
  9. {
  10. #if !UNITY_2019_1_OR_NEWER
  11. [NonSerialized]
  12. string[] m_DepthStrings;
  13. [NonSerialized]
  14. int[] m_DepthValues;
  15. [NonSerialized]
  16. string[] m_DepthStringsAuto;
  17. [NonSerialized]
  18. int[] m_DepthValuesAuto;
  19. [NonSerialized]
  20. int m_OldDepthRaw1;
  21. [NonSerialized]
  22. int m_OldDepthRaw2;
  23. [NonSerialized]
  24. string[] m_DepthStrings1;
  25. [NonSerialized]
  26. int[] m_DepthValues1;
  27. [NonSerialized]
  28. string[] m_DepthStrings2;
  29. [NonSerialized]
  30. int[] m_DepthValues2;
  31. #endif
  32. [SerializeField] int m_DepthFilter = ProfileAnalyzer.kDepthAll;
  33. public int depthFilter {get { return m_DepthFilter; }}
  34. [SerializeField] int m_DepthFilter1 = ProfileAnalyzer.kDepthAll;
  35. public int depthFilter1 {get { return m_DepthFilter1; }}
  36. [SerializeField] int m_DepthFilter2 = ProfileAnalyzer.kDepthAll;
  37. public int depthFilter2 {get { return m_DepthFilter2; }}
  38. [SerializeField] bool m_DepthFilterAuto = true;
  39. [SerializeField] int m_MostCommonDepthDiff = 0;
  40. int mostCommonDepthDiff
  41. {
  42. set
  43. {
  44. if (m_MostCommonDepthDiff != value)
  45. {
  46. m_MostCommonDepthDiff = value;
  47. UpdateAutoDepthTitleText();
  48. }
  49. }
  50. get
  51. {
  52. return m_MostCommonDepthDiff;
  53. }
  54. }
  55. void UpdateAutoDepthTitleText()
  56. {
  57. ProfileAnalyzerWindow.Styles.autoDepthTitle.text =
  58. string.Format(ProfileAnalyzerWindow.Styles.autoDepthTitleText, mostCommonDepthDiff);
  59. }
  60. Action<bool> m_UpdateActiveTabCallback = null;
  61. public DepthSliceUI(Action<bool> updateActiveTabCallback)
  62. {
  63. m_UpdateActiveTabCallback = updateActiveTabCallback;
  64. UpdateAutoDepthTitleText();
  65. }
  66. public void OnEnable(Action<bool> updateActiveTabCallback)
  67. {
  68. m_UpdateActiveTabCallback = updateActiveTabCallback;
  69. UpdateAutoDepthTitleText();
  70. }
  71. #if !UNITY_2019_1_OR_NEWER
  72. void SetDepthStringsSingle(int maxDepth, out string[] strings, out int[] values)
  73. {
  74. var count = maxDepth;
  75. List<string> depthStrings = new List<string>();
  76. List<int> depthValues = new List<int>();
  77. depthStrings.Add(DepthFilterToString(ProfileAnalyzer.kDepthAll));
  78. depthValues.Add(ProfileAnalyzer.kDepthAll);
  79. var startIndex = 1;
  80. var depthValue = 1;
  81. for (int depth = startIndex; depth <= count; depth++)
  82. {
  83. depthStrings.Add(DepthFilterToString(depth));
  84. depthValues.Add(depthValue++);
  85. }
  86. strings = depthStrings.ToArray();
  87. values = depthValues.ToArray();
  88. }
  89. void SetDepthStringsCompare(int maxDepth, out string[] strings, out int[] values, int maxDepthRight = ProfileAnalyzer.kDepthAll)
  90. {
  91. var locked = maxDepthRight != ProfileAnalyzer.kDepthAll;
  92. var count = locked ? Math.Max(maxDepth + Math.Max(0, mostCommonDepthDiff), maxDepthRight - Math.Min(0, mostCommonDepthDiff)) : maxDepth;
  93. List<string> depthStrings = new List<string>();
  94. List<int> depthValues = new List<int>();
  95. depthStrings.Add(DepthFilterToString(ProfileAnalyzer.kDepthAll));
  96. depthValues.Add(ProfileAnalyzer.kDepthAll);
  97. var leftIsMain = mostCommonDepthDiff < 0;
  98. var startIndex = 1;
  99. var depthValue = 1;
  100. if (maxDepth <= 0 && mostCommonDepthDiff < 0)
  101. {
  102. depthValue = 1;
  103. startIndex = -mostCommonDepthDiff + 1;
  104. }
  105. else if(maxDepthRight <= 0 && mostCommonDepthDiff > 0)
  106. {
  107. depthValue = 1;
  108. startIndex = mostCommonDepthDiff + 1;
  109. }
  110. for (int depth = startIndex; depth <= count; depth++)
  111. {
  112. if (locked)
  113. {
  114. var left = Mathf.Clamp(depth - Math.Max(0, mostCommonDepthDiff), 1, maxDepth);
  115. var right = Mathf.Clamp(depth - Math.Max(0, -mostCommonDepthDiff), 1, maxDepthRight);
  116. if (maxDepth <= 0)
  117. left = -1;
  118. if (maxDepthRight <= 0)
  119. right = -1;
  120. depthStrings.Add(DepthFilterToString(left, right, leftIsMain));
  121. }
  122. else
  123. depthStrings.Add(DepthFilterToString(depth));
  124. depthValues.Add(depthValue++);
  125. }
  126. strings = depthStrings.ToArray();
  127. values = depthValues.ToArray();
  128. }
  129. #endif
  130. #if UNITY_2019_1_OR_NEWER
  131. enum ViewType
  132. {
  133. Single,
  134. Left,
  135. Right,
  136. Locked,
  137. }
  138. void DrawDepthFilterDropdown(GUIContent title, bool enabled, ProfileDataView view, Action<int, int, int> callback,
  139. ViewType viewType, ProfileDataView profileSingleView, ProfileDataView profileLeftView, ProfileDataView profileRightView)
  140. {
  141. if(title !=null)
  142. EditorGUILayout.LabelField(title, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  143. int depthFilter = ProfileAnalyzer.kDepthAll;
  144. int depthFilterOther = ProfileAnalyzer.kDepthAll;
  145. var maxDepth = view.GetMaxDepth();
  146. var maxDepthLeft = ProfileAnalyzer.kDepthAll;
  147. var maxDepthRight = ProfileAnalyzer.kDepthAll;
  148. var oldDepthFilter = ProfileAnalyzer.kDepthAll;
  149. var oldDepthFilterOtherLocked = ProfileAnalyzer.kDepthAll;
  150. var depthDiff = mostCommonDepthDiff;
  151. GUIContent content;
  152. switch (viewType)
  153. {
  154. case ViewType.Single:
  155. oldDepthFilter = m_DepthFilter;
  156. depthFilter = m_DepthFilter =
  157. m_DepthFilter == ProfileAnalyzer.kDepthAll ?
  158. ProfileAnalyzer.kDepthAll :
  159. profileSingleView.ClampToValidDepthValue(m_DepthFilter);
  160. content = new GUIContent(DepthFilterToString(depthFilter));
  161. depthFilterOther = depthFilter;
  162. depthDiff = 0;
  163. break;
  164. case ViewType.Left:
  165. oldDepthFilter = m_DepthFilter1;
  166. depthFilter = m_DepthFilter1 =
  167. m_DepthFilter1 == ProfileAnalyzer.kDepthAll ?
  168. ProfileAnalyzer.kDepthAll :
  169. profileLeftView.ClampToValidDepthValue(m_DepthFilter1);
  170. content = new GUIContent(DepthFilterToString(depthFilter));
  171. depthFilterOther = depthFilter;
  172. break;
  173. case ViewType.Right:
  174. oldDepthFilter = m_DepthFilter2;
  175. depthFilter = m_DepthFilter2 = m_DepthFilter2 == ProfileAnalyzer.kDepthAll
  176. ? ProfileAnalyzer.kDepthAll
  177. : profileRightView.ClampToValidDepthValue(m_DepthFilter2);
  178. content = new GUIContent(DepthFilterToString(depthFilter));
  179. depthFilterOther = depthFilter;
  180. break;
  181. case ViewType.Locked:
  182. oldDepthFilter = m_DepthFilter1;
  183. oldDepthFilterOtherLocked = m_DepthFilter2;
  184. maxDepth = maxDepthLeft = profileLeftView.GetMaxDepth();
  185. maxDepthRight = profileRightView.GetMaxDepth();
  186. ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
  187. depthFilter = m_DepthFilter1;
  188. depthFilterOther = m_DepthFilter2;
  189. content = new GUIContent(DepthFilterToString(m_DepthFilter1, m_DepthFilter2, mostCommonDepthDiff < 0));
  190. break;
  191. default:
  192. throw new NotImplementedException();
  193. }
  194. var lastEnabled = GUI.enabled;
  195. GUI.enabled = enabled;
  196. var rect = GUILayoutUtility.GetRect(content, EditorStyles.popup, GUILayout.MinWidth(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  197. if (GUI.Button(rect, content, EditorStyles.popup))
  198. {
  199. var dropdown = new DepthSliceDropdown(maxDepth, depthFilter, depthFilterOther, (slice, left, right) =>
  200. {
  201. if (slice != depthFilter || (viewType == ViewType.Locked && (left != m_DepthFilter1 || right != m_DepthFilter2)))
  202. {
  203. callback(slice, left, right);
  204. UpdateDepthFilters(viewType == ViewType.Single, profileSingleView, profileLeftView, profileRightView);
  205. m_UpdateActiveTabCallback(true);
  206. }
  207. }, depthDiff, maxDepthRight);
  208. dropdown.Show(rect);
  209. EditorGUIUtility.ExitGUI();
  210. }
  211. else
  212. {
  213. // The depths can change because the data changed, not just because the user selected a different option in the dropdown
  214. // in that case, the depth filters need to perform a refresh
  215. if(oldDepthFilter != depthFilter || viewType == ViewType.Locked && oldDepthFilterOtherLocked != depthFilterOther)
  216. {
  217. UpdateDepthFilters(viewType == ViewType.Single, profileSingleView, profileLeftView, profileRightView);
  218. m_UpdateActiveTabCallback(true);
  219. }
  220. }
  221. GUI.enabled = lastEnabled;
  222. }
  223. #endif
  224. int CalcSliceMenuEntryIndex(int filterDepthLeft, int filterDepthRight, int leftMax, int rightMax)
  225. {
  226. return mostCommonDepthDiff > 0 ?
  227. filterDepthRight + Math.Max(0, filterDepthLeft - rightMax + (rightMax > 0 ? mostCommonDepthDiff : filterDepthLeft > 0 ? 1 : 0)) :
  228. filterDepthLeft + Math.Max(0, filterDepthRight - leftMax - (leftMax > 0 ? mostCommonDepthDiff : filterDepthRight > 0 ? -1 :0));
  229. }
  230. void CalcAutoSlicesFromMenuEntryIndex(int depthSlcieMenuEntryIndex, ref int filterDepthLeft, ref int filterDepthRight, int leftMax, int rightMax)
  231. {
  232. if (mostCommonDepthDiff > 0)
  233. {
  234. filterDepthRight = Mathf.Clamp(depthSlcieMenuEntryIndex, 1, rightMax);
  235. filterDepthLeft = Mathf.Clamp(depthSlcieMenuEntryIndex - (rightMax > 0 ? mostCommonDepthDiff : 0), 1, leftMax);
  236. }
  237. else
  238. {
  239. filterDepthLeft = Mathf.Clamp(depthSlcieMenuEntryIndex, 1, leftMax);
  240. filterDepthRight = Mathf.Clamp(depthSlcieMenuEntryIndex + (leftMax > 0 ? mostCommonDepthDiff : 0), 1, rightMax);
  241. }
  242. // if a side has no depth, only allow All
  243. if (leftMax <= 0)
  244. filterDepthLeft = -1;
  245. if (rightMax <= 0)
  246. filterDepthRight = -1;
  247. }
  248. void ClampDepthFilterForAutoRespectingDiff(ref int filterDepthLeft, ref int filterDepthRight, ProfileDataView profileLeftView, ProfileDataView profileRightView)
  249. {
  250. if (filterDepthLeft == ProfileAnalyzer.kDepthAll && filterDepthRight == ProfileAnalyzer.kDepthAll)
  251. {
  252. // nothing to do here, keep showing all
  253. return;
  254. }
  255. var leftMax = profileLeftView.GetMaxDepth();
  256. var rightMax = profileRightView.GetMaxDepth();
  257. var sliceMenuEntryIndex = CalcSliceMenuEntryIndex(filterDepthLeft, filterDepthRight, leftMax, rightMax);
  258. CalcAutoSlicesFromMenuEntryIndex(sliceMenuEntryIndex, ref filterDepthLeft, ref filterDepthRight, leftMax, rightMax);
  259. }
  260. internal void DrawDepthFilter(bool isAnalysisRunning, bool singleView,
  261. ProfileDataView profileSingleView, ProfileDataView profileLeftView, ProfileDataView profileRightView)
  262. {
  263. #if !UNITY_2019_1_OR_NEWER
  264. bool triggerRefresh = false;
  265. if (!isAnalysisRunning)
  266. {
  267. if (singleView)
  268. {
  269. var maxDepth = profileSingleView.GetMaxDepth();
  270. if (m_DepthStrings == null || maxDepth != m_OldDepthRaw1)
  271. {
  272. SetDepthStringsSingle(maxDepth, out m_DepthStrings, out m_DepthValues);
  273. m_OldDepthRaw1 = maxDepth;
  274. triggerRefresh = true;
  275. }
  276. }
  277. else
  278. {
  279. if (m_DepthFilterAuto)
  280. {
  281. var maxLeftRaw = profileLeftView.GetMaxDepth();
  282. var maxRightRaw = profileRightView.GetMaxDepth();
  283. if (m_DepthStringsAuto == null ||
  284. m_OldDepthRaw1 != maxLeftRaw || m_OldDepthRaw2 != maxRightRaw)
  285. {
  286. SetDepthStringsCompare(maxLeftRaw, out m_DepthStringsAuto, out m_DepthValuesAuto, maxRightRaw);
  287. m_OldDepthRaw1 = maxLeftRaw;
  288. m_OldDepthRaw2 = maxRightRaw;
  289. triggerRefresh = true;
  290. }
  291. }
  292. else
  293. {
  294. var maxDepthLeft = profileLeftView.GetMaxDepth();
  295. if (m_DepthStrings1 == null || m_OldDepthRaw1 != maxDepthLeft)
  296. {
  297. SetDepthStringsSingle(maxDepthLeft, out m_DepthStrings1, out m_DepthValues1);
  298. m_OldDepthRaw1 = maxDepthLeft;
  299. triggerRefresh = true;
  300. }
  301. var maxDepthRight = profileRightView.GetMaxDepth();
  302. if (m_DepthStrings2 == null || m_OldDepthRaw2 != maxDepthRight)
  303. {
  304. SetDepthStringsSingle(maxDepthRight, out m_DepthStrings2, out m_DepthValues2);
  305. m_OldDepthRaw2 = maxDepthRight;
  306. triggerRefresh = true;
  307. }
  308. }
  309. }
  310. }
  311. #endif
  312. bool lastEnabled = GUI.enabled;
  313. bool enabled = !isAnalysisRunning;
  314. EditorGUILayout.BeginHorizontal();
  315. if (singleView)
  316. {
  317. EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.depthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsLeftLabelWidth));
  318. #if UNITY_2019_1_OR_NEWER
  319. DrawDepthFilterDropdown(null, enabled,
  320. profileSingleView, (primary, left, right) => m_DepthFilter = primary,
  321. ViewType.Single, profileSingleView, profileLeftView, profileRightView);
  322. #else
  323. if (m_DepthStrings != null)
  324. {
  325. var lastDepthFilter = m_DepthFilter;
  326. m_DepthFilter = m_DepthFilter == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileSingleView.ClampToValidDepthValue(m_DepthFilter);
  327. GUI.enabled = enabled;
  328. m_DepthFilter = EditorGUILayout.IntPopup(m_DepthFilter, m_DepthStrings, m_DepthValues, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  329. GUI.enabled = lastEnabled;
  330. if (m_DepthFilter != lastDepthFilter)
  331. triggerRefresh = true;
  332. }
  333. #endif
  334. }
  335. else
  336. {
  337. EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.depthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsLeftLabelWidth));
  338. if (m_DepthFilterAuto)
  339. {
  340. #if UNITY_2019_1_OR_NEWER
  341. DrawDepthFilterDropdown(null, enabled, profileLeftView, (primary, left, right) =>
  342. {
  343. m_DepthFilter1 = left;
  344. m_DepthFilter2 = right;
  345. ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
  346. },
  347. ViewType.Locked, profileSingleView, profileLeftView, profileRightView);
  348. #else
  349. if (m_DepthStringsAuto != null)
  350. {
  351. var leftMax = profileLeftView.GetMaxDepth();
  352. var rightMax = profileRightView.GetMaxDepth();
  353. var lastDepthFilterDropdownIndex = CalcSliceMenuEntryIndex(m_DepthFilter1, m_DepthFilter2, leftMax, rightMax);
  354. ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2,
  355. profileLeftView, profileRightView);
  356. var layoutOptionWidth = ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth;
  357. if(m_DepthFilter1 != m_DepthFilter2)
  358. layoutOptionWidth = ProfileAnalyzerWindow.LayoutSize.FilterOptionsLockedEnumWidth;
  359. GUI.enabled = enabled;
  360. var depthFilterDropdownIndex = Mathf.Clamp(lastDepthFilterDropdownIndex, -1, m_DepthStringsAuto.Length - 1);
  361. depthFilterDropdownIndex = EditorGUILayout.IntPopup(depthFilterDropdownIndex, m_DepthStringsAuto, m_DepthValuesAuto, GUILayout.Width(layoutOptionWidth));
  362. GUI.enabled = lastEnabled;
  363. if (depthFilterDropdownIndex != lastDepthFilterDropdownIndex)
  364. {
  365. if(depthFilterDropdownIndex == ProfileAnalyzer.kDepthAll)
  366. m_DepthFilter1 = m_DepthFilter2 = ProfileAnalyzer.kDepthAll;
  367. else
  368. CalcAutoSlicesFromMenuEntryIndex(depthFilterDropdownIndex, ref m_DepthFilter1, ref m_DepthFilter2, leftMax, rightMax);
  369. ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
  370. triggerRefresh = true;
  371. }
  372. }
  373. #endif
  374. }
  375. else
  376. {
  377. #if UNITY_2019_1_OR_NEWER
  378. DrawDepthFilterDropdown(ProfileAnalyzerWindow.Styles.leftDepthTitle, enabled, profileLeftView,
  379. (primary, left, right) => m_DepthFilter1 = primary,
  380. ViewType.Left, profileSingleView, profileLeftView, profileRightView);
  381. #else
  382. if (m_DepthStrings1 != null)
  383. {
  384. EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.leftDepthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  385. int lastDepthFilter1 = m_DepthFilter1;
  386. m_DepthFilter1 = m_DepthFilter1 == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileLeftView.ClampToValidDepthValue(m_DepthFilter1);
  387. GUI.enabled = enabled;
  388. m_DepthFilter1 = EditorGUILayout.IntPopup(m_DepthFilter1, m_DepthStrings1, m_DepthValues1, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  389. GUI.enabled = lastEnabled;
  390. if (m_DepthFilter1 != lastDepthFilter1)
  391. triggerRefresh = true;
  392. }
  393. #endif
  394. #if UNITY_2019_1_OR_NEWER
  395. DrawDepthFilterDropdown(ProfileAnalyzerWindow.Styles.rightDepthTitle, enabled && !m_DepthFilterAuto, profileRightView,
  396. (primary, left, right) => m_DepthFilter2 = primary,
  397. ViewType.Right, profileSingleView, profileLeftView, profileRightView);
  398. #else
  399. if (m_DepthStrings2 != null)
  400. {
  401. EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.rightDepthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  402. int lastDepthFilter2 = m_DepthFilter2;
  403. m_DepthFilter2 = m_DepthFilter2 == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileRightView.ClampToValidDepthValue(m_DepthFilter2);
  404. GUI.enabled = enabled && !m_DepthFilterAuto;
  405. m_DepthFilter2 = EditorGUILayout.IntPopup(m_DepthFilter2, m_DepthStrings2, m_DepthValues2, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
  406. GUI.enabled = lastEnabled;
  407. if (m_DepthFilter2 != lastDepthFilter2)
  408. triggerRefresh = true;
  409. }
  410. #endif
  411. }
  412. bool lastDepthFilterLock = m_DepthFilterAuto;
  413. GUI.enabled = enabled;
  414. m_DepthFilterAuto = EditorGUILayout.ToggleLeft(ProfileAnalyzerWindow.Styles.autoDepthTitle, m_DepthFilterAuto);
  415. GUI.enabled = lastEnabled;
  416. if (m_DepthFilterAuto != lastDepthFilterLock)
  417. {
  418. if (UpdateDepthFilters(singleView, profileSingleView, profileLeftView, profileRightView))
  419. m_UpdateActiveTabCallback(true);
  420. #if !UNITY_2019_1_OR_NEWER
  421. m_DepthStringsAuto = null;
  422. m_DepthStrings1 = null;
  423. m_DepthStrings2 = null;
  424. #endif
  425. }
  426. }
  427. GUILayout.FlexibleSpace();
  428. EditorGUILayout.EndHorizontal();
  429. #if !UNITY_2019_1_OR_NEWER
  430. if (triggerRefresh)
  431. {
  432. UpdateDepthFilters(singleView, profileSingleView, profileLeftView, profileRightView);
  433. m_UpdateActiveTabCallback(true);
  434. }
  435. #endif
  436. }
  437. internal bool UpdateDepthFilters(bool singleView, ProfileDataView profileSingleView, ProfileDataView profileLeftView, ProfileDataView profileRightView)
  438. {
  439. bool changed = false;
  440. if (!singleView)
  441. {
  442. // First respect the auto flag
  443. if (UpdateAutoDepthFilter(profileLeftView, profileRightView))
  444. changed = true;
  445. // Make sure Single matches the updated comparison view
  446. if (profileLeftView.path == profileSingleView.path)
  447. {
  448. // Use same filter on single view if its the same file
  449. if (m_DepthFilter != m_DepthFilter1)
  450. {
  451. m_DepthFilter = m_DepthFilter1;
  452. changed = true;
  453. }
  454. }
  455. if (profileRightView.path == profileSingleView.path)
  456. {
  457. // Use same filter on single view if its the same file
  458. if (m_DepthFilter != m_DepthFilter2)
  459. {
  460. m_DepthFilter = m_DepthFilter2;
  461. changed = true;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. // Make sure comparisons match updated single view
  468. if (profileLeftView.path == profileSingleView.path)
  469. {
  470. // Use same filter on comparison left view if its the same file
  471. if (m_DepthFilter1 != m_DepthFilter)
  472. {
  473. m_DepthFilter1 = m_DepthFilter;
  474. changed = true;
  475. }
  476. if (m_DepthFilterAuto)
  477. {
  478. var newDepthFilter2 = m_DepthFilter;
  479. ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref newDepthFilter2, profileLeftView, profileRightView);
  480. if (m_DepthFilter2 != newDepthFilter2)
  481. {
  482. m_DepthFilter2 = newDepthFilter2;
  483. changed = true;
  484. }
  485. if (UpdateAutoDepthFilter(profileLeftView, profileRightView))
  486. changed = true;
  487. }
  488. if (UpdateAutoDepthFilter(profileLeftView, profileRightView))
  489. changed = true;
  490. }
  491. if (profileRightView.path == profileSingleView.path)
  492. {
  493. // Use same filter on comparison right view if its the same file
  494. if (m_DepthFilter2 != m_DepthFilter)
  495. {
  496. m_DepthFilter2 = m_DepthFilter;
  497. changed = true;
  498. }
  499. if (m_DepthFilterAuto)
  500. {
  501. var newDepthFilter1 = m_DepthFilter;
  502. ClampDepthFilterForAutoRespectingDiff(ref newDepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
  503. if (m_DepthFilter1 != newDepthFilter1)
  504. {
  505. m_DepthFilter1 = newDepthFilter1;
  506. changed = true;
  507. }
  508. if (UpdateAutoDepthFilter(profileLeftView, profileRightView))
  509. changed = true;
  510. }
  511. }
  512. }
  513. return changed;
  514. }
  515. int CalculateDepthDifference(ProfileAnalysis leftAnalysis, ProfileAnalysis rightAnalysis, List<MarkerPairing> pairings)
  516. {
  517. if (pairings.Count <= 0)
  518. {
  519. mostCommonDepthDiff = 0;
  520. return 0;
  521. }
  522. var leftMarkers = leftAnalysis.GetMarkers();
  523. var rightMarkers = rightAnalysis.GetMarkers();
  524. int totalCount = 0;
  525. Dictionary<int, int> depthDifferences = new Dictionary<int, int>();
  526. foreach (var pairing in pairings)
  527. {
  528. if (pairing.leftIndex >= 0 && pairing.rightIndex >= 0)
  529. {
  530. MarkerData leftMarker = leftMarkers[pairing.leftIndex];
  531. MarkerData rightMarker = rightMarkers[pairing.rightIndex];
  532. int markerDepthDiff = rightMarker.minDepth - leftMarker.minDepth;
  533. int value = 0;
  534. depthDifferences.TryGetValue(markerDepthDiff, out value);
  535. depthDifferences[markerDepthDiff] = value + 1;
  536. totalCount += 1;
  537. }
  538. }
  539. var newDepthDiff = 0;
  540. // Find most common depth difference
  541. int maxCount = 0;
  542. foreach (var diff in depthDifferences.Keys)
  543. {
  544. if (depthDifferences[diff] > maxCount)
  545. {
  546. maxCount = depthDifferences[diff];
  547. newDepthDiff = diff;
  548. }
  549. }
  550. return mostCommonDepthDiff = newDepthDiff;
  551. }
  552. bool UpdateAutoDepthFilter(ProfileDataView profileLeftView, ProfileDataView profileRightView)
  553. {
  554. if (m_DepthFilterAuto)
  555. {
  556. var newDepthFilter1 = m_DepthFilter1;
  557. var newDepthFilter2 = m_DepthFilter2;
  558. ClampDepthFilterForAutoRespectingDiff(ref newDepthFilter1, ref newDepthFilter2, profileLeftView, profileRightView);
  559. if (m_DepthFilter1 != newDepthFilter1)
  560. {
  561. m_DepthFilter1 = newDepthFilter1;
  562. return true;
  563. }
  564. if (m_DepthFilter2 != newDepthFilter2)
  565. {
  566. m_DepthFilter2 = newDepthFilter2;
  567. return true;
  568. }
  569. }
  570. return false;
  571. }
  572. internal bool UpdateDepthForCompareSync(ProfileAnalysis leftAnalysis, ProfileAnalysis rightAnalysis, List<MarkerPairing> pairings, ProfileDataView profileLeftView, ProfileDataView profileRightView)
  573. {
  574. int originalDepthDiff = mostCommonDepthDiff;
  575. int newDepthDiff = CalculateDepthDifference(leftAnalysis, rightAnalysis, pairings);
  576. if (newDepthDiff != originalDepthDiff)
  577. {
  578. UpdateAutoDepthFilter(profileLeftView, profileRightView);
  579. return true;
  580. }
  581. return false;
  582. }
  583. internal GUIContent GetUIInfo(bool compare)
  584. {
  585. GUIContent info;
  586. if (compare && m_DepthFilter1 == ProfileAnalyzer.kDepthAll && m_DepthFilter2 == ProfileAnalyzer.kDepthAll ||
  587. !compare && depthFilter == ProfileAnalyzer.kDepthAll)
  588. {
  589. info = new GUIContent("(All depths)", string.Format("{0}\n\nSet depth 1 to get an overview of the frame", ProfileAnalyzerWindow.Styles.medianFrameTooltip));
  590. }
  591. else
  592. {
  593. if (compare && depthFilter1 != depthFilter2)
  594. {
  595. if (m_DepthFilter1 == ProfileAnalyzer.kDepthAll)
  596. info = new GUIContent(string.Format("(Filtered to 'all' depths in the first data set, and depth '{0}' in the second)", m_DepthFilter2), ProfileAnalyzerWindow.Styles.medianFrameTooltip);
  597. else if (m_DepthFilter2 == ProfileAnalyzer.kDepthAll)
  598. info = new GUIContent(string.Format("(Filtered to depth '{0}' in the first data set, and 'all' depths in the second)", m_DepthFilter1), ProfileAnalyzerWindow.Styles.medianFrameTooltip);
  599. else
  600. info = new GUIContent(string.Format("(Filtered to depth '{0}' in the first data set, and depth '{1}' in the second)", m_DepthFilter1, depthFilter2), ProfileAnalyzerWindow.Styles.medianFrameTooltip);
  601. }
  602. else
  603. info = new GUIContent(string.Format("(Filtered to depth '{0}' only)", compare ? m_DepthFilter1 : depthFilter), ProfileAnalyzerWindow.Styles.medianFrameTooltip);
  604. }
  605. return info;
  606. }
  607. public static string DepthFilterToString(int depthFilter)
  608. {
  609. return depthFilter == ProfileAnalyzer.kDepthAll ? "All" : depthFilter.ToString();
  610. }
  611. public static string DepthFilterToString(int depthSliceLeft, int depthSliceRight, bool leftIsMain)
  612. {
  613. if(depthSliceLeft != depthSliceRight)
  614. {
  615. if (leftIsMain)
  616. return string.Format("{0} ({1}{2})", DepthFilterToString(depthSliceLeft), ProfileAnalyzerWindow.Styles.rightDepthTitle.text, DepthFilterToString(depthSliceRight));
  617. else
  618. return string.Format("{0} ({1}{2})", DepthFilterToString(depthSliceRight), ProfileAnalyzerWindow.Styles.leftDepthTitle.text, DepthFilterToString(depthSliceLeft));
  619. }
  620. return DepthFilterToString(depthSliceLeft);
  621. }
  622. }
  623. }