| 1 | /************************************************************************************ |
| 2 | |
| 3 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 4 | |
| 5 | Licensed under the Oculus SDK License Version 3.4.1 (the "License"); |
| 6 | you may not use the Oculus SDK except in compliance with the License, |
| 7 | which is provided at the time of installation or download, or which |
| 8 | otherwise accompanies this software in either electronic or hard copy form. |
| 9 | |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | https://developer.oculus.com/licenses/sdk-3.4.1 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | |
| 20 | ************************************************************************************/ |
| 21 | |
| 22 | using System.Collections.Generic; |
| 23 | using System; |
| 24 | using System.Reflection; |
| 25 | using UnityEngine; |
| 26 | using UnityEditor; |
| 27 | using System.Text; |
| 28 | |
| 29 | public class OVRSystemProfilerPanel : EditorWindow { |
| 30 | [MenuItem("Oculus/Tools/Oculus Profiler Panel")] |
| 31 | public static void ShowWindow() |
| 32 | { |
| 33 | EditorWindow.GetWindow(typeof(OVRSystemProfilerPanel), false, "Oculus Profiler"); |
| 34 | OVRPlugin.SendEvent("oculus_profiler_panel", "show_window"); |
| 35 | } |
| 36 | |
| 37 | bool showAndroidOptions = false; |
| 38 | OVRNetwork.OVRNetworkTcpClient tcpClient = new OVRNetwork.OVRNetworkTcpClient(); |
| 39 | int remoteListeningPort = OVRSystemPerfMetrics.TcpListeningPort; |
| 40 | |
| 41 | //OVRSystemPerfMetrics.PerfMetrics lastReceivedMetrics; |
| 42 | |
| 43 | const int maxMetricsFrames = 120; |
| 44 | const float metricsHistoryDuration = 1.0f; |
| 45 | |
| 46 | List<OVRSystemPerfMetrics.PerfMetrics> receivedMetricsList = new List<OVRSystemPerfMetrics.PerfMetrics>(); |
| 47 | bool pauseReceiveMetrics = false; |
| 48 | bool repaintRequested = false; |
| 49 | |
| 50 | const float labelWidth = 140.0f; |
| 51 | const float panelInnerRightPad = 6.0f; |
| 52 | const float progressBarPad = 5.0f; |
| 53 | const float progressBarHeight = 18.0f; |
| 54 | |
| 55 | string androidSdkRootPath; |
| 56 | OVRADBTool adbTool; |
| 57 | |
| 58 | // The actual window code goes here |
| 59 | void OnGUI() |
| 60 | { |
| 61 | showAndroidOptions = EditorGUILayout.Foldout(showAndroidOptions, "Android Tools"); |
| 62 | |
| 63 | if (showAndroidOptions) |
| 64 | { |
| 65 | GUILayout.BeginHorizontal(); |
| 66 | EditorGUILayout.LabelField("Android SDK root path: ", androidSdkRootPath); |
| 67 | GUILayout.EndHorizontal(); |
| 68 | |
| 69 | GUILayout.BeginHorizontal(); |
| 70 | if (GUILayout.Button("Start Server")) |
| 71 | { |
| 72 | if (adbTool == null) |
| 73 | { |
| 74 | adbTool = new OVRADBTool(androidSdkRootPath); |
| 75 | } |
| 76 | if (adbTool.isReady) |
| 77 | { |
| 78 | int exitCode = adbTool.StartServer(null); |
| 79 | EditorUtility.DisplayDialog("ADB StartServer", (exitCode == 0 ? "Success" : "Failure. ExitCode = " + exitCode.ToString()), "Ok"); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | EditorUtility.DisplayDialog("Can't locate ADBTool", adbTool.adbPath, "Ok"); |
| 84 | } |
| 85 | } |
| 86 | if (GUILayout.Button("Kill Server")) |
| 87 | { |
| 88 | if (adbTool == null) |
| 89 | { |
| 90 | adbTool = new OVRADBTool(androidSdkRootPath); |
| 91 | } |
| 92 | if (adbTool.isReady) |
| 93 | { |
| 94 | int exitCode = adbTool.KillServer(null); |
| 95 | EditorUtility.DisplayDialog("ADB KillServer", (exitCode == 0 ? "Success" : "Failure. ExitCode = " + exitCode.ToString()), "Ok"); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | EditorUtility.DisplayDialog("Can't locate ADBTool", adbTool.adbPath, "Ok"); |
| 100 | } |
| 101 | } |
| 102 | if (GUILayout.Button("Forward Port")) |
| 103 | { |
| 104 | if (adbTool == null) |
| 105 | { |
| 106 | adbTool = new OVRADBTool(androidSdkRootPath); |
| 107 | } |
| 108 | if (adbTool.isReady) |
| 109 | { |
| 110 | int exitCode = adbTool.ForwardPort(remoteListeningPort, null); |
| 111 | EditorUtility.DisplayDialog("ADB ForwardPort", (exitCode == 0 ? "Success" : "Failure. ExitCode = " + exitCode.ToString()), "Ok"); |
| 112 | OVRPlugin.SendEvent("device_metrics_profiler", (exitCode == 0 ? "adb_forward_success" : "adb_forward_failure")); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | EditorUtility.DisplayDialog("Can't locate ADBTool", adbTool.adbPath, "Ok"); |
| 117 | } |
| 118 | } |
| 119 | if (GUILayout.Button("Release Port")) |
| 120 | { |
| 121 | if (adbTool == null) |
| 122 | { |
| 123 | adbTool = new OVRADBTool(androidSdkRootPath); |
| 124 | } |
| 125 | if (adbTool.isReady) |
| 126 | { |
| 127 | int exitCode = adbTool.ReleasePort(remoteListeningPort, null); |
| 128 | EditorUtility.DisplayDialog("ADB ReleasePort", (exitCode == 0 ? "Success" : "Failure. ExitCode = " + exitCode.ToString()), "Ok"); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | EditorUtility.DisplayDialog("Can't locate ADBTool", adbTool.adbPath, "Ok"); |
| 133 | } |
| 134 | } |
| 135 | GUILayout.EndHorizontal(); |
| 136 | } |
| 137 | |
| 138 | EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); |
| 139 | |
| 140 | GUILayout.BeginHorizontal(); |
| 141 | remoteListeningPort = EditorGUILayout.DelayedIntField("Remote Port", remoteListeningPort); |
| 142 | |
| 143 | if (tcpClient.connectionState == OVRNetwork.OVRNetworkTcpClient.ConnectionState.Disconnected) |
| 144 | { |
| 145 | if (GUILayout.Button("Connect")) |
| 146 | { |
| 147 | ConnectPerfMetricsTcpServer(); |
| 148 | pauseReceiveMetrics = false; |
| 149 | OVRPlugin.SendEvent("device_metrics_profiler", "connect"); |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | if (tcpClient.connectionState == OVRNetwork.OVRNetworkTcpClient.ConnectionState.Connecting) |
| 155 | { |
| 156 | if (GUILayout.Button("Connecting ... Click again to Cancel")) |
| 157 | { |
| 158 | DisconnectPerfMetricsTcpServer(); |
| 159 | OVRPlugin.SendEvent("device_metrics_profiler", "cancel"); |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | if (GUILayout.Button("Disconnect")) |
| 165 | { |
| 166 | DisconnectPerfMetricsTcpServer(); |
| 167 | OVRPlugin.SendEvent("device_metrics_profiler", "disconnect"); |
| 168 | } |
| 169 | |
| 170 | if (GUILayout.Button(pauseReceiveMetrics ? "Continue" : "Pause")) |
| 171 | { |
| 172 | pauseReceiveMetrics = !pauseReceiveMetrics; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | GUILayout.EndHorizontal(); |
| 177 | |
| 178 | EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); |
| 179 | |
| 180 | lock (receivedMetricsList) |
| 181 | { |
| 182 | PresentIntProperty("Frame Count", "frameCount"); |
| 183 | PresentIntProperty("Dropped Frame Count", "compositorDroppedFrameCount"); |
| 184 | |
| 185 | float? avgFrameTime = GetAveragePerfValueFloat("deltaFrameTime"); |
| 186 | if (avgFrameTime.HasValue) |
| 187 | { |
| 188 | float fps = 1.0f / avgFrameTime.Value; |
| 189 | EditorGUILayout.BeginHorizontal(); |
| 190 | EditorGUILayout.LabelField("FPS", GUILayout.Width(labelWidth)); |
| 191 | EditorGUILayout.LabelField(string.Format("{0:F1}", fps)); |
| 192 | EditorGUILayout.EndHorizontal(); |
| 193 | } |
| 194 | |
| 195 | int? deviceCpuClockLevel = GetLatestPerfValueInt("deviceCpuClockLevel"); |
| 196 | int? deviceGpuClockLevel = GetLatestPerfValueInt("deviceGpuClockLevel"); |
| 197 | float? deviceCpuClockFrequencyInMHz = GetLatestPerfValueFloat("deviceCpuClockFrequencyInMHz"); |
| 198 | float? deviceGpuClockFrequencyInMHz = GetLatestPerfValueFloat("deviceGpuClockFrequencyInMHz"); |
| 199 | |
| 200 | if (deviceCpuClockLevel.HasValue || deviceCpuClockFrequencyInMHz.HasValue) |
| 201 | { |
| 202 | string cpuLabel; |
| 203 | string cpuText; |
| 204 | if (deviceCpuClockLevel.HasValue && deviceCpuClockFrequencyInMHz.HasValue) |
| 205 | { |
| 206 | cpuLabel = "CPU Level (Freq)"; |
| 207 | cpuText = string.Format("{0} ({1:F0} MHz)", deviceCpuClockLevel, deviceCpuClockFrequencyInMHz); |
| 208 | } |
| 209 | else if (deviceCpuClockLevel.HasValue) |
| 210 | { |
| 211 | cpuLabel = "CPU Level"; |
| 212 | cpuText = string.Format("{0}", deviceCpuClockLevel); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | cpuLabel = "CPU Frequency"; |
| 217 | cpuText = string.Format("{0:F0} MHz", deviceCpuClockFrequencyInMHz); |
| 218 | } |
| 219 | PresentText(cpuLabel, cpuText); |
| 220 | } |
| 221 | |
| 222 | if (deviceGpuClockLevel.HasValue || deviceGpuClockFrequencyInMHz.HasValue) |
| 223 | { |
| 224 | string cpuLabel; |
| 225 | string cpuText; |
| 226 | if (deviceGpuClockLevel.HasValue && deviceGpuClockFrequencyInMHz.HasValue) |
| 227 | { |
| 228 | cpuLabel = "GPU Level (Freq)"; |
| 229 | cpuText = string.Format("{0} ({1:F0} MHz)", deviceGpuClockLevel, deviceGpuClockFrequencyInMHz); |
| 230 | } |
| 231 | else if (deviceGpuClockLevel.HasValue) |
| 232 | { |
| 233 | cpuLabel = "GPU Level"; |
| 234 | cpuText = string.Format("{0}", deviceGpuClockLevel); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | cpuLabel = "GPU Frequency"; |
| 239 | cpuText = string.Format("{0:F0} MHz", deviceGpuClockFrequencyInMHz); |
| 240 | } |
| 241 | PresentText(cpuLabel, cpuText); |
| 242 | } |
| 243 | |
| 244 | PresentColumnTitles("Current", "Average", "Peak"); |
| 245 | |
| 246 | PresentFloatTimeInMs("Frame Time", "deltaFrameTime", 0.020f, true, true); |
| 247 | PresentFloatTimeInMs("App CPU Time", "appCpuTime", 0.020f, true, true); |
| 248 | PresentFloatTimeInMs("App GPU Time", "appGpuTime", 0.020f, true, true); |
| 249 | PresentFloatTimeInMs("Compositor CPU Time", "compositorCpuTime", 0.020f, true, true); |
| 250 | PresentFloatTimeInMs("Compositor GPU Time", "compositorGpuTime", 0.020f, true, true); |
| 251 | PresentFloatPercentage("CPU Util (Average)", "systemCpuUtilAveragePercentage", false, false); |
| 252 | PresentFloatPercentage("CPU Util (Worst Core)", "systemCpuUtilWorstPercentage", false, false); |
| 253 | PresentFloatPercentage("GPU Util", "systemGpuUtilPercentage", false, false); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void GetMetricsField(string propertyName, out FieldInfo baseFieldInfo, out FieldInfo validalityFieldInfo) |
| 258 | { |
| 259 | baseFieldInfo = typeof(OVRSystemPerfMetrics.PerfMetrics).GetField(propertyName); |
| 260 | validalityFieldInfo = typeof(OVRSystemPerfMetrics.PerfMetrics).GetField(propertyName + "_IsValid"); |
| 261 | } |
| 262 | |
| 263 | bool HasValidPerfMetrics(string propertyName) |
| 264 | { |
| 265 | FieldInfo baseFieldInfo, validalityFieldInfo; |
| 266 | GetMetricsField(propertyName, out baseFieldInfo, out validalityFieldInfo); |
| 267 | |
| 268 | if (baseFieldInfo == null || (validalityFieldInfo != null && validalityFieldInfo.FieldType != typeof(bool))) |
| 269 | { |
| 270 | Debug.LogWarning("[OVRSystemProfilerPanel] Unable to find property " + propertyName); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | if (validalityFieldInfo == null) |
| 275 | { |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | for (int i = receivedMetricsList.Count - 1; i >= 0; --i) |
| 280 | { |
| 281 | var metrics = receivedMetricsList[i]; |
| 282 | if (validalityFieldInfo != null && (bool)validalityFieldInfo.GetValue(metrics)) |
| 283 | { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | int? GetLatestPerfValueInt(string propertyName) |
| 292 | { |
| 293 | FieldInfo baseFieldInfo, validalityFieldInfo; |
| 294 | GetMetricsField(propertyName, out baseFieldInfo, out validalityFieldInfo); |
| 295 | |
| 296 | if (baseFieldInfo == null || baseFieldInfo.FieldType != typeof(int) || |
| 297 | (validalityFieldInfo != null && validalityFieldInfo.FieldType != typeof(bool))) |
| 298 | { |
| 299 | Debug.LogWarning("[OVRSystemProfilerPanel] GetLatestPerfValueInt(): Type mismatch"); |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | if (receivedMetricsList.Count == 0) |
| 304 | { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | for (int i = receivedMetricsList.Count - 1; i >= 0; --i) |
| 309 | { |
| 310 | var metrics = receivedMetricsList[i]; |
| 311 | if (validalityFieldInfo == null || (validalityFieldInfo != null && (bool)validalityFieldInfo.GetValue(metrics))) |
| 312 | { |
| 313 | return (int)baseFieldInfo.GetValue(metrics); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return null; |
| 318 | } |
| 319 | |
| 320 | float? GetLatestPerfValueFloat(string propertyName) |
| 321 | { |
| 322 | FieldInfo baseFieldInfo, validalityFieldInfo; |
| 323 | GetMetricsField(propertyName, out baseFieldInfo, out validalityFieldInfo); |
| 324 | |
| 325 | if (baseFieldInfo == null || baseFieldInfo.FieldType != typeof(float) || |
| 326 | (validalityFieldInfo != null && validalityFieldInfo.FieldType != typeof(bool))) |
| 327 | { |
| 328 | Debug.LogWarning("[OVRSystemProfilerPanel] GetLatestPerfValueFloat(): Type mismatch"); |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | if (receivedMetricsList.Count == 0) |
| 333 | { |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | for (int i = receivedMetricsList.Count - 1; i >= 0; --i) |
| 338 | { |
| 339 | var metrics = receivedMetricsList[i]; |
| 340 | if (validalityFieldInfo == null || (validalityFieldInfo != null && (bool)validalityFieldInfo.GetValue(metrics))) |
| 341 | { |
| 342 | return (float)baseFieldInfo.GetValue(metrics); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | float? GetAveragePerfValueFloat(string propertyName) |
| 350 | { |
| 351 | FieldInfo baseFieldInfo, validalityFieldInfo; |
| 352 | GetMetricsField(propertyName, out baseFieldInfo, out validalityFieldInfo); |
| 353 | |
| 354 | if (baseFieldInfo == null || baseFieldInfo.FieldType != typeof(float) || |
| 355 | (validalityFieldInfo != null && validalityFieldInfo.FieldType != typeof(bool))) |
| 356 | { |
| 357 | Debug.LogWarning("[OVRSystemProfilerPanel] GetAveragePerfValueFloat(): Type mismatch"); |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | int count = 0; |
| 362 | float sum = 0; |
| 363 | |
| 364 | OVRSystemPerfMetrics.PerfMetrics lastMetrics = null; |
| 365 | int metricsIndex; |
| 366 | for (metricsIndex = receivedMetricsList.Count - 1; metricsIndex >= 0; --metricsIndex) |
| 367 | { |
| 368 | var metrics = receivedMetricsList[metricsIndex]; |
| 369 | if (validalityFieldInfo != null && !(bool)validalityFieldInfo.GetValue(metrics)) |
| 370 | { |
| 371 | continue; |
| 372 | } |
| 373 | lastMetrics = metrics; |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | if (lastMetrics == null) |
| 378 | { |
| 379 | return null; |
| 380 | } |
| 381 | |
| 382 | for (; metricsIndex >=0; -- metricsIndex) |
| 383 | { |
| 384 | var metrics = receivedMetricsList[metricsIndex]; |
| 385 | if (metrics.frameTime < lastMetrics.frameTime - metricsHistoryDuration) |
| 386 | { |
| 387 | break; |
| 388 | } |
| 389 | if (validalityFieldInfo != null && !(bool)validalityFieldInfo.GetValue(metrics)) |
| 390 | { |
| 391 | continue; |
| 392 | } |
| 393 | sum += (float)baseFieldInfo.GetValue(metrics); |
| 394 | count++; |
| 395 | } |
| 396 | |
| 397 | if (count == 0) |
| 398 | { |
| 399 | return null; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | return sum / count; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | float? GetMaxPerfValueFloat(string propertyName) |
| 408 | { |
| 409 | FieldInfo baseFieldInfo, validalityFieldInfo; |
| 410 | GetMetricsField(propertyName, out baseFieldInfo, out validalityFieldInfo); |
| 411 | |
| 412 | if (baseFieldInfo == null || baseFieldInfo.FieldType != typeof(float) || |
| 413 | (validalityFieldInfo != null && validalityFieldInfo.FieldType != typeof(bool))) |
| 414 | { |
| 415 | Debug.LogWarning("[OVRSystemProfilerPanel] GetMaxPerfValueFloat(): Type mismatch"); |
| 416 | return null; |
| 417 | } |
| 418 | |
| 419 | OVRSystemPerfMetrics.PerfMetrics lastMetrics = null; |
| 420 | int metricsIndex; |
| 421 | for (metricsIndex = receivedMetricsList.Count - 1; metricsIndex >= 0; --metricsIndex) |
| 422 | { |
| 423 | var metrics = receivedMetricsList[metricsIndex]; |
| 424 | if (validalityFieldInfo != null && !(bool)validalityFieldInfo.GetValue(metrics)) |
| 425 | { |
| 426 | continue; |
| 427 | } |
| 428 | lastMetrics = metrics; |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | if (lastMetrics == null) |
| 433 | { |
| 434 | return null; |
| 435 | } |
| 436 | |
| 437 | float? result = null; |
| 438 | |
| 439 | for (; metricsIndex >= 0; --metricsIndex) |
| 440 | { |
| 441 | var metrics = receivedMetricsList[metricsIndex]; |
| 442 | if (metrics.frameTime < lastMetrics.frameTime - metricsHistoryDuration) |
| 443 | { |
| 444 | break; |
| 445 | } |
| 446 | if (validalityFieldInfo != null && !(bool)validalityFieldInfo.GetValue(metrics)) |
| 447 | { |
| 448 | continue; |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | float value = (float)baseFieldInfo.GetValue(metrics); |
| 453 | if (!result.HasValue || result.Value < value) |
| 454 | { |
| 455 | result = value; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return result; |
| 461 | } |
| 462 | |
| 463 | void PresentFloatPercentage(string label, string propertyName, bool displayAverage, bool displayMaximum) |
| 464 | { |
| 465 | float? lastValue = GetLatestPerfValueFloat(propertyName); |
| 466 | if (!lastValue.HasValue) |
| 467 | { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | EditorGUILayout.BeginHorizontal(); |
| 472 | EditorGUILayout.LabelField(label, GUILayout.Width(labelWidth)); |
| 473 | |
| 474 | Rect r = EditorGUILayout.BeginVertical(); |
| 475 | |
| 476 | float barWidth = (r.width - panelInnerRightPad - progressBarPad * 2) / 3.0f; |
| 477 | EditorGUI.ProgressBar(new Rect(r.x, r.y, barWidth, r.height), lastValue.Value, string.Format("{0:F1}%", lastValue.Value * 100.0f)); |
| 478 | |
| 479 | if (displayAverage) |
| 480 | { |
| 481 | float? averageValue = GetAveragePerfValueFloat(propertyName); |
| 482 | if (averageValue.HasValue) |
| 483 | { |
| 484 | EditorGUI.ProgressBar(new Rect(r.x + barWidth + progressBarPad, r.y, barWidth, r.height), averageValue.Value, string.Format("{0:F1}%", averageValue.Value * 100.0f)); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (displayMaximum) |
| 489 | { |
| 490 | float? maxValue = GetMaxPerfValueFloat(propertyName); |
| 491 | if (maxValue.HasValue) |
| 492 | { |
| 493 | EditorGUI.ProgressBar(new Rect(r.x + (barWidth + progressBarPad) * 2, r.y, barWidth, r.height), maxValue.Value, string.Format("{0:F1}%", maxValue.Value * 100.0f)); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | GUILayout.Space(progressBarHeight); |
| 498 | |
| 499 | EditorGUILayout.EndVertical(); |
| 500 | |
| 501 | EditorGUILayout.EndHorizontal(); |
| 502 | } |
| 503 | |
| 504 | void PresentFloatTimeInMs(string label, string propertyName, float maxScale, bool displayAverage, bool displayMaximum) |
| 505 | { |
| 506 | float? lastValue = GetLatestPerfValueFloat(propertyName); |
| 507 | if (!lastValue.HasValue) |
| 508 | { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | EditorGUILayout.BeginHorizontal(); |
| 513 | EditorGUILayout.LabelField(label, GUILayout.Width(labelWidth)); |
| 514 | |
| 515 | Rect r = EditorGUILayout.BeginVertical(); |
| 516 | |
| 517 | float barWidth = (r.width - panelInnerRightPad - progressBarPad * 2) / 3.0f; |
| 518 | EditorGUI.ProgressBar(new Rect(r.x, r.y, barWidth, r.height), lastValue.Value/maxScale, string.Format("{0:F1} ms", lastValue.Value * 1000.0f)); |
| 519 | |
| 520 | if (displayAverage) |
| 521 | { |
| 522 | float? averageValue = GetAveragePerfValueFloat(propertyName); |
| 523 | if (averageValue.HasValue) |
| 524 | { |
| 525 | EditorGUI.ProgressBar(new Rect(r.x + barWidth + progressBarPad, r.y, barWidth, r.height), averageValue.Value / maxScale, string.Format("{0:F1} ms", averageValue.Value * 1000.0f)); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (displayMaximum) |
| 530 | { |
| 531 | float? maxValue = GetMaxPerfValueFloat(propertyName); |
| 532 | if (maxValue.HasValue) |
| 533 | { |
| 534 | EditorGUI.ProgressBar(new Rect(r.x + (barWidth + progressBarPad) * 2, r.y, barWidth, r.height), maxValue.Value / maxScale, string.Format("{0:F1} ms", maxValue.Value * 1000.0f)); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | GUILayout.Space(progressBarHeight); |
| 539 | |
| 540 | EditorGUILayout.EndVertical(); |
| 541 | |
| 542 | EditorGUILayout.EndHorizontal(); |
| 543 | } |
| 544 | |
| 545 | void PresentIntProperty(string label, string propertyName) |
| 546 | { |
| 547 | int? lastValue = GetLatestPerfValueInt(propertyName); |
| 548 | if (!lastValue.HasValue) |
| 549 | { |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | PresentText(label, lastValue.Value.ToString()); |
| 554 | } |
| 555 | |
| 556 | void PresentText(string label, string text) |
| 557 | { |
| 558 | EditorGUILayout.BeginHorizontal(); |
| 559 | EditorGUILayout.LabelField(label, GUILayout.Width(labelWidth)); |
| 560 | EditorGUILayout.LabelField(text); |
| 561 | EditorGUILayout.EndHorizontal(); |
| 562 | } |
| 563 | |
| 564 | void PresentColumnTitles(string title0, string title1, string title2) |
| 565 | { |
| 566 | EditorGUILayout.BeginHorizontal(); |
| 567 | EditorGUILayout.LabelField("", GUILayout.Width(labelWidth)); |
| 568 | |
| 569 | float windowWidth = position.width; |
| 570 | float barWidth = (windowWidth - labelWidth - panelInnerRightPad * 3 ) / 3.0f; |
| 571 | EditorGUILayout.LabelField(title0, GUILayout.Width(barWidth)); |
| 572 | EditorGUILayout.LabelField(title1, GUILayout.Width(barWidth)); |
| 573 | EditorGUILayout.LabelField(title2, GUILayout.Width(barWidth)); |
| 574 | |
| 575 | EditorGUILayout.EndHorizontal(); |
| 576 | } |
| 577 | |
| 578 | // Called as the new window is opened. |
| 579 | private void Awake() |
| 580 | { |
| 581 | InitializeAndroidSdkPath(); |
| 582 | minSize = new Vector2(400, 300); |
| 583 | } |
| 584 | |
| 585 | void InitializeAndroidSdkPath() |
| 586 | { |
| 587 | androidSdkRootPath = OVRConfig.Instance.GetAndroidSDKPath(); |
| 588 | } |
| 589 | |
| 590 | // OnDestroy is called to close the EditorWindow window. |
| 591 | private void OnDestroy() |
| 592 | { |
| 593 | DisconnectPerfMetricsTcpServer(); |
| 594 | } |
| 595 | |
| 596 | // Called multiple times per second on all visible windows. |
| 597 | private void Update() |
| 598 | { |
| 599 | if (tcpClient != null && tcpClient.Connected) |
| 600 | { |
| 601 | tcpClient.Tick(); |
| 602 | } |
| 603 | |
| 604 | if (repaintRequested) |
| 605 | { |
| 606 | Repaint(); |
| 607 | repaintRequested = false; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void OnConnectionStateChanged() |
| 612 | { |
| 613 | repaintRequested = true; |
| 614 | |
| 615 | if (tcpClient.connectionState == OVRNetwork.OVRNetworkTcpClient.ConnectionState.Disconnected) |
| 616 | { |
| 617 | tcpClient.connectionStateChangedCallback -= OnConnectionStateChanged; |
| 618 | tcpClient.payloadReceivedCallback -= OnPayloadReceived; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | void OnPayloadReceived(int payloadType, byte[] buffer, int start, int length) |
| 623 | { |
| 624 | if (payloadType == OVRSystemPerfMetrics.PayloadTypeMetrics) |
| 625 | { |
| 626 | string message = Encoding.UTF8.GetString(buffer, start, length); |
| 627 | OnMessageReceived(message); |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | Debug.LogWarningFormat("[OVRSystemProfilerPanel] unrecongized payload type {0}", payloadType); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void OnMessageReceived(string message) |
| 636 | { |
| 637 | if (pauseReceiveMetrics) |
| 638 | { |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | var metrics = new OVRSystemPerfMetrics.PerfMetrics(); |
| 643 | if (!metrics.LoadFromJSON(message)) |
| 644 | { |
| 645 | Debug.LogWarning("Cannot analyze metrics: " + message); |
| 646 | return; |
| 647 | } |
| 648 | lock(receivedMetricsList) |
| 649 | { |
| 650 | if (receivedMetricsList.Count >= maxMetricsFrames) |
| 651 | { |
| 652 | receivedMetricsList.RemoveAt(0); |
| 653 | } |
| 654 | receivedMetricsList.Add(metrics); |
| 655 | } |
| 656 | repaintRequested = true; |
| 657 | } |
| 658 | |
| 659 | void ConnectPerfMetricsTcpServer() |
| 660 | { |
| 661 | tcpClient.connectionStateChangedCallback += OnConnectionStateChanged; |
| 662 | tcpClient.payloadReceivedCallback += OnPayloadReceived; |
| 663 | |
| 664 | tcpClient.Connect(remoteListeningPort); |
| 665 | |
| 666 | #if UNITY_2017_1_OR_NEWER |
| 667 | EditorApplication.playModeStateChanged += OnApplicationPlayModeStateChanged; |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | void DisconnectPerfMetricsTcpServer() |
| 672 | { |
| 673 | #if UNITY_2017_1_OR_NEWER |
| 674 | EditorApplication.playModeStateChanged -= OnApplicationPlayModeStateChanged; |
| 675 | #endif |
| 676 | |
| 677 | tcpClient.Disconnect(); |
| 678 | } |
| 679 | |
| 680 | #if UNITY_2017_1_OR_NEWER |
| 681 | void OnApplicationPlayModeStateChanged(PlayModeStateChange change) |
| 682 | { |
| 683 | Debug.LogFormat("[OVRSystemPerfMetricsWindow] OnApplicationPlayModeStateChanged {0}", change.ToString()); |
| 684 | if (change == PlayModeStateChange.ExitingPlayMode) |
| 685 | { |
| 686 | tcpClient.Disconnect(); |
| 687 | } |
| 688 | } |
| 689 | #endif |
| 690 | |
| 691 | } |