1/*
2 * Copyright (C) 2022 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 {WinscopeEvent} from 'messaging/winscope_event';
18import {EmitEvent} from 'messaging/winscope_event_emitter';
19import {Trace} from 'trace/trace';
20import {Traces} from 'trace/traces';
21import {TraceType} from 'trace/trace_type';
22import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
23import {NotifyHierarchyViewCallbackType} from 'viewers/common/abstract_hierarchy_viewer_presenter';
24import {ViewerEvents} from 'viewers/common/viewer_events';
25import {View, Viewer, ViewType} from 'viewers/viewer';
26import {Presenter} from './presenter';
27import {UiData} from './ui_data';
28
29export class ViewerSurfaceFlinger implements Viewer {
30  static readonly DEPENDENCIES: TraceType[] = [TraceType.SURFACE_FLINGER];
31
32  private readonly trace: Trace<HierarchyTreeNode>;
33  private readonly htmlElement: HTMLElement;
34  private readonly presenter: Presenter;
35  private readonly view: View;
36
37  constructor(
38    trace: Trace<HierarchyTreeNode>,
39    traces: Traces,
40    storage: Storage,
41  ) {
42    this.trace = trace;
43    this.htmlElement = document.createElement('viewer-surface-flinger');
44
45    const notifyViewCallback = (uiData: UiData) => {
46      (this.htmlElement as any).inputData = uiData;
47    };
48    this.presenter = new Presenter(
49      trace,
50      traces,
51      storage,
52      notifyViewCallback as NotifyHierarchyViewCallbackType,
53    );
54    this.presenter.addEventListeners(this.htmlElement);
55
56    this.htmlElement.addEventListener(
57      ViewerEvents.RectsDblClick,
58      async (event) => {
59        const rectId = (event as CustomEvent).detail.clickedRectId;
60        await this.presenter.onRectDoubleClick(rectId);
61      },
62    );
63
64    this.view = new View(
65      ViewType.TAB,
66      this.getTraces(),
67      this.htmlElement,
68      'Surface Flinger',
69    );
70  }
71
72  setEmitEvent(callback: EmitEvent) {
73    this.presenter.setEmitEvent(callback);
74  }
75
76  async onWinscopeEvent(event: WinscopeEvent) {
77    await this.presenter.onAppEvent(event);
78  }
79
80  getViews(): View[] {
81    return [this.view];
82  }
83
84  getTraces(): Array<Trace<HierarchyTreeNode>> {
85    return [this.trace];
86  }
87}
88