blob: de62d399cb7cc3d55298724c356c03c378e38190 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Jan Schefflerf168ba42020-07-30 15:29:474// @ts-nocheck
5// TODO(crbug.com/1011811): Enable TypeScript compiler checks
Blink Reformat4c46d092018-04-07 15:32:376
Tim van der Lippecec9b762020-02-13 15:31:227import * as LayerViewer from '../layer_viewer/layer_viewer.js';
8import * as SDK from '../sdk/sdk.js'; // eslint-disable-line no-unused-vars
9import * as TimelineModel from '../timeline_model/timeline_model.js';
10import * as UI from '../ui/ui.js';
11
12export class TimelinePaintProfilerView extends UI.SplitWidget.SplitWidget {
Blink Reformat4c46d092018-04-07 15:32:3713 /**
Tim van der Lippecec9b762020-02-13 15:31:2214 * @param {!TimelineModel.TimelineFrameModel.TimelineFrameModel} frameModel
Blink Reformat4c46d092018-04-07 15:32:3715 */
16 constructor(frameModel) {
17 super(false, false);
18 this.element.classList.add('timeline-paint-profiler-view');
19 this.setSidebarSize(60);
20 this.setResizable(false);
21
22 this._frameModel = frameModel;
Tim van der Lippecec9b762020-02-13 15:31:2223 this._logAndImageSplitWidget = new UI.SplitWidget.SplitWidget(true, false);
Blink Reformat4c46d092018-04-07 15:32:3724 this._logAndImageSplitWidget.element.classList.add('timeline-paint-profiler-log-split');
25 this.setMainWidget(this._logAndImageSplitWidget);
Tim van der Lippe0176f6c2020-01-08 11:07:0126 this._imageView = new TimelinePaintImageView();
Blink Reformat4c46d092018-04-07 15:32:3727 this._logAndImageSplitWidget.setMainWidget(this._imageView);
28
Tim van der Lippecec9b762020-02-13 15:31:2229 this._paintProfilerView =
30 new LayerViewer.PaintProfilerView.PaintProfilerView(this._imageView.showImage.bind(this._imageView));
Blink Reformat4c46d092018-04-07 15:32:3731 this._paintProfilerView.addEventListener(
32 LayerViewer.PaintProfilerView.Events.WindowChanged, this._onWindowChanged, this);
33 this.setSidebarWidget(this._paintProfilerView);
34
Tim van der Lippecec9b762020-02-13 15:31:2235 this._logTreeView = new LayerViewer.PaintProfilerView.PaintProfilerCommandLogView();
Blink Reformat4c46d092018-04-07 15:32:3736 this._logAndImageSplitWidget.setSidebarWidget(this._logTreeView);
37
38 this._needsUpdateWhenVisible = false;
Tim van der Lippecec9b762020-02-13 15:31:2239 /** @type {?SDK.PaintProfiler.PaintProfilerSnapshot} */
Blink Reformat4c46d092018-04-07 15:32:3740 this._pendingSnapshot = null;
41 /** @type {?SDK.TracingModel.Event} */
42 this._event = null;
Tim van der Lippecec9b762020-02-13 15:31:2243 /** @type {?SDK.PaintProfiler.PaintProfilerModel} */
Blink Reformat4c46d092018-04-07 15:32:3744 this._paintProfilerModel = null;
Tim van der Lippecec9b762020-02-13 15:31:2245 /** @type {?SDK.PaintProfiler.PaintProfilerSnapshot} */
Blink Reformat4c46d092018-04-07 15:32:3746 this._lastLoadedSnapshot = null;
47 }
48
49 /**
50 * @override
51 */
52 wasShown() {
53 if (this._needsUpdateWhenVisible) {
54 this._needsUpdateWhenVisible = false;
55 this._update();
56 }
57 }
58
59 /**
Tim van der Lippecec9b762020-02-13 15:31:2260 * @param {!SDK.PaintProfiler.PaintProfilerSnapshot} snapshot
Blink Reformat4c46d092018-04-07 15:32:3761 */
62 setSnapshot(snapshot) {
63 this._releaseSnapshot();
64 this._pendingSnapshot = snapshot;
65 this._event = null;
66 this._updateWhenVisible();
67 }
68
69 /**
Tim van der Lippecec9b762020-02-13 15:31:2270 * @param {!SDK.PaintProfiler.PaintProfilerModel} paintProfilerModel
Blink Reformat4c46d092018-04-07 15:32:3771 * @param {!SDK.TracingModel.Event} event
72 * @return {boolean}
73 */
74 setEvent(paintProfilerModel, event) {
75 this._releaseSnapshot();
76 this._paintProfilerModel = paintProfilerModel;
77 this._pendingSnapshot = null;
78 this._event = event;
79
80 this._updateWhenVisible();
Tim van der Lippe1d6e57a2019-09-30 11:55:3481 if (this._event.name === TimelineModel.TimelineModel.RecordType.Paint) {
Tim van der Lippecec9b762020-02-13 15:31:2282 return !!TimelineModel.TimelineModel.TimelineData.forEvent(event).picture;
Tim van der Lippe1d6e57a2019-09-30 11:55:3483 }
84 if (this._event.name === TimelineModel.TimelineModel.RecordType.RasterTask) {
Blink Reformat4c46d092018-04-07 15:32:3785 return this._frameModel.hasRasterTile(this._event);
Tim van der Lippe1d6e57a2019-09-30 11:55:3486 }
Blink Reformat4c46d092018-04-07 15:32:3787 return false;
88 }
89
90 _updateWhenVisible() {
Tim van der Lippe1d6e57a2019-09-30 11:55:3491 if (this.isShowing()) {
Blink Reformat4c46d092018-04-07 15:32:3792 this._update();
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 } else {
Blink Reformat4c46d092018-04-07 15:32:3794 this._needsUpdateWhenVisible = true;
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 }
Blink Reformat4c46d092018-04-07 15:32:3796 }
97
98 _update() {
99 this._logTreeView.setCommandLog([]);
100 this._paintProfilerView.setSnapshotAndLog(null, [], null);
101
102 let snapshotPromise;
103 if (this._pendingSnapshot) {
104 snapshotPromise = Promise.resolve({rect: null, snapshot: this._pendingSnapshot});
105 } else if (this._event.name === TimelineModel.TimelineModel.RecordType.Paint) {
Tim van der Lippecec9b762020-02-13 15:31:22106 const picture = TimelineModel.TimelineModel.TimelineData.forEvent(this._event).picture;
Blink Reformat4c46d092018-04-07 15:32:37107 snapshotPromise = picture.objectPromise()
108 .then(data => this._paintProfilerModel.loadSnapshot(data['skp64']))
109 .then(snapshot => snapshot && {rect: null, snapshot: snapshot});
110 } else if (this._event.name === TimelineModel.TimelineModel.RecordType.RasterTask) {
111 snapshotPromise = this._frameModel.rasterTilePromise(this._event);
112 } else {
113 console.assert(false, 'Unexpected event type or no snapshot');
114 return;
115 }
116 snapshotPromise.then(snapshotWithRect => {
117 this._releaseSnapshot();
118 if (!snapshotWithRect) {
119 this._imageView.showImage();
120 return;
121 }
122 const snapshot = snapshotWithRect.snapshot;
123 this._lastLoadedSnapshot = snapshot;
124 this._imageView.setMask(snapshotWithRect.rect);
125 snapshot.commandLog().then(log => onCommandLogDone.call(this, snapshot, snapshotWithRect.rect, log));
126 });
127
128 /**
Tim van der Lippecec9b762020-02-13 15:31:22129 * @param {!SDK.PaintProfiler.PaintProfilerSnapshot} snapshot
Blink Reformat4c46d092018-04-07 15:32:37130 * @param {?Protocol.DOM.Rect} clipRect
Tim van der Lippecec9b762020-02-13 15:31:22131 * @param {!Array.<!SDK.PaintProfiler.PaintProfilerLogItem>=} log
Tim van der Lippe0176f6c2020-01-08 11:07:01132 * @this {TimelinePaintProfilerView}
Blink Reformat4c46d092018-04-07 15:32:37133 */
134 function onCommandLogDone(snapshot, clipRect, log) {
135 this._logTreeView.setCommandLog(log || []);
136 this._paintProfilerView.setSnapshotAndLog(snapshot, log || [], clipRect);
137 }
138 }
139
140 _releaseSnapshot() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34141 if (!this._lastLoadedSnapshot) {
Blink Reformat4c46d092018-04-07 15:32:37142 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34143 }
Blink Reformat4c46d092018-04-07 15:32:37144 this._lastLoadedSnapshot.release();
145 this._lastLoadedSnapshot = null;
146 }
147
148 _onWindowChanged() {
149 this._logTreeView.updateWindow(this._paintProfilerView.selectionWindow());
150 }
Tim van der Lippe0176f6c2020-01-08 11:07:01151}
Blink Reformat4c46d092018-04-07 15:32:37152
153/**
154 * @unrestricted
155 */
Tim van der Lippecec9b762020-02-13 15:31:22156export class TimelinePaintImageView extends UI.Widget.Widget {
Blink Reformat4c46d092018-04-07 15:32:37157 constructor() {
158 super(true);
159 this.registerRequiredCSS('timeline/timelinePaintProfiler.css');
160 this.contentElement.classList.add('fill', 'paint-profiler-image-view');
161 this._imageContainer = this.contentElement.createChild('div', 'paint-profiler-image-container');
162 this._imageElement = this._imageContainer.createChild('img');
163 this._maskElement = this._imageContainer.createChild('div');
164 this._imageElement.addEventListener('load', this._updateImagePosition.bind(this), false);
165
Tim van der Lippecec9b762020-02-13 15:31:22166 this._transformController = new LayerViewer.TransformController.TransformController(this.contentElement, true);
Blink Reformat4c46d092018-04-07 15:32:37167 this._transformController.addEventListener(
168 LayerViewer.TransformController.Events.TransformChanged, this._updateImagePosition, this);
169 }
170
171 /**
172 * @override
173 */
174 onResize() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 if (this._imageElement.src) {
Blink Reformat4c46d092018-04-07 15:32:37176 this._updateImagePosition();
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 }
Blink Reformat4c46d092018-04-07 15:32:37178 }
179
180 _updateImagePosition() {
181 const width = this._imageElement.naturalWidth;
182 const height = this._imageElement.naturalHeight;
183 const clientWidth = this.contentElement.clientWidth;
184 const clientHeight = this.contentElement.clientHeight;
185
186 const paddingFraction = 0.1;
187 const paddingX = clientWidth * paddingFraction;
188 const paddingY = clientHeight * paddingFraction;
189 const scaleX = (clientWidth - paddingX) / width;
190 const scaleY = (clientHeight - paddingY) / height;
191 const scale = Math.min(scaleX, scaleY);
192
193 if (this._maskRectangle) {
194 const style = this._maskElement.style;
195 style.width = width + 'px';
196 style.height = height + 'px';
197 style.borderLeftWidth = this._maskRectangle.x + 'px';
198 style.borderTopWidth = this._maskRectangle.y + 'px';
199 style.borderRightWidth = (width - this._maskRectangle.x - this._maskRectangle.width) + 'px';
200 style.borderBottomWidth = (height - this._maskRectangle.y - this._maskRectangle.height) + 'px';
201 }
202 this._transformController.setScaleConstraints(0.5, 10 / scale);
203 let matrix = new WebKitCSSMatrix()
204 .scale(this._transformController.scale(), this._transformController.scale())
205 .translate(clientWidth / 2, clientHeight / 2)
206 .scale(scale, scale)
207 .translate(-width / 2, -height / 2);
208 const bounds = UI.Geometry.boundsForTransformedPoints(matrix, [0, 0, 0, width, height, 0]);
209 this._transformController.clampOffsets(
210 paddingX - bounds.maxX, clientWidth - paddingX - bounds.minX, paddingY - bounds.maxY,
211 clientHeight - paddingY - bounds.minY);
212 matrix = new WebKitCSSMatrix()
213 .translate(this._transformController.offsetX(), this._transformController.offsetY())
214 .multiply(matrix);
215 this._imageContainer.style.webkitTransform = matrix.toString();
216 }
217
218 /**
219 * @param {string=} imageURL
220 */
221 showImage(imageURL) {
222 this._imageContainer.classList.toggle('hidden', !imageURL);
Tim van der Lippe1d6e57a2019-09-30 11:55:34223 if (imageURL) {
Blink Reformat4c46d092018-04-07 15:32:37224 this._imageElement.src = imageURL;
Tim van der Lippe1d6e57a2019-09-30 11:55:34225 }
Blink Reformat4c46d092018-04-07 15:32:37226 }
227
228 /**
229 * @param {?Protocol.DOM.Rect} maskRectangle
230 */
231 setMask(maskRectangle) {
232 this._maskRectangle = maskRectangle;
233 this._maskElement.classList.toggle('hidden', !maskRectangle);
234 }
Tim van der Lippe0176f6c2020-01-08 11:07:01235}