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/ui_rect'; 20import {DisplayIdentifier} from './display_identifier'; 21import {TraceAndTrees} from './hierarchy_presenter'; 22import {RectFilter} from './rect_filter'; 23import {RectShowState} from './rect_show_state'; 24import {UserOptions} from './user_options'; 25 26export class RectsPresenter { 27 private readonly rectFilter = new RectFilter(this.convertToKey); 28 private allCurrentRects: UiRect[] = []; 29 private rectsToDraw: UiRect[] = []; 30 private displays: DisplayIdentifier[] = []; 31 private rectIdToShowState: Map<string, RectShowState> | undefined; 32 33 constructor( 34 private userOptions: UserOptions, 35 private makeUiRectsStrategy: ( 36 tree: HierarchyTreeNode, 37 trace: Trace<HierarchyTreeNode>, 38 ) => UiRect[], 39 private makeDisplaysStrategy?: (rects: UiRect[]) => DisplayIdentifier[], 40 private convertToKey: (rectId: string) => string = (id: string) => id, 41 ) {} 42 43 getUserOptions(): UserOptions { 44 return this.userOptions; 45 } 46 47 getRectsToDraw(): UiRect[] { 48 return this.rectsToDraw; 49 } 50 51 getRectIdToShowState(): Map<string, RectShowState> | undefined { 52 return this.rectIdToShowState; 53 } 54 55 getDisplays(): DisplayIdentifier[] { 56 return this.displays; 57 } 58 59 setDisplays(displays: DisplayIdentifier[]) { 60 this.displays = displays; 61 } 62 63 applyHierarchyTreesChange(hierarchyTrees: TraceAndTrees[]) { 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 updateRectShowStates( 87 rectIdToShowState: Map<string, RectShowState> | undefined, 88 ) { 89 this.rectFilter.clear(); 90 if (rectIdToShowState) { 91 for (const [id, state] of rectIdToShowState.entries()) { 92 this.rectFilter.updateRectShowState(id, state); 93 } 94 } 95 this.updateRectsToDrawAndRectIdToShowState(); 96 } 97 98 clear() { 99 this.allCurrentRects = []; 100 this.rectsToDraw = []; 101 this.displays = []; 102 this.rectIdToShowState = undefined; 103 this.rectFilter.clear(); 104 } 105 106 private updateRectsToDrawAndRectIdToShowState() { 107 this.rectsToDraw = this.filterRects(this.allCurrentRects); 108 this.rectIdToShowState = this.rectFilter.getRectIdToShowState( 109 this.allCurrentRects, 110 this.rectsToDraw, 111 ); 112 } 113 114 private filterRects(rects: UiRect[]): UiRect[] { 115 const isOnlyVisibleMode = 116 this.userOptions['showOnlyVisible']?.enabled ?? false; 117 const isIgnoreRectShowStateMode = 118 this.userOptions['ignoreRectShowState']?.enabled ?? false; 119 const isOnlyWithContentMode = 120 this.userOptions['showOnlyWithContent']?.enabled ?? false; 121 return this.rectFilter.filterRects( 122 rects, 123 isOnlyVisibleMode, 124 isIgnoreRectShowStateMode, 125 isOnlyWithContentMode, 126 ); 127 } 128} 129