Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 1 | // Copyright (C) 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import * as m from 'mithril'; |
| 16 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 17 | import {assertExists, assertTrue} from '../base/logging'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 18 | |
| 19 | import {globals} from './globals'; |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 20 | import {isPanelVNode, Panel, PanelSize} from './panel'; |
| 21 | import { |
| 22 | debugNow, |
| 23 | perfDebug, |
| 24 | perfDisplay, |
| 25 | RunningStatistics, |
| 26 | runningStatStr |
| 27 | } from './perf'; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * If the panel container scrolls, the backing canvas height is |
| 31 | * SCROLLING_CANVAS_OVERDRAW_FACTOR * parent container height. |
| 32 | */ |
| 33 | const SCROLLING_CANVAS_OVERDRAW_FACTOR = 2; |
| 34 | |
Deepanjan Roy | 4fa0ecc | 2018-10-03 16:10:46 -0400 | [diff] [blame] | 35 | // We need any here so we can accept vnodes with arbitrary attrs. |
| 36 | // tslint:disable-next-line:no-any |
| 37 | export type AnyAttrsVnode = m.Vnode<any, {}>; |
| 38 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 39 | interface Attrs { |
Deepanjan Roy | 4fa0ecc | 2018-10-03 16:10:46 -0400 | [diff] [blame] | 40 | panels: AnyAttrsVnode[]; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 41 | doesScroll: boolean; |
| 42 | } |
| 43 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 44 | export class PanelContainer implements m.ClassComponent<Attrs> { |
| 45 | // These values are updated with proper values in oncreate. |
| 46 | private parentWidth = 0; |
| 47 | private parentHeight = 0; |
| 48 | private scrollTop = 0; |
| 49 | private panelHeights: number[] = []; |
| 50 | private totalPanelHeight = 0; |
| 51 | private canvasHeight = 0; |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 52 | |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 53 | private panelPerfStats = new WeakMap<Panel, RunningStatistics>(); |
| 54 | private perfStats = { |
| 55 | totalPanels: 0, |
| 56 | panelsOnCanvas: 0, |
| 57 | renderStats: new RunningStatistics(10), |
| 58 | }; |
| 59 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 60 | // Attrs received in the most recent mithril redraw. We receive a new vnode |
| 61 | // with new attrs on every redraw, and we cache it here so that resize |
| 62 | // listeners and canvas redraw callbacks can access it. |
| 63 | private attrs: Attrs; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 64 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 65 | private ctx?: CanvasRenderingContext2D; |
| 66 | |
| 67 | private onResize: () => void = () => {}; |
| 68 | private parentOnScroll: () => void = () => {}; |
| 69 | private canvasRedrawer: () => void; |
| 70 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 71 | get canvasOverdrawFactor() { |
| 72 | return this.attrs.doesScroll ? SCROLLING_CANVAS_OVERDRAW_FACTOR : 1; |
| 73 | } |
| 74 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 75 | constructor(vnode: m.CVnode<Attrs>) { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 76 | this.attrs = vnode.attrs; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 77 | this.canvasRedrawer = () => this.redrawCanvas(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 78 | globals.rafScheduler.addRedrawCallback(this.canvasRedrawer); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 79 | perfDisplay.addContainer(this); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 80 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 81 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 82 | oncreate(vnodeDom: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 83 | // Save the canvas context in the state. |
| 84 | const canvas = |
| 85 | vnodeDom.dom.querySelector('.main-canvas') as HTMLCanvasElement; |
| 86 | const ctx = canvas.getContext('2d'); |
| 87 | if (!ctx) { |
| 88 | throw Error('Cannot create canvas context'); |
| 89 | } |
| 90 | this.ctx = ctx; |
| 91 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 92 | const clientRect = |
| 93 | assertExists(vnodeDom.dom.parentElement).getBoundingClientRect(); |
| 94 | this.parentWidth = clientRect.width; |
| 95 | this.parentHeight = clientRect.height; |
| 96 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 97 | this.readPanelHeightsFromDom(vnodeDom.dom); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 98 | (vnodeDom.dom as HTMLElement).style.height = `${this.totalPanelHeight}px`; |
| 99 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 100 | this.updateCanvasDimensions(); |
| 101 | this.repositionCanvas(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 102 | |
| 103 | // Save the resize handler in the state so we can remove it later. |
| 104 | // TODO: Encapsulate resize handling better. |
| 105 | this.onResize = () => { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 106 | this.readParentSizeFromDom(vnodeDom.dom); |
| 107 | this.updateCanvasDimensions(); |
| 108 | this.repositionCanvas(); |
Deepanjan Roy | 112ff6a | 2018-09-10 08:31:43 -0400 | [diff] [blame] | 109 | globals.rafScheduler.scheduleFullRedraw(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | // Once ResizeObservers are out, we can stop accessing the window here. |
| 113 | window.addEventListener('resize', this.onResize); |
| 114 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 115 | // TODO(dproy): Handle change in doesScroll attribute. |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 116 | if (this.attrs.doesScroll) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 117 | this.parentOnScroll = () => { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 118 | this.scrollTop = assertExists(vnodeDom.dom.parentElement).scrollTop; |
| 119 | this.repositionCanvas(); |
Deepanjan Roy | f190cb2 | 2018-08-28 10:43:07 -0400 | [diff] [blame] | 120 | globals.rafScheduler.scheduleRedraw(); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 121 | }; |
| 122 | vnodeDom.dom.parentElement!.addEventListener( |
| 123 | 'scroll', this.parentOnScroll, {passive: true}); |
| 124 | } |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 125 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 126 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 127 | onremove({attrs, dom}: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 128 | window.removeEventListener('resize', this.onResize); |
| 129 | globals.rafScheduler.removeRedrawCallback(this.canvasRedrawer); |
| 130 | if (attrs.doesScroll) { |
| 131 | dom.parentElement!.removeEventListener('scroll', this.parentOnScroll); |
| 132 | } |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 133 | perfDisplay.removeContainer(this); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 134 | } |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 135 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 136 | view({attrs}: m.CVnode<Attrs>) { |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 137 | this.attrs = attrs; |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 138 | const renderPanel = (panel: m.Vnode) => perfDebug() ? |
| 139 | m('.panel', panel, m('.debug-panel-border')) : |
| 140 | m('.panel', panel); |
| 141 | |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 142 | return m( |
| 143 | '.scroll-limiter', |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 144 | m('canvas.main-canvas'), |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 145 | attrs.panels.map(renderPanel)); |
Deepanjan Roy | abd79aa | 2018-08-28 07:29:15 -0400 | [diff] [blame] | 146 | } |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 147 | |
| 148 | onupdate(vnodeDom: m.CVnodeDOM<Attrs>) { |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 149 | const totalPanelHeightChanged = this.readPanelHeightsFromDom(vnodeDom.dom); |
| 150 | const parentSizeChanged = this.readParentSizeFromDom(vnodeDom.dom); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 151 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 152 | if (totalPanelHeightChanged) { |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 153 | (vnodeDom.dom as HTMLElement).style.height = `${this.totalPanelHeight}px`; |
| 154 | } |
| 155 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 156 | const canvasSizeShouldChange = |
| 157 | this.attrs.doesScroll ? parentSizeChanged : totalPanelHeightChanged; |
| 158 | if (canvasSizeShouldChange) { |
| 159 | this.updateCanvasDimensions(); |
| 160 | this.repositionCanvas(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 164 | private updateCanvasDimensions() { |
| 165 | this.canvasHeight = this.attrs.doesScroll ? |
| 166 | this.parentHeight * this.canvasOverdrawFactor : |
| 167 | this.totalPanelHeight; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 168 | const ctx = assertExists(this.ctx); |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 169 | const canvas = assertExists(ctx.canvas); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 170 | canvas.style.height = `${this.canvasHeight}px`; |
| 171 | const dpr = window.devicePixelRatio; |
| 172 | ctx.canvas.width = this.parentWidth * dpr; |
| 173 | ctx.canvas.height = this.canvasHeight * dpr; |
| 174 | ctx.scale(dpr, dpr); |
| 175 | } |
| 176 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 177 | private repositionCanvas() { |
| 178 | const canvas = assertExists(assertExists(this.ctx).canvas); |
| 179 | const canvasYStart = this.scrollTop - this.getCanvasOverdrawHeightPerSide(); |
| 180 | canvas.style.transform = `translateY(${canvasYStart}px)`; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Reads dimensions of parent node. Returns true if read dimensions are |
| 185 | * different from what was cached in the state. |
| 186 | */ |
| 187 | private readParentSizeFromDom(dom: Element): boolean { |
| 188 | const oldWidth = this.parentWidth; |
| 189 | const oldHeight = this.parentHeight; |
| 190 | const clientRect = assertExists(dom.parentElement).getBoundingClientRect(); |
| 191 | this.parentWidth = clientRect.width; |
| 192 | this.parentHeight = clientRect.height; |
| 193 | return this.parentHeight !== oldHeight || this.parentWidth !== oldWidth; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Reads dimensions of panels. Returns true if total panel height is different |
| 198 | * from what was cached in state. |
| 199 | */ |
| 200 | private readPanelHeightsFromDom(dom: Element): boolean { |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 201 | const prevHeight = this.totalPanelHeight; |
| 202 | this.panelHeights = []; |
| 203 | this.totalPanelHeight = 0; |
| 204 | |
Deepanjan Roy | 1a225c2 | 2018-10-04 07:33:02 -0400 | [diff] [blame^] | 205 | const panels = dom.querySelectorAll('.panel'); |
| 206 | assertTrue(panels.length === this.attrs.panels.length); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 207 | for (let i = 0; i < panels.length; i++) { |
| 208 | const height = panels[i].getBoundingClientRect().height; |
| 209 | this.panelHeights[i] = height; |
| 210 | this.totalPanelHeight += height; |
| 211 | } |
| 212 | |
| 213 | return this.totalPanelHeight !== prevHeight; |
| 214 | } |
| 215 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 216 | private overlapsCanvas(yStart: number, yEnd: number) { |
| 217 | return yEnd > 0 && yStart < this.canvasHeight; |
| 218 | } |
| 219 | |
| 220 | private redrawCanvas() { |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 221 | const redrawStart = debugNow(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 222 | if (!this.ctx) return; |
| 223 | this.ctx.clearRect(0, 0, this.parentWidth, this.canvasHeight); |
| 224 | const canvasYStart = this.scrollTop - this.getCanvasOverdrawHeightPerSide(); |
| 225 | |
| 226 | let panelYStart = 0; |
| 227 | const panels = assertExists(this.attrs).panels; |
| 228 | assertTrue(panels.length === this.panelHeights.length); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 229 | let totalOnCanvas = 0; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 230 | for (let i = 0; i < panels.length; i++) { |
| 231 | const panel = panels[i]; |
| 232 | const panelHeight = this.panelHeights[i]; |
| 233 | const yStartOnCanvas = panelYStart - canvasYStart; |
| 234 | |
| 235 | if (!this.overlapsCanvas(yStartOnCanvas, yStartOnCanvas + panelHeight)) { |
| 236 | panelYStart += panelHeight; |
| 237 | continue; |
| 238 | } |
| 239 | |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 240 | totalOnCanvas++; |
| 241 | |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 242 | if (!isPanelVNode(panel)) { |
| 243 | throw Error('Vnode passed to panel container is not a panel'); |
| 244 | } |
| 245 | |
| 246 | this.ctx.save(); |
| 247 | this.ctx.translate(0, yStartOnCanvas); |
| 248 | const clipRect = new Path2D(); |
| 249 | const size = {width: this.parentWidth, height: panelHeight}; |
| 250 | clipRect.rect(0, 0, size.width, size.height); |
| 251 | this.ctx.clip(clipRect); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 252 | const beforeRender = debugNow(); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 253 | panel.state.renderCanvas(this.ctx, size, panel); |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 254 | this.updatePanelStats( |
| 255 | i, panel.state, debugNow() - beforeRender, this.ctx, size); |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 256 | this.ctx.restore(); |
| 257 | panelYStart += panelHeight; |
| 258 | } |
Deepanjan Roy | 9a906ed | 2018-09-20 11:06:00 -0400 | [diff] [blame] | 259 | const redrawDur = debugNow() - redrawStart; |
| 260 | this.updatePerfStats(redrawDur, panels.length, totalOnCanvas); |
| 261 | } |
| 262 | |
| 263 | private updatePanelStats( |
| 264 | panelIndex: number, panel: Panel, renderTime: number, |
| 265 | ctx: CanvasRenderingContext2D, size: PanelSize) { |
| 266 | if (!perfDebug()) return; |
| 267 | let renderStats = this.panelPerfStats.get(panel); |
| 268 | if (renderStats === undefined) { |
| 269 | renderStats = new RunningStatistics(); |
| 270 | this.panelPerfStats.set(panel, renderStats); |
| 271 | } |
| 272 | renderStats.addValue(renderTime); |
| 273 | |
| 274 | const statW = 300; |
| 275 | ctx.fillStyle = 'hsl(97, 100%, 96%)'; |
| 276 | ctx.fillRect(size.width - statW, size.height - 20, statW, 20); |
| 277 | ctx.fillStyle = 'hsla(122, 77%, 22%)'; |
| 278 | const statStr = `Panel ${panelIndex + 1} | ` + runningStatStr(renderStats); |
| 279 | ctx.fillText(statStr, size.width - statW, size.height - 10); |
| 280 | } |
| 281 | |
| 282 | private updatePerfStats( |
| 283 | renderTime: number, totalPanels: number, panelsOnCanvas: number) { |
| 284 | if (!perfDebug()) return; |
| 285 | this.perfStats.renderStats.addValue(renderTime); |
| 286 | this.perfStats.totalPanels = totalPanels; |
| 287 | this.perfStats.panelsOnCanvas = panelsOnCanvas; |
| 288 | } |
| 289 | |
| 290 | renderPerfStats(index: number) { |
| 291 | assertTrue(perfDebug()); |
| 292 | return [m( |
| 293 | 'section', |
| 294 | m('div', `Panel Container ${index + 1}`), |
| 295 | m('div', |
| 296 | `${this.perfStats.totalPanels} panels, ` + |
| 297 | `${this.perfStats.panelsOnCanvas} on canvas.`), |
| 298 | m('div', runningStatStr(this.perfStats.renderStats)), )]; |
Deepanjan Roy | 1f658fe | 2018-09-11 08:38:17 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private getCanvasOverdrawHeightPerSide() { |
| 302 | const overdrawHeight = (this.canvasOverdrawFactor - 1) * this.parentHeight; |
| 303 | return overdrawHeight / 2; |
| 304 | } |
| 305 | } |