display.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2018 The noVNC Authors
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. import * as Log from './util/logging.js';
  9. import Base64 from "./base64.js";
  10. import { supportsImageMetadata } from './util/browser.js';
  11. export default class Display {
  12. constructor(target) {
  13. this._drawCtx = null;
  14. this._c_forceCanvas = false;
  15. this._renderQ = []; // queue drawing actions for in-oder rendering
  16. this._flushing = false;
  17. // the full frame buffer (logical canvas) size
  18. this._fb_width = 0;
  19. this._fb_height = 0;
  20. this._prevDrawStyle = "";
  21. this._tile = null;
  22. this._tile16x16 = null;
  23. this._tile_x = 0;
  24. this._tile_y = 0;
  25. Log.Debug(">> Display.constructor");
  26. // The visible canvas
  27. this._target = target;
  28. if (!this._target) {
  29. throw new Error("Target must be set");
  30. }
  31. if (typeof this._target === 'string') {
  32. throw new Error('target must be a DOM element');
  33. }
  34. if (!this._target.getContext) {
  35. throw new Error("no getContext method");
  36. }
  37. this._targetCtx = this._target.getContext('2d');
  38. // the visible canvas viewport (i.e. what actually gets seen)
  39. this._viewportLoc = { 'x': 0, 'y': 0, 'w': this._target.width, 'h': this._target.height };
  40. // The hidden canvas, where we do the actual rendering
  41. this._backbuffer = document.createElement('canvas');
  42. this._drawCtx = this._backbuffer.getContext('2d');
  43. this._damageBounds = { left: 0, top: 0,
  44. right: this._backbuffer.width,
  45. bottom: this._backbuffer.height };
  46. Log.Debug("User Agent: " + navigator.userAgent);
  47. this.clear();
  48. // Check canvas features
  49. if (!('createImageData' in this._drawCtx)) {
  50. throw new Error("Canvas does not support createImageData");
  51. }
  52. this._tile16x16 = this._drawCtx.createImageData(16, 16);
  53. Log.Debug("<< Display.constructor");
  54. // ===== PROPERTIES =====
  55. this._scale = 1.0;
  56. this._clipViewport = false;
  57. this.logo = null;
  58. // ===== EVENT HANDLERS =====
  59. this.onflush = () => {}; // A flush request has finished
  60. }
  61. // ===== PROPERTIES =====
  62. get scale() { return this._scale; }
  63. set scale(scale) {
  64. this._rescale(scale);
  65. }
  66. get clipViewport() { return this._clipViewport; }
  67. set clipViewport(viewport) {
  68. this._clipViewport = viewport;
  69. // May need to readjust the viewport dimensions
  70. const vp = this._viewportLoc;
  71. this.viewportChangeSize(vp.w, vp.h);
  72. this.viewportChangePos(0, 0);
  73. }
  74. get width() {
  75. return this._fb_width;
  76. }
  77. get height() {
  78. return this._fb_height;
  79. }
  80. // ===== PUBLIC METHODS =====
  81. viewportChangePos(deltaX, deltaY) {
  82. const vp = this._viewportLoc;
  83. deltaX = Math.floor(deltaX);
  84. deltaY = Math.floor(deltaY);
  85. if (!this._clipViewport) {
  86. deltaX = -vp.w; // clamped later of out of bounds
  87. deltaY = -vp.h;
  88. }
  89. const vx2 = vp.x + vp.w - 1;
  90. const vy2 = vp.y + vp.h - 1;
  91. // Position change
  92. if (deltaX < 0 && vp.x + deltaX < 0) {
  93. deltaX = -vp.x;
  94. }
  95. if (vx2 + deltaX >= this._fb_width) {
  96. deltaX -= vx2 + deltaX - this._fb_width + 1;
  97. }
  98. if (vp.y + deltaY < 0) {
  99. deltaY = -vp.y;
  100. }
  101. if (vy2 + deltaY >= this._fb_height) {
  102. deltaY -= (vy2 + deltaY - this._fb_height + 1);
  103. }
  104. if (deltaX === 0 && deltaY === 0) {
  105. return;
  106. }
  107. Log.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  108. vp.x += deltaX;
  109. vp.y += deltaY;
  110. this._damage(vp.x, vp.y, vp.w, vp.h);
  111. this.flip();
  112. }
  113. viewportChangeSize(width, height) {
  114. if (!this._clipViewport ||
  115. typeof(width) === "undefined" ||
  116. typeof(height) === "undefined") {
  117. Log.Debug("Setting viewport to full display region");
  118. width = this._fb_width;
  119. height = this._fb_height;
  120. }
  121. width = Math.floor(width);
  122. height = Math.floor(height);
  123. if (width > this._fb_width) {
  124. width = this._fb_width;
  125. }
  126. if (height > this._fb_height) {
  127. height = this._fb_height;
  128. }
  129. const vp = this._viewportLoc;
  130. if (vp.w !== width || vp.h !== height) {
  131. vp.w = width;
  132. vp.h = height;
  133. const canvas = this._target;
  134. canvas.width = width;
  135. canvas.height = height;
  136. // The position might need to be updated if we've grown
  137. this.viewportChangePos(0, 0);
  138. this._damage(vp.x, vp.y, vp.w, vp.h);
  139. this.flip();
  140. // Update the visible size of the target canvas
  141. this._rescale(this._scale);
  142. }
  143. }
  144. absX(x) {
  145. if (this._scale === 0) {
  146. return 0;
  147. }
  148. return x / this._scale + this._viewportLoc.x;
  149. }
  150. absY(y) {
  151. if (this._scale === 0) {
  152. return 0;
  153. }
  154. return y / this._scale + this._viewportLoc.y;
  155. }
  156. resize(width, height) {
  157. this._prevDrawStyle = "";
  158. this._fb_width = width;
  159. this._fb_height = height;
  160. const canvas = this._backbuffer;
  161. if (canvas.width !== width || canvas.height !== height) {
  162. // We have to save the canvas data since changing the size will clear it
  163. let saveImg = null;
  164. if (canvas.width > 0 && canvas.height > 0) {
  165. saveImg = this._drawCtx.getImageData(0, 0, canvas.width, canvas.height);
  166. }
  167. if (canvas.width !== width) {
  168. canvas.width = width;
  169. }
  170. if (canvas.height !== height) {
  171. canvas.height = height;
  172. }
  173. if (saveImg) {
  174. this._drawCtx.putImageData(saveImg, 0, 0);
  175. }
  176. }
  177. // Readjust the viewport as it may be incorrectly sized
  178. // and positioned
  179. const vp = this._viewportLoc;
  180. this.viewportChangeSize(vp.w, vp.h);
  181. this.viewportChangePos(0, 0);
  182. }
  183. // Track what parts of the visible canvas that need updating
  184. _damage(x, y, w, h) {
  185. if (x < this._damageBounds.left) {
  186. this._damageBounds.left = x;
  187. }
  188. if (y < this._damageBounds.top) {
  189. this._damageBounds.top = y;
  190. }
  191. if ((x + w) > this._damageBounds.right) {
  192. this._damageBounds.right = x + w;
  193. }
  194. if ((y + h) > this._damageBounds.bottom) {
  195. this._damageBounds.bottom = y + h;
  196. }
  197. }
  198. // Update the visible canvas with the contents of the
  199. // rendering canvas
  200. flip(from_queue) {
  201. if (this._renderQ.length !== 0 && !from_queue) {
  202. this._renderQ_push({
  203. 'type': 'flip'
  204. });
  205. } else {
  206. let x = this._damageBounds.left;
  207. let y = this._damageBounds.top;
  208. let w = this._damageBounds.right - x;
  209. let h = this._damageBounds.bottom - y;
  210. let vx = x - this._viewportLoc.x;
  211. let vy = y - this._viewportLoc.y;
  212. if (vx < 0) {
  213. w += vx;
  214. x -= vx;
  215. vx = 0;
  216. }
  217. if (vy < 0) {
  218. h += vy;
  219. y -= vy;
  220. vy = 0;
  221. }
  222. if ((vx + w) > this._viewportLoc.w) {
  223. w = this._viewportLoc.w - vx;
  224. }
  225. if ((vy + h) > this._viewportLoc.h) {
  226. h = this._viewportLoc.h - vy;
  227. }
  228. if ((w > 0) && (h > 0)) {
  229. // FIXME: We may need to disable image smoothing here
  230. // as well (see copyImage()), but we haven't
  231. // noticed any problem yet.
  232. this._targetCtx.drawImage(this._backbuffer,
  233. x, y, w, h,
  234. vx, vy, w, h);
  235. }
  236. this._damageBounds.left = this._damageBounds.top = 65535;
  237. this._damageBounds.right = this._damageBounds.bottom = 0;
  238. }
  239. }
  240. clear() {
  241. if (this._logo) {
  242. this.resize(this._logo.width, this._logo.height);
  243. this.imageRect(0, 0, this._logo.type, this._logo.data);
  244. } else {
  245. this.resize(240, 20);
  246. this._drawCtx.clearRect(0, 0, this._fb_width, this._fb_height);
  247. }
  248. this.flip();
  249. }
  250. pending() {
  251. return this._renderQ.length > 0;
  252. }
  253. flush() {
  254. if (this._renderQ.length === 0) {
  255. this.onflush();
  256. } else {
  257. this._flushing = true;
  258. }
  259. }
  260. fillRect(x, y, width, height, color, from_queue) {
  261. if (this._renderQ.length !== 0 && !from_queue) {
  262. this._renderQ_push({
  263. 'type': 'fill',
  264. 'x': x,
  265. 'y': y,
  266. 'width': width,
  267. 'height': height,
  268. 'color': color
  269. });
  270. } else {
  271. this._setFillColor(color);
  272. this._drawCtx.fillRect(x, y, width, height);
  273. this._damage(x, y, width, height);
  274. }
  275. }
  276. copyImage(old_x, old_y, new_x, new_y, w, h, from_queue) {
  277. if (this._renderQ.length !== 0 && !from_queue) {
  278. this._renderQ_push({
  279. 'type': 'copy',
  280. 'old_x': old_x,
  281. 'old_y': old_y,
  282. 'x': new_x,
  283. 'y': new_y,
  284. 'width': w,
  285. 'height': h,
  286. });
  287. } else {
  288. // Due to this bug among others [1] we need to disable the image-smoothing to
  289. // avoid getting a blur effect when copying data.
  290. //
  291. // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
  292. //
  293. // We need to set these every time since all properties are reset
  294. // when the the size is changed
  295. this._drawCtx.mozImageSmoothingEnabled = false;
  296. this._drawCtx.webkitImageSmoothingEnabled = false;
  297. this._drawCtx.msImageSmoothingEnabled = false;
  298. this._drawCtx.imageSmoothingEnabled = false;
  299. this._drawCtx.drawImage(this._backbuffer,
  300. old_x, old_y, w, h,
  301. new_x, new_y, w, h);
  302. this._damage(new_x, new_y, w, h);
  303. }
  304. }
  305. imageRect(x, y, mime, arr) {
  306. const img = new Image();
  307. img.src = "data: " + mime + ";base64," + Base64.encode(arr);
  308. this._renderQ_push({
  309. 'type': 'img',
  310. 'img': img,
  311. 'x': x,
  312. 'y': y
  313. });
  314. }
  315. // start updating a tile
  316. startTile(x, y, width, height, color) {
  317. this._tile_x = x;
  318. this._tile_y = y;
  319. if (width === 16 && height === 16) {
  320. this._tile = this._tile16x16;
  321. } else {
  322. this._tile = this._drawCtx.createImageData(width, height);
  323. }
  324. const red = color[2];
  325. const green = color[1];
  326. const blue = color[0];
  327. const data = this._tile.data;
  328. for (let i = 0; i < width * height * 4; i += 4) {
  329. data[i] = red;
  330. data[i + 1] = green;
  331. data[i + 2] = blue;
  332. data[i + 3] = 255;
  333. }
  334. }
  335. // update sub-rectangle of the current tile
  336. subTile(x, y, w, h, color) {
  337. const red = color[2];
  338. const green = color[1];
  339. const blue = color[0];
  340. const xend = x + w;
  341. const yend = y + h;
  342. const data = this._tile.data;
  343. const width = this._tile.width;
  344. for (let j = y; j < yend; j++) {
  345. for (let i = x; i < xend; i++) {
  346. const p = (i + (j * width)) * 4;
  347. data[p] = red;
  348. data[p + 1] = green;
  349. data[p + 2] = blue;
  350. data[p + 3] = 255;
  351. }
  352. }
  353. }
  354. // draw the current tile to the screen
  355. finishTile() {
  356. this._drawCtx.putImageData(this._tile, this._tile_x, this._tile_y);
  357. this._damage(this._tile_x, this._tile_y,
  358. this._tile.width, this._tile.height);
  359. }
  360. blitImage(x, y, width, height, arr, offset, from_queue) {
  361. if (this._renderQ.length !== 0 && !from_queue) {
  362. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  363. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  364. // this probably isn't getting called *nearly* as much
  365. const new_arr = new Uint8Array(width * height * 4);
  366. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  367. this._renderQ_push({
  368. 'type': 'blit',
  369. 'data': new_arr,
  370. 'x': x,
  371. 'y': y,
  372. 'width': width,
  373. 'height': height,
  374. });
  375. } else {
  376. this._bgrxImageData(x, y, width, height, arr, offset);
  377. }
  378. }
  379. blitRgbImage(x, y, width, height, arr, offset, from_queue) {
  380. if (this._renderQ.length !== 0 && !from_queue) {
  381. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  382. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  383. // this probably isn't getting called *nearly* as much
  384. const new_arr = new Uint8Array(width * height * 3);
  385. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  386. this._renderQ_push({
  387. 'type': 'blitRgb',
  388. 'data': new_arr,
  389. 'x': x,
  390. 'y': y,
  391. 'width': width,
  392. 'height': height,
  393. });
  394. } else {
  395. this._rgbImageData(x, y, width, height, arr, offset);
  396. }
  397. }
  398. blitRgbxImage(x, y, width, height, arr, offset, from_queue) {
  399. if (this._renderQ.length !== 0 && !from_queue) {
  400. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  401. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  402. // this probably isn't getting called *nearly* as much
  403. const new_arr = new Uint8Array(width * height * 4);
  404. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  405. this._renderQ_push({
  406. 'type': 'blitRgbx',
  407. 'data': new_arr,
  408. 'x': x,
  409. 'y': y,
  410. 'width': width,
  411. 'height': height,
  412. });
  413. } else {
  414. this._rgbxImageData(x, y, width, height, arr, offset);
  415. }
  416. }
  417. drawImage(img, x, y) {
  418. this._drawCtx.drawImage(img, x, y);
  419. this._damage(x, y, img.width, img.height);
  420. }
  421. autoscale(containerWidth, containerHeight) {
  422. let scaleRatio;
  423. if (containerWidth === 0 || containerHeight === 0) {
  424. scaleRatio = 0;
  425. } else {
  426. const vp = this._viewportLoc;
  427. const targetAspectRatio = containerWidth / containerHeight;
  428. const fbAspectRatio = vp.w / vp.h;
  429. if (fbAspectRatio >= targetAspectRatio) {
  430. scaleRatio = containerWidth / vp.w;
  431. } else {
  432. scaleRatio = containerHeight / vp.h;
  433. }
  434. }
  435. this._rescale(scaleRatio);
  436. }
  437. // ===== PRIVATE METHODS =====
  438. _rescale(factor) {
  439. this._scale = factor;
  440. 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';
  447. if ((this._target.style.width !== width) ||
  448. (this._target.style.height !== height)) {
  449. this._target.style.width = width;
  450. this._target.style.height = height;
  451. }
  452. }
  453. _setFillColor(color) {
  454. const newStyle = 'rgb(' + color[2] + ',' + color[1] + ',' + color[0] + ')';
  455. if (newStyle !== this._prevDrawStyle) {
  456. this._drawCtx.fillStyle = newStyle;
  457. this._prevDrawStyle = newStyle;
  458. }
  459. }
  460. _rgbImageData(x, y, width, height, arr, offset) {
  461. const img = this._drawCtx.createImageData(width, height);
  462. const data = img.data;
  463. for (let i = 0, j = offset; i < width * height * 4; i += 4, j += 3) {
  464. data[i] = arr[j];
  465. data[i + 1] = arr[j + 1];
  466. data[i + 2] = arr[j + 2];
  467. data[i + 3] = 255; // Alpha
  468. }
  469. this._drawCtx.putImageData(img, x, y);
  470. this._damage(x, y, img.width, img.height);
  471. }
  472. _bgrxImageData(x, y, width, height, arr, offset) {
  473. const img = this._drawCtx.createImageData(width, height);
  474. const data = img.data;
  475. for (let i = 0, j = offset; i < width * height * 4; i += 4, j += 4) {
  476. data[i] = arr[j + 2];
  477. data[i + 1] = arr[j + 1];
  478. data[i + 2] = arr[j];
  479. data[i + 3] = 255; // Alpha
  480. }
  481. this._drawCtx.putImageData(img, x, y);
  482. this._damage(x, y, img.width, img.height);
  483. }
  484. _rgbxImageData(x, y, width, height, arr, offset) {
  485. // NB(directxman12): arr must be an Type Array view
  486. let img;
  487. if (supportsImageMetadata) {
  488. img = new ImageData(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4), width, height);
  489. } else {
  490. img = this._drawCtx.createImageData(width, height);
  491. img.data.set(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4));
  492. }
  493. this._drawCtx.putImageData(img, x, y);
  494. this._damage(x, y, img.width, img.height);
  495. }
  496. _renderQ_push(action) {
  497. this._renderQ.push(action);
  498. if (this._renderQ.length === 1) {
  499. // If this can be rendered immediately it will be, otherwise
  500. // the scanner will wait for the relevant event
  501. this._scan_renderQ();
  502. }
  503. }
  504. _resume_renderQ() {
  505. // "this" is the object that is ready, not the
  506. // display object
  507. this.removeEventListener('load', this._noVNC_display._resume_renderQ);
  508. this._noVNC_display._scan_renderQ();
  509. }
  510. _scan_renderQ() {
  511. let ready = true;
  512. while (ready && this._renderQ.length > 0) {
  513. const a = this._renderQ[0];
  514. switch (a.type) {
  515. case 'flip':
  516. this.flip(true);
  517. break;
  518. case 'copy':
  519. this.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height, true);
  520. break;
  521. case 'fill':
  522. this.fillRect(a.x, a.y, a.width, a.height, a.color, true);
  523. break;
  524. case 'blit':
  525. this.blitImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  526. break;
  527. case 'blitRgb':
  528. this.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  529. break;
  530. case 'blitRgbx':
  531. this.blitRgbxImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  532. break;
  533. case 'img':
  534. if (a.img.complete) {
  535. this.drawImage(a.img, a.x, a.y);
  536. } else {
  537. a.img._noVNC_display = this;
  538. a.img.addEventListener('load', this._resume_renderQ);
  539. // We need to wait for this image to 'load'
  540. // to keep things in-order
  541. ready = false;
  542. }
  543. break;
  544. }
  545. if (ready) {
  546. this._renderQ.shift();
  547. }
  548. }
  549. if (this._renderQ.length === 0 && this._flushing) {
  550. this._flushing = false;
  551. this.onflush();
  552. }
  553. }
  554. }