1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Trace} from 'trace/trace';
18import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
19import {UiRect} from 'viewers/components/rects/types2d';
20import {DisplayIdentifier} from './display_identifier';
21import {RectFilter} from './rect_filter';
22import {RectShowState} from './rect_show_state';
23import {UserOptions} from './user_options';
24
25export class RectsPresenter {
26  private allCurrentRects: UiRect[] = [];
27  private rectFilter = new RectFilter();
28  private rectsToDraw: UiRect[] = [];
29  private displays: DisplayIdentifier[] = [];
30  private rectIdToShowState: Map<string, RectShowState> | undefined;
31
32  constructor(
33    private userOptions: UserOptions,
34    private makeUiRectsStrategy: (
35      tree: HierarchyTreeNode,
36      trace: Trace<HierarchyTreeNode>,
37    ) => UiRect[],
38    private makeDisplaysStrategy?: (rects: UiRect[]) => DisplayIdentifier[],
39  ) {}
40
41  getUserOptions() {
42    return this.userOptions;
43  }
44
45  getRectsToDraw() {
46    return this.rectsToDraw;
47  }
48
49  getRectIdToShowState() {
50    return this.rectIdToShowState;
51  }
52
53  getDisplays() {
54    return this.displays;
55  }
56
57  setDisplays(displays: DisplayIdentifier[]) {
58    this.displays = displays;
59  }
60
61  applyHierarchyTreesChange(
62    hierarchyTrees: Array<[Trace<HierarchyTreeNode>, HierarchyTreeNode[]]>,
63  ) {
64    this.allCurrentRects = [];
65    for (const [trace, trees] of hierarchyTrees) {
66      trees.forEach((tree) => {
67        this.allCurrentRects.push(...this.makeUiRectsStrategy(tree, trace));
68      });
69    }
70    this.updateRectsToDrawAndRectIdToShowState();
71    if (this.makeDisplaysStrategy) {
72      this.displays = this.makeDisplaysStrategy(this.rectsToDraw);
73    }
74  }
75
76  applyRectsUserOptionsChange(userOptions: UserOptions) {
77    this.userOptions = userOptions;
78    this.updateRectsToDrawAndRectIdToShowState();
79  }
80
81  applyRectShowStateChange(id: string, newShowState: RectShowState) {
82    this.rectFilter.updateRectShowState(id, newShowState);
83    this.updateRectsToDrawAndRectIdToShowState();
84  }
85
86  private updateRectsToDrawAndRectIdToShowState() {
87    this.rectsToDraw = this.filterRects(this.allCurrentRects);
88    this.rectIdToShowState = this.rectFilter.getRectIdToShowState(
89      this.allCurrentRects,
90      this.rectsToDraw,
91    );
92  }
93
94  private filterRects(rects: UiRect[]): UiRect[] {
95    const isOnlyVisibleMode =
96      this.userOptions['showOnlyVisible']?.enabled ?? false;
97    const isIgnoreHiddenMode =
98      this.userOptions['ignoreNonHidden']?.enabled ?? false;
99    return this.rectFilter.filterRects(
100      rects,
101      isOnlyVisibleMode,
102      isIgnoreHiddenMode,
103    );
104  }
105}
106