2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2015 Samuel Mannehed for Cendio AB
5 * Licensed under MPL 2.0 (see LICENSE.txt)
7 * See README.md for usage and integration instructions.
10 /*jslint browser: true, white: false */
11 /*global Util, Base64, changeCursor */
13 import { browserSupportsCursorURIs as cursorURIsSupported
} from './util/browsers.js';
14 import { set_defaults
, make_properties
} from './util/properties.js';
15 import * as Log
from './util/logging.js';
16 import Base64
from "./base64.js";
18 export default function Display(defaults
) {
20 this._c_forceCanvas
= false;
22 this._renderQ
= []; // queue drawing actions for in-oder rendering
23 this._flushing
= false;
25 // the full frame buffer (logical canvas) size
29 this._prevDrawStyle
= "";
31 this._tile16x16
= null;
35 set_defaults(this, defaults
, {
39 "onFlush": function () {},
42 Log
.Debug(">> Display.constructor");
46 throw new Error("Target must be set");
49 if (typeof this._target
=== 'string') {
50 throw new Error('target must be a DOM element');
53 if (!this._target
.getContext
) {
54 throw new Error("no getContext method");
57 this._targetCtx
= this._target
.getContext('2d');
59 // the visible canvas viewport (i.e. what actually gets seen)
60 this._viewportLoc
= { 'x': 0, 'y': 0, 'w': this._target
.width
, 'h': this._target
.height
};
62 // The hidden canvas, where we do the actual rendering
63 this._backbuffer
= document
.createElement('canvas');
64 this._drawCtx
= this._backbuffer
.getContext('2d');
66 this._damageBounds
= { left
:0, top
:0,
67 right
: this._backbuffer
.width
,
68 bottom
: this._backbuffer
.height
};
70 Log
.Debug("User Agent: " + navigator
.userAgent
);
74 // Check canvas features
75 if ('createImageData' in this._drawCtx
) {
76 this._render_mode
= 'canvas rendering';
78 throw new Error("Canvas does not support createImageData");
81 if (this._prefer_js
=== null) {
82 Log
.Info("Prefering javascript operations");
83 this._prefer_js
= true;
86 // Determine browser support for setting the cursor via data URI scheme
87 if (this._cursor_uri
|| this._cursor_uri
=== null ||
88 this._cursor_uri
=== undefined) {
89 this._cursor_uri
= cursorURIsSupported();
92 Log
.Debug("<< Display.constructor");
95 var SUPPORTS_IMAGEDATA_CONSTRUCTOR
= false;
97 new ImageData(new Uint8ClampedArray(4), 1, 1);
98 SUPPORTS_IMAGEDATA_CONSTRUCTOR
= true;
103 Display
.prototype = {
105 viewportChangePos: function (deltaX
, deltaY
) {
106 var vp
= this._viewportLoc
;
107 deltaX
= Math
.floor(deltaX
);
108 deltaY
= Math
.floor(deltaY
);
110 if (!this._viewport
) {
111 deltaX
= -vp
.w
; // clamped later of out of bounds
115 var vx2
= vp
.x
+ vp
.w
- 1;
116 var vy2
= vp
.y
+ vp
.h
- 1;
120 if (deltaX
< 0 && vp
.x
+ deltaX
< 0) {
123 if (vx2
+ deltaX
>= this._fb_width
) {
124 deltaX
-= vx2
+ deltaX
- this._fb_width
+ 1;
127 if (vp
.y
+ deltaY
< 0) {
130 if (vy2
+ deltaY
>= this._fb_height
) {
131 deltaY
-= (vy2
+ deltaY
- this._fb_height
+ 1);
134 if (deltaX
=== 0 && deltaY
=== 0) {
137 Log
.Debug("viewportChange deltaX: " + deltaX
+ ", deltaY: " + deltaY
);
142 this._damage(vp
.x
, vp
.y
, vp
.w
, vp
.h
);
147 viewportChangeSize: function(width
, height
) {
149 if (!this._viewport
||
150 typeof(width
) === "undefined" ||
151 typeof(height
) === "undefined") {
153 Log
.Debug("Setting viewport to full display region");
154 width
= this._fb_width
;
155 height
= this._fb_height
;
158 if (width
> this._fb_width
) {
159 width
= this._fb_width
;
161 if (height
> this._fb_height
) {
162 height
= this._fb_height
;
165 var vp
= this._viewportLoc
;
166 if (vp
.w
!== width
|| vp
.h
!== height
) {
170 var 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 return x
/ this._scale
+ this._viewportLoc
.x
;
190 return y
/ this._scale
+ this._viewportLoc
.y
;
193 resize: function (width
, height
) {
194 this._prevDrawStyle
= "";
196 this._fb_width
= width
;
197 this._fb_height
= height
;
199 var canvas
= this._backbuffer
;
200 if (canvas
.width
!== width
|| canvas
.height
!== height
) {
202 // We have to save the canvas data since changing the size will clear it
204 if (canvas
.width
> 0 && canvas
.height
> 0) {
205 saveImg
= this._drawCtx
.getImageData(0, 0, canvas
.width
, canvas
.height
);
208 if (canvas
.width
!== width
) {
209 canvas
.width
= width
;
211 if (canvas
.height
!== height
) {
212 canvas
.height
= height
;
216 this._drawCtx
.putImageData(saveImg
, 0, 0);
220 // Readjust the viewport as it may be incorrectly sized
222 var vp
= this._viewportLoc
;
223 this.viewportChangeSize(vp
.w
, vp
.h
);
224 this.viewportChangePos(0, 0);
227 // Track what parts of the visible canvas that need updating
228 _damage: function(x
, y
, w
, h
) {
229 if (x
< this._damageBounds
.left
) {
230 this._damageBounds
.left
= x
;
232 if (y
< this._damageBounds
.top
) {
233 this._damageBounds
.top
= y
;
235 if ((x
+ w
) > this._damageBounds
.right
) {
236 this._damageBounds
.right
= x
+ w
;
238 if ((y
+ h
) > this._damageBounds
.bottom
) {
239 this._damageBounds
.bottom
= y
+ h
;
243 // Update the visible canvas with the contents of the
245 flip: function(from_queue
) {
246 if (this._renderQ
.length
!== 0 && !from_queue
) {
251 var x
, y
, vx
, vy
, w
, h
;
253 x
= this._damageBounds
.left
;
254 y
= this._damageBounds
.top
;
255 w
= this._damageBounds
.right
- x
;
256 h
= this._damageBounds
.bottom
- y
;
258 vx
= x
- this._viewportLoc
.x
;
259 vy
= y
- this._viewportLoc
.y
;
272 if ((vx
+ w
) > this._viewportLoc
.w
) {
273 w
= this._viewportLoc
.w
- vx
;
275 if ((vy
+ h
) > this._viewportLoc
.h
) {
276 h
= this._viewportLoc
.h
- vy
;
279 if ((w
> 0) && (h
> 0)) {
280 // FIXME: We may need to disable image smoothing here
281 // as well (see copyImage()), but we haven't
282 // noticed any problem yet.
283 this._targetCtx
.drawImage(this._backbuffer
,
288 this._damageBounds
.left
= this._damageBounds
.top
= 65535;
289 this._damageBounds
.right
= this._damageBounds
.bottom
= 0;
295 this.resize(this._logo
.width
, this._logo
.height
);
296 this.imageRect(0, 0, this._logo
.type
, this._logo
.data
);
298 this.resize(240, 20);
299 this._drawCtx
.clearRect(0, 0, this._fb_width
, this._fb_height
);
304 pending: function() {
305 return this._renderQ
.length
> 0;
309 if (this._renderQ
.length
=== 0) {
312 this._flushing
= true;
316 fillRect: function (x
, y
, width
, height
, color
, from_queue
) {
317 if (this._renderQ
.length
!== 0 && !from_queue
) {
327 this._setFillColor(color
);
328 this._drawCtx
.fillRect(x
, y
, width
, height
);
329 this._damage(x
, y
, width
, height
);
333 copyImage: function (old_x
, old_y
, new_x
, new_y
, w
, h
, from_queue
) {
334 if (this._renderQ
.length
!== 0 && !from_queue
) {
345 // Due to this bug among others [1] we need to disable the image-smoothing to
346 // avoid getting a blur effect when copying data.
348 // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
350 // We need to set these every time since all properties are reset
351 // when the the size is changed
352 this._drawCtx
.mozImageSmoothingEnabled
= false;
353 this._drawCtx
.webkitImageSmoothingEnabled
= false;
354 this._drawCtx
.msImageSmoothingEnabled
= false;
355 this._drawCtx
.imageSmoothingEnabled
= false;
357 this._drawCtx
.drawImage(this._backbuffer
,
360 this._damage(new_x
, new_y
, w
, h
);
364 imageRect: function(x
, y
, mime
, arr
) {
365 var img
= new Image();
366 img
.src
= "data: " + mime
+ ";base64," + Base64
.encode(arr
);
375 // start updating a tile
376 startTile: function (x
, y
, width
, height
, color
) {
379 if (width
=== 16 && height
=== 16) {
380 this._tile
= this._tile16x16
;
382 this._tile
= this._drawCtx
.createImageData(width
, height
);
385 if (this._prefer_js
) {
387 var green
= color
[1];
390 var data
= this._tile
.data
;
391 for (var i
= 0; i
< width
* height
* 4; i
+= 4) {
398 this.fillRect(x
, y
, width
, height
, color
, true);
402 // update sub-rectangle of the current tile
403 subTile: function (x
, y
, w
, h
, color
) {
404 if (this._prefer_js
) {
406 var green
= color
[1];
411 var data
= this._tile
.data
;
412 var width
= this._tile
.width
;
413 for (var j
= y
; j
< yend
; j
++) {
414 for (var i
= x
; i
< xend
; i
++) {
415 var p
= (i
+ (j
* width
)) * 4;
423 this.fillRect(this._tile_x
+ x
, this._tile_y
+ y
, w
, h
, color
, true);
427 // draw the current tile to the screen
428 finishTile: function () {
429 if (this._prefer_js
) {
430 this._drawCtx
.putImageData(this._tile
, this._tile_x
, this._tile_y
);
431 this._damage(this._tile_x
, this._tile_y
,
432 this._tile
.width
, this._tile
.height
);
434 // else: No-op -- already done by setSubTile
437 blitImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
438 if (this._renderQ
.length
!== 0 && !from_queue
) {
439 // NB(directxman12): it's technically more performant here to use preallocated arrays,
440 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
441 // this probably isn't getting called *nearly* as much
442 var new_arr
= new Uint8Array(width
* height
* 4);
443 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
453 this._bgrxImageData(x
, y
, width
, height
, arr
, offset
);
457 blitRgbImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
458 if (this._renderQ
.length
!== 0 && !from_queue
) {
459 // NB(directxman12): it's technically more performant here to use preallocated arrays,
460 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
461 // this probably isn't getting called *nearly* as much
462 var new_arr
= new Uint8Array(width
* height
* 3);
463 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
473 this._rgbImageData(x
, y
, width
, height
, arr
, offset
);
477 blitRgbxImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
478 if (this._renderQ
.length
!== 0 && !from_queue
) {
479 // NB(directxman12): it's technically more performant here to use preallocated arrays,
480 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
481 // this probably isn't getting called *nearly* as much
482 var new_arr
= new Uint8Array(width
* height
* 4);
483 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
493 this._rgbxImageData(x
, y
, width
, height
, arr
, offset
);
497 drawImage: function (img
, x
, y
) {
498 this._drawCtx
.drawImage(img
, x
, y
);
499 this._damage(x
, y
, img
.width
, img
.height
);
502 changeCursor: function (pixels
, mask
, hotx
, hoty
, w
, h
) {
503 if (this._cursor_uri
=== false) {
504 Log
.Warn("changeCursor called but no cursor data URI support");
508 Display
.changeCursor(this._target
, pixels
, mask
, hotx
, hoty
, w
, h
);
511 defaultCursor: function () {
512 this._target
.style
.cursor
= "default";
515 disableLocalCursor: function () {
516 this._target
.style
.cursor
= "none";
519 clippingDisplay: function () {
520 var vp
= this._viewportLoc
;
521 return this._fb_width
> vp
.w
|| this._fb_height
> vp
.h
;
524 // Overridden getters/setters
525 set_scale: function (scale
) {
526 this._rescale(scale
);
529 set_viewport: function (viewport
) {
530 this._viewport
= viewport
;
531 // May need to readjust the viewport dimensions
532 var vp
= this._viewportLoc
;
533 this.viewportChangeSize(vp
.w
, vp
.h
);
534 this.viewportChangePos(0, 0);
537 get_width: function () {
538 return this._fb_width
;
540 get_height: function () {
541 return this._fb_height
;
544 autoscale: function (containerWidth
, containerHeight
, downscaleOnly
) {
545 var vp
= this._viewportLoc
;
546 var targetAspectRatio
= containerWidth
/ containerHeight
;
547 var fbAspectRatio
= vp
.w
/ vp
.h
;
550 if (fbAspectRatio
>= targetAspectRatio
) {
551 scaleRatio
= containerWidth
/ vp
.w
;
553 scaleRatio
= containerHeight
/ vp
.h
;
556 if (scaleRatio
> 1.0 && downscaleOnly
) {
560 this._rescale(scaleRatio
);
564 _rescale: function (factor
) {
565 this._scale
= factor
;
566 var vp
= this._viewportLoc
;
568 // NB(directxman12): If you set the width directly, or set the
569 // style width to a number, the canvas is cleared.
570 // However, if you set the style width to a string
571 // ('NNNpx'), the canvas is scaled without clearing.
572 var width
= Math
.round(factor
* vp
.w
) + 'px';
573 var height
= Math
.round(factor
* vp
.h
) + 'px';
575 if ((this._target
.style
.width
!== width
) ||
576 (this._target
.style
.height
!== height
)) {
577 this._target
.style
.width
= width
;
578 this._target
.style
.height
= height
;
582 _setFillColor: function (color
) {
583 var newStyle
= 'rgb(' + color
[2] + ',' + color
[1] + ',' + color
[0] + ')';
584 if (newStyle
!== this._prevDrawStyle
) {
585 this._drawCtx
.fillStyle
= newStyle
;
586 this._prevDrawStyle
= newStyle
;
590 _rgbImageData: function (x
, y
, width
, height
, arr
, offset
) {
591 var img
= this._drawCtx
.createImageData(width
, height
);
593 for (var i
= 0, j
= offset
; i
< width
* height
* 4; i
+= 4, j
+= 3) {
595 data
[i
+ 1] = arr
[j
+ 1];
596 data
[i
+ 2] = arr
[j
+ 2];
597 data
[i
+ 3] = 255; // Alpha
599 this._drawCtx
.putImageData(img
, x
, y
);
600 this._damage(x
, y
, img
.width
, img
.height
);
603 _bgrxImageData: function (x
, y
, width
, height
, arr
, offset
) {
604 var img
= this._drawCtx
.createImageData(width
, height
);
606 for (var i
= 0, j
= offset
; i
< width
* height
* 4; i
+= 4, j
+= 4) {
607 data
[i
] = arr
[j
+ 2];
608 data
[i
+ 1] = arr
[j
+ 1];
609 data
[i
+ 2] = arr
[j
];
610 data
[i
+ 3] = 255; // Alpha
612 this._drawCtx
.putImageData(img
, x
, y
);
613 this._damage(x
, y
, img
.width
, img
.height
);
616 _rgbxImageData: function (x
, y
, width
, height
, arr
, offset
) {
617 // NB(directxman12): arr must be an Type Array view
619 if (SUPPORTS_IMAGEDATA_CONSTRUCTOR
) {
620 img
= new ImageData(new Uint8ClampedArray(arr
.buffer
, arr
.byteOffset
, width
* height
* 4), width
, height
);
622 img
= this._drawCtx
.createImageData(width
, height
);
623 img
.data
.set(new Uint8ClampedArray(arr
.buffer
, arr
.byteOffset
, width
* height
* 4));
625 this._drawCtx
.putImageData(img
, x
, y
);
626 this._damage(x
, y
, img
.width
, img
.height
);
629 _renderQ_push: function (action
) {
630 this._renderQ
.push(action
);
631 if (this._renderQ
.length
=== 1) {
632 // If this can be rendered immediately it will be, otherwise
633 // the scanner will wait for the relevant event
634 this._scan_renderQ();
638 _resume_renderQ: function() {
639 // "this" is the object that is ready, not the
641 this.removeEventListener('load', this._noVNC_display
._resume_renderQ
);
642 this._noVNC_display
._scan_renderQ();
645 _scan_renderQ: function () {
647 while (ready
&& this._renderQ
.length
> 0) {
648 var a
= this._renderQ
[0];
654 this.copyImage(a
.old_x
, a
.old_y
, a
.x
, a
.y
, a
.width
, a
.height
, true);
657 this.fillRect(a
.x
, a
.y
, a
.width
, a
.height
, a
.color
, true);
660 this.blitImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
663 this.blitRgbImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
666 this.blitRgbxImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
669 if (a
.img
.complete
) {
670 this.drawImage(a
.img
, a
.x
, a
.y
);
672 a
.img
._noVNC_display
= this;
673 a
.img
.addEventListener('load', this._resume_renderQ
);
674 // We need to wait for this image to 'load'
675 // to keep things in-order
682 this._renderQ
.shift();
686 if (this._renderQ
.length
=== 0 && this._flushing
) {
687 this._flushing
= false;
693 make_properties(Display
, [
694 ['target', 'wo', 'dom'], // Canvas element for rendering
695 ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
696 ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "type": mime-type, "data": data}
697 ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
698 ['viewport', 'rw', 'bool'], // Use viewport clipping
699 ['width', 'ro', 'int'], // Display area width
700 ['height', 'ro', 'int'], // Display area height
702 ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
704 ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
705 ['cursor_uri', 'rw', 'raw'], // Can we render cursor using data URI
707 ['onFlush', 'rw', 'func'], // onFlush(): A flush request has finished
711 Display
.changeCursor = function (target
, pixels
, mask
, hotx
, hoty
, w
, h
) {
712 if ((w
=== 0) || (h
=== 0)) {
713 target
.style
.cursor
= 'none';
719 for (y
= 0; y
< h
; y
++) {
720 for (x
= 0; x
< w
; x
++) {
721 var idx
= y
* Math
.ceil(w
/ 8) + Math
.floor(x
/ 8);
722 var alpha
= (mask
[idx
] << (x
% 8)) & 0x80 ? 255 : 0;
723 idx
= ((w
* y
) + x
) * 4;
724 cur
.push(pixels
[idx
+ 2]); // red
725 cur
.push(pixels
[idx
+ 1]); // green
726 cur
.push(pixels
[idx
]); // blue
727 cur
.push(alpha
); // alpha
731 var canvas
= document
.createElement('canvas');
732 var ctx
= canvas
.getContext('2d');
738 if (SUPPORTS_IMAGEDATA_CONSTRUCTOR
) {
739 img
= new ImageData(new Uint8ClampedArray(cur
), w
, h
);
741 img
= ctx
.createImageData(w
, h
);
742 img
.data
.set(new Uint8ClampedArray(cur
));
744 ctx
.clearRect(0, 0, w
, h
);
745 ctx
.putImageData(img
, 0, 0);
747 var url
= canvas
.toDataURL();
748 target
.style
.cursor
= 'url(' + url
+ ')' + hotx
+ ' ' + hoty
+ ', default';