2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
9 import * as Log
from './util/logging.js';
10 import Base64
from "./base64.js";
11 import { supportsImageMetadata
} from './util/browser.js';
12 import { toSigned32bit
} from './util/int.js';
14 export default class Display
{
18 this._renderQ
= []; // queue drawing actions for in-oder rendering
19 this._flushing
= false;
21 // the full frame buffer (logical canvas) size
25 this._prevDrawStyle
= "";
27 Log
.Debug(">> Display.constructor");
30 this._target
= target
;
33 throw new Error("Target must be set");
36 if (typeof this._target
=== 'string') {
37 throw new Error('target must be a DOM element');
40 if (!this._target
.getContext
) {
41 throw new Error("no getContext method");
44 this._targetCtx
= this._target
.getContext('2d');
46 // the visible canvas viewport (i.e. what actually gets seen)
47 this._viewportLoc
= { 'x': 0, 'y': 0, 'w': this._target
.width
, 'h': this._target
.height
};
49 // The hidden canvas, where we do the actual rendering
50 this._backbuffer
= document
.createElement('canvas');
51 this._drawCtx
= this._backbuffer
.getContext('2d');
53 this._damageBounds
= { left
: 0, top
: 0,
54 right
: this._backbuffer
.width
,
55 bottom
: this._backbuffer
.height
};
57 Log
.Debug("User Agent: " + navigator
.userAgent
);
59 // Check canvas features
60 if (!('createImageData' in this._drawCtx
)) {
61 throw new Error("Canvas does not support createImageData");
64 Log
.Debug("<< Display.constructor");
66 // ===== PROPERTIES =====
69 this._clipViewport
= false;
71 // ===== EVENT HANDLERS =====
73 this.onflush
= () => {}; // A flush request has finished
76 // ===== PROPERTIES =====
78 get scale() { return this._scale
; }
83 get clipViewport() { return this._clipViewport
; }
84 set clipViewport(viewport
) {
85 this._clipViewport
= viewport
;
86 // May need to readjust the viewport dimensions
87 const vp
= this._viewportLoc
;
88 this.viewportChangeSize(vp
.w
, vp
.h
);
89 this.viewportChangePos(0, 0);
97 return this._fbHeight
;
100 // ===== PUBLIC METHODS =====
102 viewportChangePos(deltaX
, deltaY
) {
103 const vp
= this._viewportLoc
;
104 deltaX
= Math
.floor(deltaX
);
105 deltaY
= Math
.floor(deltaY
);
107 if (!this._clipViewport
) {
108 deltaX
= -vp
.w
; // clamped later of out of bounds
112 const vx2
= vp
.x
+ vp
.w
- 1;
113 const vy2
= vp
.y
+ vp
.h
- 1;
117 if (deltaX
< 0 && vp
.x
+ deltaX
< 0) {
120 if (vx2
+ deltaX
>= this._fbWidth
) {
121 deltaX
-= vx2
+ deltaX
- this._fbWidth
+ 1;
124 if (vp
.y
+ deltaY
< 0) {
127 if (vy2
+ deltaY
>= this._fbHeight
) {
128 deltaY
-= (vy2
+ deltaY
- this._fbHeight
+ 1);
131 if (deltaX
=== 0 && deltaY
=== 0) {
134 Log
.Debug("viewportChange deltaX: " + deltaX
+ ", deltaY: " + deltaY
);
139 this._damage(vp
.x
, vp
.y
, vp
.w
, vp
.h
);
144 viewportChangeSize(width
, height
) {
146 if (!this._clipViewport
||
147 typeof(width
) === "undefined" ||
148 typeof(height
) === "undefined") {
150 Log
.Debug("Setting viewport to full display region");
151 width
= this._fbWidth
;
152 height
= this._fbHeight
;
155 width
= Math
.floor(width
);
156 height
= Math
.floor(height
);
158 if (width
> this._fbWidth
) {
159 width
= this._fbWidth
;
161 if (height
> this._fbHeight
) {
162 height
= this._fbHeight
;
165 const vp
= this._viewportLoc
;
166 if (vp
.w
!== width
|| vp
.h
!== height
) {
170 const canvas
= this._target
;
171 canvas
.width
= width
;
172 canvas
.height
= height
;
174 // The position might need to be updated if we've grown
175 this.viewportChangePos(0, 0);
177 this._damage(vp
.x
, vp
.y
, vp
.w
, vp
.h
);
180 // Update the visible size of the target canvas
181 this._rescale(this._scale
);
186 if (this._scale
=== 0) {
189 return toSigned32bit(x
/ this._scale
+ this._viewportLoc
.x
);
193 if (this._scale
=== 0) {
196 return toSigned32bit(y
/ this._scale
+ this._viewportLoc
.y
);
199 resize(width
, height
) {
200 this._prevDrawStyle
= "";
202 this._fbWidth
= width
;
203 this._fbHeight
= height
;
205 const canvas
= this._backbuffer
;
206 if (canvas
.width
!== width
|| canvas
.height
!== height
) {
208 // We have to save the canvas data since changing the size will clear it
210 if (canvas
.width
> 0 && canvas
.height
> 0) {
211 saveImg
= this._drawCtx
.getImageData(0, 0, canvas
.width
, canvas
.height
);
214 if (canvas
.width
!== width
) {
215 canvas
.width
= width
;
217 if (canvas
.height
!== height
) {
218 canvas
.height
= height
;
222 this._drawCtx
.putImageData(saveImg
, 0, 0);
226 // Readjust the viewport as it may be incorrectly sized
228 const vp
= this._viewportLoc
;
229 this.viewportChangeSize(vp
.w
, vp
.h
);
230 this.viewportChangePos(0, 0);
233 // Track what parts of the visible canvas that need updating
234 _damage(x
, y
, w
, h
) {
235 if (x
< this._damageBounds
.left
) {
236 this._damageBounds
.left
= x
;
238 if (y
< this._damageBounds
.top
) {
239 this._damageBounds
.top
= y
;
241 if ((x
+ w
) > this._damageBounds
.right
) {
242 this._damageBounds
.right
= x
+ w
;
244 if ((y
+ h
) > this._damageBounds
.bottom
) {
245 this._damageBounds
.bottom
= y
+ h
;
249 // Update the visible canvas with the contents of the
252 if (this._renderQ
.length
!== 0 && !fromQueue
) {
257 let x
= this._damageBounds
.left
;
258 let y
= this._damageBounds
.top
;
259 let w
= this._damageBounds
.right
- x
;
260 let h
= this._damageBounds
.bottom
- y
;
262 let vx
= x
- this._viewportLoc
.x
;
263 let vy
= y
- this._viewportLoc
.y
;
276 if ((vx
+ w
) > this._viewportLoc
.w
) {
277 w
= this._viewportLoc
.w
- vx
;
279 if ((vy
+ h
) > this._viewportLoc
.h
) {
280 h
= this._viewportLoc
.h
- vy
;
283 if ((w
> 0) && (h
> 0)) {
284 // FIXME: We may need to disable image smoothing here
285 // as well (see copyImage()), but we haven't
286 // noticed any problem yet.
287 this._targetCtx
.drawImage(this._backbuffer
,
292 this._damageBounds
.left
= this._damageBounds
.top
= 65535;
293 this._damageBounds
.right
= this._damageBounds
.bottom
= 0;
298 return this._renderQ
.length
> 0;
302 if (this._renderQ
.length
=== 0) {
305 this._flushing
= true;
309 fillRect(x
, y
, width
, height
, color
, fromQueue
) {
310 if (this._renderQ
.length
!== 0 && !fromQueue
) {
320 this._setFillColor(color
);
321 this._drawCtx
.fillRect(x
, y
, width
, height
);
322 this._damage(x
, y
, width
, height
);
326 copyImage(oldX
, oldY
, newX
, newY
, w
, h
, fromQueue
) {
327 if (this._renderQ
.length
!== 0 && !fromQueue
) {
338 // Due to this bug among others [1] we need to disable the image-smoothing to
339 // avoid getting a blur effect when copying data.
341 // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
343 // We need to set these every time since all properties are reset
344 // when the the size is changed
345 this._drawCtx
.mozImageSmoothingEnabled
= false;
346 this._drawCtx
.webkitImageSmoothingEnabled
= false;
347 this._drawCtx
.msImageSmoothingEnabled
= false;
348 this._drawCtx
.imageSmoothingEnabled
= false;
350 this._drawCtx
.drawImage(this._backbuffer
,
353 this._damage(newX
, newY
, w
, h
);
357 imageRect(x
, y
, width
, height
, mime
, arr
) {
358 /* The internal logic cannot handle empty images, so bail early */
359 if ((width
=== 0) || (height
=== 0)) {
363 const img
= new Image();
364 img
.src
= "data: " + mime
+ ";base64," + Base64
.encode(arr
);
376 blitImage(x
, y
, width
, height
, arr
, offset
, fromQueue
) {
377 if (this._renderQ
.length
!== 0 && !fromQueue
) {
378 // NB(directxman12): it's technically more performant here to use preallocated arrays,
379 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
380 // this probably isn't getting called *nearly* as much
381 const newArr
= new Uint8Array(width
* height
* 4);
382 newArr
.set(new Uint8Array(arr
.buffer
, 0, newArr
.length
));
392 // NB(directxman12): arr must be an Type Array view
393 let data
= new Uint8ClampedArray(arr
.buffer
,
394 arr
.byteOffset
+ offset
,
397 if (supportsImageMetadata
) {
398 img
= new ImageData(data
, width
, height
);
400 img
= this._drawCtx
.createImageData(width
, height
);
403 this._drawCtx
.putImageData(img
, x
, y
);
404 this._damage(x
, y
, width
, height
);
408 drawImage(img
, x
, y
) {
409 this._drawCtx
.drawImage(img
, x
, y
);
410 this._damage(x
, y
, img
.width
, img
.height
);
413 autoscale(containerWidth
, containerHeight
) {
416 if (containerWidth
=== 0 || containerHeight
=== 0) {
421 const vp
= this._viewportLoc
;
422 const targetAspectRatio
= containerWidth
/ containerHeight
;
423 const fbAspectRatio
= vp
.w
/ vp
.h
;
425 if (fbAspectRatio
>= targetAspectRatio
) {
426 scaleRatio
= containerWidth
/ vp
.w
;
428 scaleRatio
= containerHeight
/ vp
.h
;
432 this._rescale(scaleRatio
);
435 // ===== PRIVATE METHODS =====
438 this._scale
= factor
;
439 const vp
= this._viewportLoc
;
441 // NB(directxman12): If you set the width directly, or set the
442 // style width to a number, the canvas is cleared.
443 // However, if you set the style width to a string
444 // ('NNNpx'), the canvas is scaled without clearing.
445 const width
= factor
* vp
.w
+ 'px';
446 const height
= factor
* vp
.h
+ 'px';
448 if ((this._target
.style
.width
!== width
) ||
449 (this._target
.style
.height
!== height
)) {
450 this._target
.style
.width
= width
;
451 this._target
.style
.height
= height
;
455 _setFillColor(color
) {
456 const newStyle
= 'rgb(' + color
[0] + ',' + color
[1] + ',' + color
[2] + ')';
457 if (newStyle
!== this._prevDrawStyle
) {
458 this._drawCtx
.fillStyle
= newStyle
;
459 this._prevDrawStyle
= newStyle
;
463 _renderQPush(action
) {
464 this._renderQ
.push(action
);
465 if (this._renderQ
.length
=== 1) {
466 // If this can be rendered immediately it will be, otherwise
467 // the scanner will wait for the relevant event
473 // "this" is the object that is ready, not the
475 this.removeEventListener('load', this._noVNCDisplay
._resumeRenderQ
);
476 this._noVNCDisplay
._scanRenderQ();
481 while (ready
&& this._renderQ
.length
> 0) {
482 const a
= this._renderQ
[0];
488 this.copyImage(a
.oldX
, a
.oldY
, a
.x
, a
.y
, a
.width
, a
.height
, true);
491 this.fillRect(a
.x
, a
.y
, a
.width
, a
.height
, a
.color
, true);
494 this.blitImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
497 if (a
.img
.complete
) {
498 if (a
.img
.width
!== a
.width
|| a
.img
.height
!== a
.height
) {
499 Log
.Error("Decoded image has incorrect dimensions. Got " +
500 a
.img
.width
+ "x" + a
.img
.height
+ ". Expected " +
501 a
.width
+ "x" + a
.height
+ ".");
504 this.drawImage(a
.img
, a
.x
, a
.y
);
506 a
.img
._noVNCDisplay
= this;
507 a
.img
.addEventListener('load', this._resumeRenderQ
);
508 // We need to wait for this image to 'load'
509 // to keep things in-order
516 this._renderQ
.shift();
520 if (this._renderQ
.length
=== 0 && this._flushing
) {
521 this._flushing
= false;