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
, {
41 "onFlush": function () {},
44 Log
.Debug(">> Display.constructor");
48 throw new Error("Target must be set");
51 if (typeof this._target
=== 'string') {
52 throw new Error('target must be a DOM element');
55 if (!this._target
.getContext
) {
56 throw new Error("no getContext method");
59 this._targetCtx
= this._target
.getContext('2d');
61 // the visible canvas viewport (i.e. what actually gets seen)
62 this._viewportLoc
= { 'x': 0, 'y': 0, 'w': this._target
.width
, 'h': this._target
.height
};
64 // The hidden canvas, where we do the actual rendering
65 this._backbuffer
= document
.createElement('canvas');
66 this._drawCtx
= this._backbuffer
.getContext('2d');
68 this._damageBounds
= { left
:0, top
:0,
69 right
: this._backbuffer
.width
,
70 bottom
: this._backbuffer
.height
};
72 Log
.Debug("User Agent: " + navigator
.userAgent
);
76 // Check canvas features
77 if ('createImageData' in this._drawCtx
) {
78 this._render_mode
= 'canvas rendering';
80 throw new Error("Canvas does not support createImageData");
83 if (this._prefer_js
=== null) {
84 Log
.Info("Prefering javascript operations");
85 this._prefer_js
= true;
88 // Determine browser support for setting the cursor via data URI scheme
89 if (this._cursor_uri
|| this._cursor_uri
=== null ||
90 this._cursor_uri
=== undefined) {
91 this._cursor_uri
= cursorURIsSupported();
94 Log
.Debug("<< Display.constructor");
97 var SUPPORTS_IMAGEDATA_CONSTRUCTOR
= false;
99 new ImageData(new Uint8ClampedArray(4), 1, 1);
100 SUPPORTS_IMAGEDATA_CONSTRUCTOR
= true;
105 Display
.prototype = {
107 viewportChangePos: function (deltaX
, deltaY
) {
108 var vp
= this._viewportLoc
;
109 deltaX
= Math
.floor(deltaX
);
110 deltaY
= Math
.floor(deltaY
);
112 if (!this._viewport
) {
113 deltaX
= -vp
.w
; // clamped later of out of bounds
117 var vx2
= vp
.x
+ vp
.w
- 1;
118 var vy2
= vp
.y
+ vp
.h
- 1;
122 if (deltaX
< 0 && vp
.x
+ deltaX
< 0) {
125 if (vx2
+ deltaX
>= this._fb_width
) {
126 deltaX
-= vx2
+ deltaX
- this._fb_width
+ 1;
129 if (vp
.y
+ deltaY
< 0) {
132 if (vy2
+ deltaY
>= this._fb_height
) {
133 deltaY
-= (vy2
+ deltaY
- this._fb_height
+ 1);
136 if (deltaX
=== 0 && deltaY
=== 0) {
139 Log
.Debug("viewportChange deltaX: " + deltaX
+ ", deltaY: " + deltaY
);
144 this._damage(vp
.x
, vp
.y
, vp
.w
, vp
.h
);
149 viewportChangeSize: function(width
, height
) {
151 if (!this._viewport
||
152 typeof(width
) === "undefined" ||
153 typeof(height
) === "undefined") {
155 Log
.Debug("Setting viewport to full display region");
156 width
= this._fb_width
;
157 height
= this._fb_height
;
160 if (width
> this._fb_width
) {
161 width
= this._fb_width
;
163 if (height
> this._fb_height
) {
164 height
= this._fb_height
;
167 var vp
= this._viewportLoc
;
168 if (vp
.w
!== width
|| vp
.h
!== height
) {
172 var canvas
= this._target
;
173 canvas
.width
= width
;
174 canvas
.height
= height
;
176 // The position might need to be updated if we've grown
177 this.viewportChangePos(0, 0);
179 this._damage(vp
.x
, vp
.y
, vp
.w
, vp
.h
);
182 // Update the visible size of the target canvas
183 this._rescale(this._scale
);
188 return x
/ this._scale
+ this._viewportLoc
.x
;
192 return y
/ this._scale
+ this._viewportLoc
.y
;
195 resize: function (width
, height
) {
196 this._prevDrawStyle
= "";
198 this._fb_width
= width
;
199 this._fb_height
= height
;
201 var canvas
= this._backbuffer
;
202 if (canvas
.width
!== width
|| canvas
.height
!== height
) {
204 // We have to save the canvas data since changing the size will clear it
206 if (canvas
.width
> 0 && canvas
.height
> 0) {
207 saveImg
= this._drawCtx
.getImageData(0, 0, canvas
.width
, canvas
.height
);
210 if (canvas
.width
!== width
) {
211 canvas
.width
= width
;
213 if (canvas
.height
!== height
) {
214 canvas
.height
= height
;
218 this._drawCtx
.putImageData(saveImg
, 0, 0);
222 // Readjust the viewport as it may be incorrectly sized
224 var vp
= this._viewportLoc
;
225 this.viewportChangeSize(vp
.w
, vp
.h
);
226 this.viewportChangePos(0, 0);
229 // Track what parts of the visible canvas that need updating
230 _damage: function(x
, y
, w
, h
) {
231 if (x
< this._damageBounds
.left
) {
232 this._damageBounds
.left
= x
;
234 if (y
< this._damageBounds
.top
) {
235 this._damageBounds
.top
= y
;
237 if ((x
+ w
) > this._damageBounds
.right
) {
238 this._damageBounds
.right
= x
+ w
;
240 if ((y
+ h
) > this._damageBounds
.bottom
) {
241 this._damageBounds
.bottom
= y
+ h
;
245 // Update the visible canvas with the contents of the
247 flip: function(from_queue
) {
248 if (this._renderQ
.length
!== 0 && !from_queue
) {
253 var x
, y
, vx
, vy
, w
, h
;
255 x
= this._damageBounds
.left
;
256 y
= this._damageBounds
.top
;
257 w
= this._damageBounds
.right
- x
;
258 h
= this._damageBounds
.bottom
- y
;
260 vx
= x
- this._viewportLoc
.x
;
261 vy
= y
- this._viewportLoc
.y
;
274 if ((vx
+ w
) > this._viewportLoc
.w
) {
275 w
= this._viewportLoc
.w
- vx
;
277 if ((vy
+ h
) > this._viewportLoc
.h
) {
278 h
= this._viewportLoc
.h
- vy
;
281 if ((w
> 0) && (h
> 0)) {
282 // FIXME: We may need to disable image smoothing here
283 // as well (see copyImage()), but we haven't
284 // noticed any problem yet.
285 this._targetCtx
.drawImage(this._backbuffer
,
290 this._damageBounds
.left
= this._damageBounds
.top
= 65535;
291 this._damageBounds
.right
= this._damageBounds
.bottom
= 0;
297 this.resize(this._logo
.width
, this._logo
.height
);
298 this.imageRect(0, 0, this._logo
.type
, this._logo
.data
);
300 this.resize(240, 20);
301 this._drawCtx
.clearRect(0, 0, this._fb_width
, this._fb_height
);
306 pending: function() {
307 return this._renderQ
.length
> 0;
311 if (this._renderQ
.length
=== 0) {
314 this._flushing
= true;
318 fillRect: function (x
, y
, width
, height
, color
, from_queue
) {
319 if (this._renderQ
.length
!== 0 && !from_queue
) {
329 this._setFillColor(color
);
330 this._drawCtx
.fillRect(x
, y
, width
, height
);
331 this._damage(x
, y
, width
, height
);
335 copyImage: function (old_x
, old_y
, new_x
, new_y
, w
, h
, from_queue
) {
336 if (this._renderQ
.length
!== 0 && !from_queue
) {
347 // Due to this bug among others [1] we need to disable the image-smoothing to
348 // avoid getting a blur effect when copying data.
350 // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
352 // We need to set these every time since all properties are reset
353 // when the the size is changed
354 this._drawCtx
.mozImageSmoothingEnabled
= false;
355 this._drawCtx
.webkitImageSmoothingEnabled
= false;
356 this._drawCtx
.msImageSmoothingEnabled
= false;
357 this._drawCtx
.imageSmoothingEnabled
= false;
359 this._drawCtx
.drawImage(this._backbuffer
,
362 this._damage(new_x
, new_y
, w
, h
);
366 imageRect: function(x
, y
, mime
, arr
) {
367 var img
= new Image();
368 img
.src
= "data: " + mime
+ ";base64," + Base64
.encode(arr
);
377 // start updating a tile
378 startTile: function (x
, y
, width
, height
, color
) {
381 if (width
=== 16 && height
=== 16) {
382 this._tile
= this._tile16x16
;
384 this._tile
= this._drawCtx
.createImageData(width
, height
);
387 if (this._prefer_js
) {
389 if (this._true_color
) {
392 bgr
= this._colourMap
[color
[0]];
398 var data
= this._tile
.data
;
399 for (var i
= 0; i
< width
* height
* 4; i
+= 4) {
406 this.fillRect(x
, y
, width
, height
, color
, true);
410 // update sub-rectangle of the current tile
411 subTile: function (x
, y
, w
, h
, color
) {
412 if (this._prefer_js
) {
414 if (this._true_color
) {
417 bgr
= this._colourMap
[color
[0]];
425 var data
= this._tile
.data
;
426 var width
= this._tile
.width
;
427 for (var j
= y
; j
< yend
; j
++) {
428 for (var i
= x
; i
< xend
; i
++) {
429 var p
= (i
+ (j
* width
)) * 4;
437 this.fillRect(this._tile_x
+ x
, this._tile_y
+ y
, w
, h
, color
, true);
441 // draw the current tile to the screen
442 finishTile: function () {
443 if (this._prefer_js
) {
444 this._drawCtx
.putImageData(this._tile
, this._tile_x
, this._tile_y
);
445 this._damage(this._tile_x
, this._tile_y
,
446 this._tile
.width
, this._tile
.height
);
448 // else: No-op -- already done by setSubTile
451 blitImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
452 if (this._renderQ
.length
!== 0 && !from_queue
) {
453 // NB(directxman12): it's technically more performant here to use preallocated arrays,
454 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
455 // this probably isn't getting called *nearly* as much
456 var new_arr
= new Uint8Array(width
* height
* 4);
457 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
466 } else if (this._true_color
) {
467 this._bgrxImageData(x
, y
, width
, height
, arr
, offset
);
469 this._cmapImageData(x
, y
, width
, height
, arr
, offset
);
473 blitRgbImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
474 if (this._renderQ
.length
!== 0 && !from_queue
) {
475 // NB(directxman12): it's technically more performant here to use preallocated arrays,
476 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
477 // this probably isn't getting called *nearly* as much
478 var new_arr
= new Uint8Array(width
* height
* 3);
479 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
488 } else if (this._true_color
) {
489 this._rgbImageData(x
, y
, width
, height
, arr
, offset
);
492 this._cmapImageData(x
, y
, width
, height
, arr
, offset
);
496 blitRgbxImage: function (x
, y
, width
, height
, arr
, offset
, from_queue
) {
497 if (this._renderQ
.length
!== 0 && !from_queue
) {
498 // NB(directxman12): it's technically more performant here to use preallocated arrays,
499 // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
500 // this probably isn't getting called *nearly* as much
501 var new_arr
= new Uint8Array(width
* height
* 4);
502 new_arr
.set(new Uint8Array(arr
.buffer
, 0, new_arr
.length
));
512 this._rgbxImageData(x
, y
, width
, height
, arr
, offset
);
516 drawImage: function (img
, x
, y
) {
517 this._drawCtx
.drawImage(img
, x
, y
);
518 this._damage(x
, y
, img
.width
, img
.height
);
521 changeCursor: function (pixels
, mask
, hotx
, hoty
, w
, h
) {
522 if (this._cursor_uri
=== false) {
523 Log
.Warn("changeCursor called but no cursor data URI support");
527 if (this._true_color
) {
528 Display
.changeCursor(this._target
, pixels
, mask
, hotx
, hoty
, w
, h
);
530 Display
.changeCursor(this._target
, pixels
, mask
, hotx
, hoty
, w
, h
, this._colourMap
);
534 defaultCursor: function () {
535 this._target
.style
.cursor
= "default";
538 disableLocalCursor: function () {
539 this._target
.style
.cursor
= "none";
542 clippingDisplay: function () {
543 var vp
= this._viewportLoc
;
544 return this._fb_width
> vp
.w
|| this._fb_height
> vp
.h
;
547 // Overridden getters/setters
548 set_scale: function (scale
) {
549 this._rescale(scale
);
552 set_viewport: function (viewport
) {
553 this._viewport
= viewport
;
554 // May need to readjust the viewport dimensions
555 var vp
= this._viewportLoc
;
556 this.viewportChangeSize(vp
.w
, vp
.h
);
557 this.viewportChangePos(0, 0);
560 get_width: function () {
561 return this._fb_width
;
563 get_height: function () {
564 return this._fb_height
;
567 autoscale: function (containerWidth
, containerHeight
, downscaleOnly
) {
568 var vp
= this._viewportLoc
;
569 var targetAspectRatio
= containerWidth
/ containerHeight
;
570 var fbAspectRatio
= vp
.w
/ vp
.h
;
573 if (fbAspectRatio
>= targetAspectRatio
) {
574 scaleRatio
= containerWidth
/ vp
.w
;
576 scaleRatio
= containerHeight
/ vp
.h
;
579 if (scaleRatio
> 1.0 && downscaleOnly
) {
583 this._rescale(scaleRatio
);
587 _rescale: function (factor
) {
588 this._scale
= factor
;
589 var vp
= this._viewportLoc
;
591 // NB(directxman12): If you set the width directly, or set the
592 // style width to a number, the canvas is cleared.
593 // However, if you set the style width to a string
594 // ('NNNpx'), the canvas is scaled without clearing.
595 var width
= Math
.round(factor
* vp
.w
) + 'px';
596 var height
= Math
.round(factor
* vp
.h
) + 'px';
598 if ((this._target
.style
.width
!== width
) ||
599 (this._target
.style
.height
!== height
)) {
600 this._target
.style
.width
= width
;
601 this._target
.style
.height
= height
;
605 _setFillColor: function (color
) {
607 if (this._true_color
) {
610 bgr
= this._colourMap
[color
];
613 var newStyle
= 'rgb(' + bgr
[2] + ',' + bgr
[1] + ',' + bgr
[0] + ')';
614 if (newStyle
!== this._prevDrawStyle
) {
615 this._drawCtx
.fillStyle
= newStyle
;
616 this._prevDrawStyle
= newStyle
;
620 _rgbImageData: function (x
, y
, width
, height
, arr
, offset
) {
621 var img
= this._drawCtx
.createImageData(width
, height
);
623 for (var i
= 0, j
= offset
; i
< width
* height
* 4; i
+= 4, j
+= 3) {
625 data
[i
+ 1] = arr
[j
+ 1];
626 data
[i
+ 2] = arr
[j
+ 2];
627 data
[i
+ 3] = 255; // Alpha
629 this._drawCtx
.putImageData(img
, x
, y
);
630 this._damage(x
, y
, img
.width
, img
.height
);
633 _bgrxImageData: function (x
, y
, width
, height
, arr
, offset
) {
634 var img
= this._drawCtx
.createImageData(width
, height
);
636 for (var i
= 0, j
= offset
; i
< width
* height
* 4; i
+= 4, j
+= 4) {
637 data
[i
] = arr
[j
+ 2];
638 data
[i
+ 1] = arr
[j
+ 1];
639 data
[i
+ 2] = arr
[j
];
640 data
[i
+ 3] = 255; // Alpha
642 this._drawCtx
.putImageData(img
, x
, y
);
643 this._damage(x
, y
, img
.width
, img
.height
);
646 _rgbxImageData: function (x
, y
, width
, height
, arr
, offset
) {
647 // NB(directxman12): arr must be an Type Array view
649 if (SUPPORTS_IMAGEDATA_CONSTRUCTOR
) {
650 img
= new ImageData(new Uint8ClampedArray(arr
.buffer
, arr
.byteOffset
, width
* height
* 4), width
, height
);
652 img
= this._drawCtx
.createImageData(width
, height
);
653 img
.data
.set(new Uint8ClampedArray(arr
.buffer
, arr
.byteOffset
, width
* height
* 4));
655 this._drawCtx
.putImageData(img
, x
, y
);
656 this._damage(x
, y
, img
.width
, img
.height
);
659 _cmapImageData: function (x
, y
, width
, height
, arr
, offset
) {
660 var img
= this._drawCtx
.createImageData(width
, height
);
662 var cmap
= this._colourMap
;
663 for (var i
= 0, j
= offset
; i
< width
* height
* 4; i
+= 4, j
++) {
664 var bgr
= cmap
[arr
[j
]];
666 data
[i
+ 1] = bgr
[1];
667 data
[i
+ 2] = bgr
[0];
668 data
[i
+ 3] = 255; // Alpha
670 this._drawCtx
.putImageData(img
, x
, y
);
671 this._damage(x
, y
, img
.width
, img
.height
);
674 _renderQ_push: function (action
) {
675 this._renderQ
.push(action
);
676 if (this._renderQ
.length
=== 1) {
677 // If this can be rendered immediately it will be, otherwise
678 // the scanner will wait for the relevant event
679 this._scan_renderQ();
683 _resume_renderQ: function() {
684 // "this" is the object that is ready, not the
686 this.removeEventListener('load', this._noVNC_display
._resume_renderQ
);
687 this._noVNC_display
._scan_renderQ();
690 _scan_renderQ: function () {
692 while (ready
&& this._renderQ
.length
> 0) {
693 var a
= this._renderQ
[0];
699 this.copyImage(a
.old_x
, a
.old_y
, a
.x
, a
.y
, a
.width
, a
.height
, true);
702 this.fillRect(a
.x
, a
.y
, a
.width
, a
.height
, a
.color
, true);
705 this.blitImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
708 this.blitRgbImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
711 this.blitRgbxImage(a
.x
, a
.y
, a
.width
, a
.height
, a
.data
, 0, true);
714 if (a
.img
.complete
) {
715 this.drawImage(a
.img
, a
.x
, a
.y
);
717 a
.img
._noVNC_display
= this;
718 a
.img
.addEventListener('load', this._resume_renderQ
);
719 // We need to wait for this image to 'load'
720 // to keep things in-order
727 this._renderQ
.shift();
731 if (this._renderQ
.length
=== 0 && this._flushing
) {
732 this._flushing
= false;
738 make_properties(Display
, [
739 ['target', 'wo', 'dom'], // Canvas element for rendering
740 ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
741 ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "type": mime-type, "data": data}
742 ['true_color', 'rw', 'bool'], // Use true-color pixel data
743 ['colourMap', 'rw', 'arr'], // Colour map array (when not true-color)
744 ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
745 ['viewport', 'rw', 'bool'], // Use viewport clipping
746 ['width', 'ro', 'int'], // Display area width
747 ['height', 'ro', 'int'], // Display area height
749 ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
751 ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
752 ['cursor_uri', 'rw', 'raw'], // Can we render cursor using data URI
754 ['onFlush', 'rw', 'func'], // onFlush(): A flush request has finished
758 Display
.changeCursor = function (target
, pixels
, mask
, hotx
, hoty
, w
, h
, cmap
) {
759 if ((w
=== 0) || (h
=== 0)) {
760 target
.style
.cursor
= 'none';
766 for (y
= 0; y
< h
; y
++) {
767 for (x
= 0; x
< w
; x
++) {
768 var idx
= y
* Math
.ceil(w
/ 8) + Math
.floor(x
/ 8);
769 var alpha
= (mask
[idx
] << (x
% 8)) & 0x80 ? 255 : 0;
772 var rgb
= cmap
[pixels
[idx
]];
773 cur
.push(rgb
[0]); // red
774 cur
.push(rgb
[1]); // green
775 cur
.push(rgb
[2]); // blue
776 cur
.push(alpha
); // alpha
778 idx
= ((w
* y
) + x
) * 4;
779 cur
.push(pixels
[idx
+ 2]); // red
780 cur
.push(pixels
[idx
+ 1]); // green
781 cur
.push(pixels
[idx
]); // blue
782 cur
.push(alpha
); // alpha
787 var canvas
= document
.createElement('canvas');
788 var ctx
= canvas
.getContext('2d');
794 if (SUPPORTS_IMAGEDATA_CONSTRUCTOR
) {
795 img
= new ImageData(new Uint8ClampedArray(cur
), w
, h
);
797 img
= ctx
.createImageData(w
, h
);
798 img
.data
.set(new Uint8ClampedArray(cur
));
800 ctx
.clearRect(0, 0, w
, h
);
801 ctx
.putImageData(img
, 0, 0);
803 var url
= canvas
.toDataURL();
804 target
.style
.cursor
= 'url(' + url
+ ')' + hotx
+ ' ' + hoty
+ ', default';