void ClampDepthFilterForAutoRespectingDiff(ref int filterDepthLeft, ref int filterDepthRight, ProfileDataView profileLeftView, ProfileDataView profileRightView)
        {
            if (filterDepthLeft == ProfileAnalyzer.kDepthAll && filterDepthRight == ProfileAnalyzer.kDepthAll)
            {
                // nothing to do here, keep showing all
                return;
            }

            var leftMax  = profileLeftView.GetMaxDepth();
            var rightMax = profileRightView.GetMaxDepth();

            var sliceMenuEntryIndex = CalcSliceMenuEntryIndex(filterDepthLeft, filterDepthRight, leftMax, rightMax);

            CalcAutoSlicesFromMenuEntryIndex(sliceMenuEntryIndex, ref filterDepthLeft, ref filterDepthRight, leftMax, rightMax);
        }
        internal void DrawDepthFilter(bool isAnalysisRunning, bool singleView,
                                      ProfileDataView profileSingleView, ProfileDataView profileLeftView, ProfileDataView profileRightView)
        {
#if !UNITY_2019_1_OR_NEWER
            bool triggerRefresh = false;
            if (!isAnalysisRunning)
            {
                if (singleView)
                {
                    var maxDepth = profileSingleView.GetMaxDepth();
                    if (m_DepthStrings == null || maxDepth != m_OldDepthRaw1)
                    {
                        SetDepthStringsSingle(maxDepth, out m_DepthStrings, out m_DepthValues);
                        m_OldDepthRaw1 = maxDepth;
                        triggerRefresh = true;
                    }
                }
                else
                {
                    if (m_DepthFilterAuto)
                    {
                        var maxLeftRaw  = profileLeftView.GetMaxDepth();
                        var maxRightRaw = profileRightView.GetMaxDepth();
                        if (m_DepthStringsAuto == null ||
                            m_OldDepthRaw1 != maxLeftRaw || m_OldDepthRaw2 != maxRightRaw)
                        {
                            SetDepthStringsCompare(maxLeftRaw, out m_DepthStringsAuto, out m_DepthValuesAuto, maxRightRaw);
                            m_OldDepthRaw1 = maxLeftRaw;
                            m_OldDepthRaw2 = maxRightRaw;
                            triggerRefresh = true;
                        }
                    }
                    else
                    {
                        var maxDepthLeft = profileLeftView.GetMaxDepth();
                        if (m_DepthStrings1 == null || m_OldDepthRaw1 != maxDepthLeft)
                        {
                            SetDepthStringsSingle(maxDepthLeft, out m_DepthStrings1, out m_DepthValues1);
                            m_OldDepthRaw1 = maxDepthLeft;
                            triggerRefresh = true;
                        }

                        var maxDepthRight = profileRightView.GetMaxDepth();
                        if (m_DepthStrings2 == null || m_OldDepthRaw2 != maxDepthRight)
                        {
                            SetDepthStringsSingle(maxDepthRight, out m_DepthStrings2, out m_DepthValues2);
                            m_OldDepthRaw2 = maxDepthRight;
                            triggerRefresh = true;
                        }
                    }
                }
            }
#endif

            bool lastEnabled = GUI.enabled;
            bool enabled     = !isAnalysisRunning;

            EditorGUILayout.BeginHorizontal();
            if (singleView)
            {
                EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.depthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsLeftLabelWidth));
#if UNITY_2019_1_OR_NEWER
                DrawDepthFilterDropdown(null, enabled,
                                        profileSingleView, (primary, left, right) => m_DepthFilter = primary,
                                        ViewType.Single, profileSingleView, profileLeftView, profileRightView);
#else
                if (m_DepthStrings != null)
                {
                    var lastDepthFilter = m_DepthFilter;
                    m_DepthFilter = m_DepthFilter == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileSingleView.ClampToValidDepthValue(m_DepthFilter);

                    GUI.enabled   = enabled;
                    m_DepthFilter = EditorGUILayout.IntPopup(m_DepthFilter, m_DepthStrings, m_DepthValues, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
                    GUI.enabled   = lastEnabled;
                    if (m_DepthFilter != lastDepthFilter)
                    {
                        triggerRefresh = true;
                    }
                }
#endif
            }
            else
            {
                EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.depthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsLeftLabelWidth));

                if (m_DepthFilterAuto)
                {
#if UNITY_2019_1_OR_NEWER
                    DrawDepthFilterDropdown(null, enabled, profileLeftView, (primary, left, right) =>
                    {
                        m_DepthFilter1 = left;
                        m_DepthFilter2 = right;
                        ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
                    },
                                            ViewType.Locked, profileSingleView, profileLeftView, profileRightView);
#else
                    if (m_DepthStringsAuto != null)
                    {
                        var leftMax  = profileLeftView.GetMaxDepth();
                        var rightMax = profileRightView.GetMaxDepth();
                        var lastDepthFilterDropdownIndex = CalcSliceMenuEntryIndex(m_DepthFilter1, m_DepthFilter2, leftMax, rightMax);
                        ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2,
                                                              profileLeftView, profileRightView);

                        var layoutOptionWidth = ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth;
                        if (m_DepthFilter1 != m_DepthFilter2)
                        {
                            layoutOptionWidth = ProfileAnalyzerWindow.LayoutSize.FilterOptionsLockedEnumWidth;
                        }

                        GUI.enabled = enabled;
                        var depthFilterDropdownIndex = Mathf.Clamp(lastDepthFilterDropdownIndex, -1, m_DepthStringsAuto.Length - 1);
                        depthFilterDropdownIndex = EditorGUILayout.IntPopup(depthFilterDropdownIndex, m_DepthStringsAuto, m_DepthValuesAuto, GUILayout.Width(layoutOptionWidth));
                        GUI.enabled = lastEnabled;

                        if (depthFilterDropdownIndex != lastDepthFilterDropdownIndex)
                        {
                            if (depthFilterDropdownIndex == ProfileAnalyzer.kDepthAll)
                            {
                                m_DepthFilter1 = m_DepthFilter2 = ProfileAnalyzer.kDepthAll;
                            }
                            else
                            {
                                CalcAutoSlicesFromMenuEntryIndex(depthFilterDropdownIndex, ref m_DepthFilter1, ref m_DepthFilter2, leftMax, rightMax);
                            }
                            ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);
                            triggerRefresh = true;
                        }
                    }
#endif
                }
                else
                {
#if UNITY_2019_1_OR_NEWER
                    DrawDepthFilterDropdown(ProfileAnalyzerWindow.Styles.leftDepthTitle, enabled, profileLeftView,
                                            (primary, left, right) => m_DepthFilter1 = primary,
                                            ViewType.Left, profileSingleView, profileLeftView, profileRightView);
#else
                    if (m_DepthStrings1 != null)
                    {
                        EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.leftDepthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));

                        int lastDepthFilter1 = m_DepthFilter1;
                        m_DepthFilter1 = m_DepthFilter1 == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileLeftView.ClampToValidDepthValue(m_DepthFilter1);

                        GUI.enabled    = enabled;
                        m_DepthFilter1 = EditorGUILayout.IntPopup(m_DepthFilter1, m_DepthStrings1, m_DepthValues1, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
                        GUI.enabled    = lastEnabled;
                        if (m_DepthFilter1 != lastDepthFilter1)
                        {
                            triggerRefresh = true;
                        }
                    }
#endif

#if UNITY_2019_1_OR_NEWER
                    DrawDepthFilterDropdown(ProfileAnalyzerWindow.Styles.rightDepthTitle, enabled && !m_DepthFilterAuto, profileRightView,
                                            (primary, left, right) => m_DepthFilter2 = primary,
                                            ViewType.Right, profileSingleView, profileLeftView, profileRightView);
#else
                    if (m_DepthStrings2 != null)
                    {
                        EditorGUILayout.LabelField(ProfileAnalyzerWindow.Styles.rightDepthTitle, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));

                        int lastDepthFilter2 = m_DepthFilter2;
                        m_DepthFilter2 = m_DepthFilter2 == ProfileAnalyzer.kDepthAll ? ProfileAnalyzer.kDepthAll : profileRightView.ClampToValidDepthValue(m_DepthFilter2);

                        GUI.enabled    = enabled && !m_DepthFilterAuto;
                        m_DepthFilter2 = EditorGUILayout.IntPopup(m_DepthFilter2, m_DepthStrings2, m_DepthValues2, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
                        GUI.enabled    = lastEnabled;
                        if (m_DepthFilter2 != lastDepthFilter2)
                        {
                            triggerRefresh = true;
                        }
                    }
#endif
                }
                bool lastDepthFilterLock = m_DepthFilterAuto;
                GUI.enabled       = enabled;
                m_DepthFilterAuto = EditorGUILayout.ToggleLeft(ProfileAnalyzerWindow.Styles.autoDepthTitle, m_DepthFilterAuto);
                GUI.enabled       = lastEnabled;
                if (m_DepthFilterAuto != lastDepthFilterLock)
                {
                    if (UpdateDepthFilters(singleView, profileSingleView, profileLeftView, profileRightView))
                    {
                        m_UpdateActiveTabCallback(true);
                    }
#if !UNITY_2019_1_OR_NEWER
                    m_DepthStringsAuto = null;
                    m_DepthStrings1    = null;
                    m_DepthStrings2    = null;
#endif
                }
            }
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
#if !UNITY_2019_1_OR_NEWER
            if (triggerRefresh)
            {
                UpdateDepthFilters(singleView, profileSingleView, profileLeftView, profileRightView);
                m_UpdateActiveTabCallback(true);
            }
#endif
        }
        void DrawDepthFilterDropdown(GUIContent title, bool enabled, ProfileDataView view, Action <int, int, int> callback,
                                     ViewType viewType, ProfileDataView profileSingleView, ProfileDataView profileLeftView, ProfileDataView profileRightView)
        {
            if (title != null)
            {
                EditorGUILayout.LabelField(title, GUILayout.Width(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));
            }

            int depthFilter      = ProfileAnalyzer.kDepthAll;
            int depthFilterOther = ProfileAnalyzer.kDepthAll;
            var maxDepth         = view.GetMaxDepth();
            var maxDepthLeft     = ProfileAnalyzer.kDepthAll;
            var maxDepthRight    = ProfileAnalyzer.kDepthAll;

            var        oldDepthFilter            = ProfileAnalyzer.kDepthAll;
            var        oldDepthFilterOtherLocked = ProfileAnalyzer.kDepthAll;
            var        depthDiff = mostCommonDepthDiff;
            GUIContent content;

            switch (viewType)
            {
            case ViewType.Single:
                oldDepthFilter = m_DepthFilter;
                depthFilter    = m_DepthFilter =
                    m_DepthFilter == ProfileAnalyzer.kDepthAll ?
                    ProfileAnalyzer.kDepthAll :
                    profileSingleView.ClampToValidDepthValue(m_DepthFilter);
                content          = new GUIContent(DepthFilterToString(depthFilter));
                depthFilterOther = depthFilter;
                depthDiff        = 0;
                break;

            case ViewType.Left:
                oldDepthFilter = m_DepthFilter1;
                depthFilter    = m_DepthFilter1 =
                    m_DepthFilter1 == ProfileAnalyzer.kDepthAll ?
                    ProfileAnalyzer.kDepthAll :
                    profileLeftView.ClampToValidDepthValue(m_DepthFilter1);
                content          = new GUIContent(DepthFilterToString(depthFilter));
                depthFilterOther = depthFilter;
                break;

            case ViewType.Right:
                oldDepthFilter = m_DepthFilter2;
                depthFilter    = m_DepthFilter2 = m_DepthFilter2 == ProfileAnalyzer.kDepthAll
                        ? ProfileAnalyzer.kDepthAll
                        : profileRightView.ClampToValidDepthValue(m_DepthFilter2);
                content          = new GUIContent(DepthFilterToString(depthFilter));
                depthFilterOther = depthFilter;
                break;

            case ViewType.Locked:
                oldDepthFilter            = m_DepthFilter1;
                oldDepthFilterOtherLocked = m_DepthFilter2;
                maxDepth      = maxDepthLeft = profileLeftView.GetMaxDepth();
                maxDepthRight = profileRightView.GetMaxDepth();

                ClampDepthFilterForAutoRespectingDiff(ref m_DepthFilter1, ref m_DepthFilter2, profileLeftView, profileRightView);

                depthFilter      = m_DepthFilter1;
                depthFilterOther = m_DepthFilter2;
                content          = new GUIContent(DepthFilterToString(m_DepthFilter1, m_DepthFilter2, mostCommonDepthDiff < 0));
                break;

            default:
                throw new NotImplementedException();
            }

            var lastEnabled = GUI.enabled;

            GUI.enabled = enabled;
            var rect = GUILayoutUtility.GetRect(content, EditorStyles.popup, GUILayout.MinWidth(ProfileAnalyzerWindow.LayoutSize.FilterOptionsEnumWidth));

            if (GUI.Button(rect, content, EditorStyles.popup))
            {
                var dropdown = new DepthSliceDropdown(maxDepth, depthFilter, depthFilterOther, (slice, left, right) =>
                {
                    if (slice != depthFilter || (viewType == ViewType.Locked && (left != m_DepthFilter1 || right != m_DepthFilter2)))
                    {
                        callback(slice, left, right);
                        UpdateDepthFilters(viewType == ViewType.Single, profileSingleView, profileLeftView, profileRightView);
                        m_UpdateActiveTabCallback(true);
                    }
                }, depthDiff, maxDepthRight);
                dropdown.Show(rect);
                EditorGUIUtility.ExitGUI();
            }
            else
            {
                // The depths can change because the data changed, not just because the user selected a different option in the dropdown
                // in that case, the depth filters need to perform a refresh
                if (oldDepthFilter != depthFilter || viewType == ViewType.Locked && oldDepthFilterOtherLocked != depthFilterOther)
                {
                    UpdateDepthFilters(viewType == ViewType.Single, profileSingleView, profileLeftView, profileRightView);
                    m_UpdateActiveTabCallback(true);
                }
            }
            GUI.enabled = lastEnabled;
        }