-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
canvas.ts
300 lines (251 loc) · 8 KB
/
canvas.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import {settings} from "core/settings"
import {logger} from "core/logging"
import * as p from "core/properties"
import {div, append} from "core/dom"
import {OutputBackend} from "core/enums"
import {UIEventBus} from "core/ui_events"
import {load_module} from "core/util/modules"
import {Context2d, CanvasLayer} from "core/util/canvas"
import {UIElement, UIElementView} from "../ui/ui_element"
import {type PlotView} from "../plots/plot"
import type {ReglWrapper} from "../glyphs/webgl/regl_wrap"
import {InlineStyleSheet, StyleSheetLike} from "core/dom"
import canvas_css from "styles/canvas.css"
export type FrameBox = [number, number, number, number]
// Notes on WebGL support:
// Glyps can be rendered into the original 2D canvas, or in a (hidden)
// webgl canvas that we create below. In this way, the rest of bokehjs
// can keep working as it is, and we can incrementally update glyphs to
// make them use GL.
//
// When the author or user wants to, we try to create a webgl canvas,
// which is saved on the ctx object that gets passed around during drawing.
// The presence (and not-being-false) of the this.glcanvas attribute is the
// marker that we use throughout that determines whether we have gl support.
export type WebGLState = {
readonly canvas: HTMLCanvasElement
readonly regl_wrapper: ReglWrapper
}
async function init_webgl(): Promise<WebGLState | null> {
// We use a global invisible canvas and gl context. By having a global context,
// we avoid the limitation of max 16 contexts that most browsers have.
const canvas = document.createElement("canvas")
const gl = canvas.getContext("webgl", {premultipliedAlpha: true})
// If WebGL is available, we store a reference to the ReGL wrapper on
// the ctx object, because that's what gets passed everywhere.
if (gl != null) {
const webgl = await load_module(import("../glyphs/webgl"))
if (webgl != null) {
const regl_wrapper = webgl.get_regl(gl)
if (regl_wrapper.has_webgl) {
return {canvas, regl_wrapper}
} else {
logger.trace("WebGL is supported, but not the required extensions")
}
} else {
logger.trace("WebGL is supported, but bokehjs(.min).js bundle is not available")
}
} else {
logger.trace("WebGL is not supported")
}
return null
}
const global_webgl: () => Promise<WebGLState | null> = (() => {
let _global_webgl: WebGLState | null | undefined
return async () => {
if (_global_webgl !== undefined)
return _global_webgl
else
return _global_webgl = await init_webgl()
}
})()
export class CanvasView extends UIElementView {
declare model: Canvas
webgl: WebGLState | null = null
underlays_el: HTMLElement
primary: CanvasLayer
overlays: CanvasLayer
overlays_el: HTMLElement
events_el: HTMLElement
ui_event_bus: UIEventBus
protected _size = new InlineStyleSheet()
override initialize(): void {
super.initialize()
this.underlays_el = div({class: "bk-layer"})
this.primary = this.create_layer()
this.overlays = this.create_layer()
this.overlays_el = div({class: "bk-layer"})
this.events_el = div({class: ["bk-layer", "bk-events"]})
this.ui_event_bus = new UIEventBus(this)
}
get layers(): (HTMLElement | CanvasLayer)[] {
return [
this.underlays_el,
this.primary,
this.overlays,
this.overlays_el,
this.events_el,
]
}
override async lazy_initialize(): Promise<void> {
await super.lazy_initialize()
if (this.model.output_backend == "webgl") {
this.webgl = await global_webgl()
if (settings.force_webgl && this.webgl == null)
throw new Error("webgl is not available")
}
}
override remove(): void {
this.ui_event_bus.destroy()
super.remove()
}
override stylesheets(): StyleSheetLike[] {
return [...super.stylesheets(), canvas_css, this._size]
}
override render(): void {
super.render()
const elements = [
this.underlays_el,
this.primary.el,
this.overlays.el,
this.overlays_el,
this.events_el,
]
append(this.shadow_el, ...elements)
}
add_underlay(el: HTMLElement): void {
this.underlays_el.appendChild(el)
}
add_overlay(el: HTMLElement): void {
this.overlays_el.appendChild(el)
}
add_event(el: HTMLElement): void {
this.events_el.appendChild(el)
}
get pixel_ratio(): number {
return this.primary.pixel_ratio // XXX: primary
}
override _update_bbox(): boolean {
const changed = super._update_bbox()
if (changed) {
const {width, height} = this.bbox
this._size.replace(`
.bk-layer {
width: ${width}px;
height: ${height}px;
}
`)
this.primary.resize(width, height)
this.overlays.resize(width, height)
}
return changed
}
override after_resize(): void {
if (this.plot_views.length != 0) {
// Canvas is being managed by a plot, thus it should not attempt
// self-resize, as it would result in inconsistent state and
// possibly invalid layout and/or lack of repaint of a plot.
return
} else {
super.after_resize()
}
}
override _after_resize(): void {
super._after_resize()
const {width, height} = this.bbox
this.primary.resize(width, height)
this.overlays.resize(width, height)
}
resize(): void {
this._update_bbox()
this._after_resize()
}
prepare_webgl(frame_box: FrameBox): void {
// Prepare WebGL for a drawing pass
const {webgl} = this
if (webgl != null) {
// Sync canvas size
const {width, height} = this.bbox
webgl.canvas.width = this.pixel_ratio*width
webgl.canvas.height = this.pixel_ratio*height
const [sx, sy, w, h] = frame_box
const {xview, yview} = this.bbox
const vx = xview.compute(sx)
const vy = yview.compute(sy + h)
const ratio = this.pixel_ratio
webgl.regl_wrapper.set_scissor(ratio*vx, ratio*vy, ratio*w, ratio*h)
this._clear_webgl()
}
}
blit_webgl(ctx: Context2d): void {
// This should be called when the ctx has no state except the HIDPI transform
const {webgl} = this
if (webgl != null) {
// Blit gl canvas into the 2D canvas. To do 1-on-1 blitting, we need
// to remove the hidpi transform, then blit, then restore.
// ctx.globalCompositeOperation = "source-over" -> OK; is the default
logger.debug("Blitting WebGL canvas")
ctx.restore()
ctx.drawImage(webgl.canvas, 0, 0)
// Set back hidpi transform
ctx.save()
if (this.model.hidpi) {
const ratio = this.pixel_ratio
ctx.scale(ratio, ratio)
ctx.translate(0.5, 0.5)
}
this._clear_webgl()
}
}
protected _clear_webgl(): void {
const {webgl} = this
if (webgl != null) {
// Prepare GL for drawing
const {regl_wrapper, canvas} = webgl
regl_wrapper.clear(canvas.width, canvas.height)
}
}
compose(): CanvasLayer {
const composite = this.create_layer()
const {width, height} = this.bbox
composite.resize(width, height)
composite.ctx.drawImage(this.primary.canvas, 0, 0)
composite.ctx.drawImage(this.overlays.canvas, 0, 0)
return composite
}
create_layer(): CanvasLayer {
const {output_backend, hidpi} = this.model
return new CanvasLayer(output_backend, hidpi)
}
to_blob(): Promise<Blob> {
return this.compose().to_blob()
}
plot_views: PlotView[] = []
/*
get plot_views(): PlotView[] {
return [] // XXX
}
*/
}
export namespace Canvas {
export type Attrs = p.AttrsOf<Props>
export type Props = UIElement.Props & {
hidpi: p.Property<boolean>
output_backend: p.Property<OutputBackend>
}
}
export interface Canvas extends Canvas.Attrs {}
export class Canvas extends UIElement {
declare properties: Canvas.Props
declare __view_type__: CanvasView
constructor(attrs?: Partial<Canvas.Attrs>) {
super(attrs)
}
static {
this.prototype.default_view = CanvasView
this.define<Canvas.Props>(({Boolean}) => ({
hidpi: [ Boolean, true ],
output_backend: [ OutputBackend, "canvas" ],
}))
}
}