display.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  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)
  6. *
  7. * See README.md for usage and integration instructions.
  8. */
  9. /*jslint browser: true, white: false */
  10. /*global Util, Base64, changeCursor */
  11. var Display;
  12. (function () {
  13. "use strict";
  14. var SUPPORTS_IMAGEDATA_CONSTRUCTOR = false;
  15. try {
  16. new ImageData(new Uint8ClampedArray(1), 1, 1);
  17. SUPPORTS_IMAGEDATA_CONSTRUCTOR = true;
  18. } catch (ex) {
  19. // ignore failure
  20. }
  21. Display = function (defaults) {
  22. this._drawCtx = null;
  23. this._c_forceCanvas = false;
  24. this._renderQ = []; // queue drawing actions for in-oder rendering
  25. // the full frame buffer (logical canvas) size
  26. this._fb_width = 0;
  27. this._fb_height = 0;
  28. // the size limit of the viewport (start disabled)
  29. this._maxWidth = 0;
  30. this._maxHeight = 0;
  31. // the visible "physical canvas" viewport
  32. this._viewportLoc = { 'x': 0, 'y': 0, 'w': 0, 'h': 0 };
  33. this._cleanRect = { 'x1': 0, 'y1': 0, 'x2': -1, 'y2': -1 };
  34. this._prevDrawStyle = "";
  35. this._tile = null;
  36. this._tile16x16 = null;
  37. this._tile_x = 0;
  38. this._tile_y = 0;
  39. Util.set_defaults(this, defaults, {
  40. 'true_color': true,
  41. 'colourMap': [],
  42. 'scale': 1.0,
  43. 'viewport': false,
  44. 'render_mode': ''
  45. });
  46. Util.Debug(">> Display.constructor");
  47. if (!this._target) {
  48. throw new Error("Target must be set");
  49. }
  50. if (typeof this._target === 'string') {
  51. throw new Error('target must be a DOM element');
  52. }
  53. if (!this._target.getContext) {
  54. throw new Error("no getContext method");
  55. }
  56. if (!this._drawCtx) {
  57. this._drawCtx = this._target.getContext('2d');
  58. }
  59. Util.Debug("User Agent: " + navigator.userAgent);
  60. if (Util.Engine.gecko) { Util.Debug("Browser: gecko " + Util.Engine.gecko); }
  61. if (Util.Engine.webkit) { Util.Debug("Browser: webkit " + Util.Engine.webkit); }
  62. if (Util.Engine.trident) { Util.Debug("Browser: trident " + Util.Engine.trident); }
  63. if (Util.Engine.presto) { Util.Debug("Browser: presto " + Util.Engine.presto); }
  64. this.clear();
  65. // Check canvas features
  66. if ('createImageData' in this._drawCtx) {
  67. this._render_mode = 'canvas rendering';
  68. } else {
  69. throw new Error("Canvas does not support createImageData");
  70. }
  71. if (this._prefer_js === null) {
  72. Util.Info("Prefering javascript operations");
  73. this._prefer_js = true;
  74. }
  75. // Determine browser support for setting the cursor via data URI scheme
  76. if (this._cursor_uri || this._cursor_uri === null ||
  77. this._cursor_uri === undefined) {
  78. this._cursor_uri = Util.browserSupportsCursorURIs();
  79. }
  80. Util.Debug("<< Display.constructor");
  81. };
  82. Display.prototype = {
  83. // Public methods
  84. viewportChangePos: function (deltaX, deltaY) {
  85. var vp = this._viewportLoc;
  86. deltaX = Math.floor(deltaX);
  87. deltaY = Math.floor(deltaY);
  88. if (!this._viewport) {
  89. deltaX = -vp.w; // clamped later of out of bounds
  90. deltaY = -vp.h;
  91. }
  92. var vx2 = vp.x + vp.w - 1;
  93. var vy2 = vp.y + vp.h - 1;
  94. // Position change
  95. if (deltaX < 0 && vp.x + deltaX < 0) {
  96. deltaX = -vp.x;
  97. }
  98. if (vx2 + deltaX >= this._fb_width) {
  99. deltaX -= vx2 + deltaX - this._fb_width + 1;
  100. }
  101. if (vp.y + deltaY < 0) {
  102. deltaY = -vp.y;
  103. }
  104. if (vy2 + deltaY >= this._fb_height) {
  105. deltaY -= (vy2 + deltaY - this._fb_height + 1);
  106. }
  107. if (deltaX === 0 && deltaY === 0) {
  108. return;
  109. }
  110. Util.Debug("viewportChange deltaX: " + deltaX + ", deltaY: " + deltaY);
  111. vp.x += deltaX;
  112. vx2 += deltaX;
  113. vp.y += deltaY;
  114. vy2 += deltaY;
  115. // Update the clean rectangle
  116. var cr = this._cleanRect;
  117. if (vp.x > cr.x1) {
  118. cr.x1 = vp.x;
  119. }
  120. if (vx2 < cr.x2) {
  121. cr.x2 = vx2;
  122. }
  123. if (vp.y > cr.y1) {
  124. cr.y1 = vp.y;
  125. }
  126. if (vy2 < cr.y2) {
  127. cr.y2 = vy2;
  128. }
  129. var x1, w;
  130. if (deltaX < 0) {
  131. // Shift viewport left, redraw left section
  132. x1 = 0;
  133. w = -deltaX;
  134. } else {
  135. // Shift viewport right, redraw right section
  136. x1 = vp.w - deltaX;
  137. w = deltaX;
  138. }
  139. var y1, h;
  140. if (deltaY < 0) {
  141. // Shift viewport up, redraw top section
  142. y1 = 0;
  143. h = -deltaY;
  144. } else {
  145. // Shift viewport down, redraw bottom section
  146. y1 = vp.h - deltaY;
  147. h = deltaY;
  148. }
  149. var saveStyle = this._drawCtx.fillStyle;
  150. var canvas = this._target;
  151. this._drawCtx.fillStyle = "rgb(255,255,255)";
  152. // Due to this bug among others [1] we need to disable the image-smoothing to
  153. // avoid getting a blur effect when panning.
  154. //
  155. // 1. https://bugzilla.mozilla.org/show_bug.cgi?id=1194719
  156. //
  157. // We need to set these every time since all properties are reset
  158. // when the the size is changed
  159. if (this._drawCtx.mozImageSmoothingEnabled) {
  160. this._drawCtx.mozImageSmoothingEnabled = false;
  161. } else if (this._drawCtx.webkitImageSmoothingEnabled) {
  162. this._drawCtx.webkitImageSmoothingEnabled = false;
  163. } else if (this._drawCtx.msImageSmoothingEnabled) {
  164. this._drawCtx.msImageSmoothingEnabled = false;
  165. } else if (this._drawCtx.imageSmoothingEnabled) {
  166. this._drawCtx.imageSmoothingEnabled = false;
  167. }
  168. // Copy the valid part of the viewport to the shifted location
  169. this._drawCtx.drawImage(canvas, 0, 0, vp.w, vp.h, -deltaX, -deltaY, vp.w, vp.h);
  170. if (deltaX !== 0) {
  171. this._drawCtx.fillRect(x1, 0, w, vp.h);
  172. }
  173. if (deltaY !== 0) {
  174. this._drawCtx.fillRect(0, y1, vp.w, h);
  175. }
  176. this._drawCtx.fillStyle = saveStyle;
  177. },
  178. viewportChangeSize: function(width, height) {
  179. if (typeof(width) === "undefined" || typeof(height) === "undefined") {
  180. Util.Debug("Setting viewport to full display region");
  181. width = this._fb_width;
  182. height = this._fb_height;
  183. }
  184. var vp = this._viewportLoc;
  185. if (vp.w !== width || vp.h !== height) {
  186. if (this._viewport) {
  187. if (this._maxWidth !== 0 && width > this._maxWidth) {
  188. width = this._maxWidth;
  189. }
  190. if (this._maxHeight !== 0 && height > this._maxHeight) {
  191. height = this._maxHeight;
  192. }
  193. }
  194. var cr = this._cleanRect;
  195. if (width < vp.w && cr.x2 > vp.x + width - 1) {
  196. cr.x2 = vp.x + width - 1;
  197. }
  198. if (height < vp.h && cr.y2 > vp.y + height - 1) {
  199. cr.y2 = vp.y + height - 1;
  200. }
  201. vp.w = width;
  202. vp.h = height;
  203. var canvas = this._target;
  204. if (canvas.width !== width || canvas.height !== height) {
  205. // We have to save the canvas data since changing the size will clear it
  206. var saveImg = null;
  207. if (vp.w > 0 && vp.h > 0 && canvas.width > 0 && canvas.height > 0) {
  208. var img_width = canvas.width < vp.w ? canvas.width : vp.w;
  209. var img_height = canvas.height < vp.h ? canvas.height : vp.h;
  210. saveImg = this._drawCtx.getImageData(0, 0, img_width, img_height);
  211. }
  212. if (canvas.width !== width) {
  213. canvas.width = width;
  214. canvas.style.width = width + 'px';
  215. }
  216. if (canvas.height !== height) {
  217. canvas.height = height;
  218. canvas.style.height = height + 'px';
  219. }
  220. if (saveImg) {
  221. this._drawCtx.putImageData(saveImg, 0, 0);
  222. }
  223. }
  224. }
  225. },
  226. // Return a map of clean and dirty areas of the viewport and reset the
  227. // tracking of clean and dirty areas
  228. //
  229. // Returns: { 'cleanBox': { 'x': x, 'y': y, 'w': w, 'h': h},
  230. // 'dirtyBoxes': [{ 'x': x, 'y': y, 'w': w, 'h': h }, ...] }
  231. getCleanDirtyReset: function () {
  232. var vp = this._viewportLoc;
  233. var cr = this._cleanRect;
  234. var cleanBox = { 'x': cr.x1, 'y': cr.y1,
  235. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y2 - cr.y1 + 1 };
  236. var dirtyBoxes = [];
  237. if (cr.x1 >= cr.x2 || cr.y1 >= cr.y2) {
  238. // Whole viewport is dirty
  239. dirtyBoxes.push({ 'x': vp.x, 'y': vp.y, 'w': vp.w, 'h': vp.h });
  240. } else {
  241. // Redraw dirty regions
  242. var vx2 = vp.x + vp.w - 1;
  243. var vy2 = vp.y + vp.h - 1;
  244. if (vp.x < cr.x1) {
  245. // left side dirty region
  246. dirtyBoxes.push({'x': vp.x, 'y': vp.y,
  247. 'w': cr.x1 - vp.x + 1, 'h': vp.h});
  248. }
  249. if (vx2 > cr.x2) {
  250. // right side dirty region
  251. dirtyBoxes.push({'x': cr.x2 + 1, 'y': vp.y,
  252. 'w': vx2 - cr.x2, 'h': vp.h});
  253. }
  254. if(vp.y < cr.y1) {
  255. // top/middle dirty region
  256. dirtyBoxes.push({'x': cr.x1, 'y': vp.y,
  257. 'w': cr.x2 - cr.x1 + 1, 'h': cr.y1 - vp.y});
  258. }
  259. if (vy2 > cr.y2) {
  260. // bottom/middle dirty region
  261. dirtyBoxes.push({'x': cr.x1, 'y': cr.y2 + 1,
  262. 'w': cr.x2 - cr.x1 + 1, 'h': vy2 - cr.y2});
  263. }
  264. }
  265. this._cleanRect = {'x1': vp.x, 'y1': vp.y,
  266. 'x2': vp.x + vp.w - 1, 'y2': vp.y + vp.h - 1};
  267. return {'cleanBox': cleanBox, 'dirtyBoxes': dirtyBoxes};
  268. },
  269. absX: function (x) {
  270. return x + this._viewportLoc.x;
  271. },
  272. absY: function (y) {
  273. return y + this._viewportLoc.y;
  274. },
  275. resize: function (width, height) {
  276. this._prevDrawStyle = "";
  277. this._fb_width = width;
  278. this._fb_height = height;
  279. this._rescale(this._scale);
  280. this.viewportChangeSize();
  281. },
  282. clear: function () {
  283. if (this._logo) {
  284. this.resize(this._logo.width, this._logo.height);
  285. this.blitStringImage(this._logo.data, 0, 0);
  286. } else {
  287. if (Util.Engine.trident === 6) {
  288. // NB(directxman12): there's a bug in IE10 where we can fail to actually
  289. // clear the canvas here because of the resize.
  290. // Clearing the current viewport first fixes the issue
  291. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  292. }
  293. this.resize(240, 20);
  294. this._drawCtx.clearRect(0, 0, this._viewportLoc.w, this._viewportLoc.h);
  295. }
  296. this._renderQ = [];
  297. },
  298. fillRect: function (x, y, width, height, color, from_queue) {
  299. if (this._renderQ.length !== 0 && !from_queue) {
  300. this.renderQ_push({
  301. 'type': 'fill',
  302. 'x': x,
  303. 'y': y,
  304. 'width': width,
  305. 'height': height,
  306. 'color': color
  307. });
  308. } else {
  309. this._setFillColor(color);
  310. this._drawCtx.fillRect(x - this._viewportLoc.x, y - this._viewportLoc.y, width, height);
  311. }
  312. },
  313. copyImage: function (old_x, old_y, new_x, new_y, w, h, from_queue) {
  314. if (this._renderQ.length !== 0 && !from_queue) {
  315. this.renderQ_push({
  316. 'type': 'copy',
  317. 'old_x': old_x,
  318. 'old_y': old_y,
  319. 'x': new_x,
  320. 'y': new_y,
  321. 'width': w,
  322. 'height': h,
  323. });
  324. } else {
  325. var x1 = old_x - this._viewportLoc.x;
  326. var y1 = old_y - this._viewportLoc.y;
  327. var x2 = new_x - this._viewportLoc.x;
  328. var y2 = new_y - this._viewportLoc.y;
  329. this._drawCtx.drawImage(this._target, x1, y1, w, h, x2, y2, w, h);
  330. }
  331. },
  332. // start updating a tile
  333. startTile: function (x, y, width, height, color) {
  334. this._tile_x = x;
  335. this._tile_y = y;
  336. if (width === 16 && height === 16) {
  337. this._tile = this._tile16x16;
  338. } else {
  339. this._tile = this._drawCtx.createImageData(width, height);
  340. }
  341. if (this._prefer_js) {
  342. var bgr;
  343. if (this._true_color) {
  344. bgr = color;
  345. } else {
  346. bgr = this._colourMap[color[0]];
  347. }
  348. var red = bgr[2];
  349. var green = bgr[1];
  350. var blue = bgr[0];
  351. var data = this._tile.data;
  352. for (var i = 0; i < width * height * 4; i += 4) {
  353. data[i] = red;
  354. data[i + 1] = green;
  355. data[i + 2] = blue;
  356. data[i + 3] = 255;
  357. }
  358. } else {
  359. this.fillRect(x, y, width, height, color, true);
  360. }
  361. },
  362. // update sub-rectangle of the current tile
  363. subTile: function (x, y, w, h, color) {
  364. if (this._prefer_js) {
  365. var bgr;
  366. if (this._true_color) {
  367. bgr = color;
  368. } else {
  369. bgr = this._colourMap[color[0]];
  370. }
  371. var red = bgr[2];
  372. var green = bgr[1];
  373. var blue = bgr[0];
  374. var xend = x + w;
  375. var yend = y + h;
  376. var data = this._tile.data;
  377. var width = this._tile.width;
  378. for (var j = y; j < yend; j++) {
  379. for (var i = x; i < xend; i++) {
  380. var p = (i + (j * width)) * 4;
  381. data[p] = red;
  382. data[p + 1] = green;
  383. data[p + 2] = blue;
  384. data[p + 3] = 255;
  385. }
  386. }
  387. } else {
  388. this.fillRect(this._tile_x + x, this._tile_y + y, w, h, color, true);
  389. }
  390. },
  391. // draw the current tile to the screen
  392. finishTile: function () {
  393. if (this._prefer_js) {
  394. this._drawCtx.putImageData(this._tile, this._tile_x - this._viewportLoc.x,
  395. this._tile_y - this._viewportLoc.y);
  396. }
  397. // else: No-op -- already done by setSubTile
  398. },
  399. blitImage: function (x, y, width, height, arr, offset, from_queue) {
  400. if (this._renderQ.length !== 0 && !from_queue) {
  401. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  402. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  403. // this probably isn't getting called *nearly* as much
  404. var new_arr = new Uint8Array(width * height * 4);
  405. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  406. this.renderQ_push({
  407. 'type': 'blit',
  408. 'data': new_arr,
  409. 'x': x,
  410. 'y': y,
  411. 'width': width,
  412. 'height': height,
  413. });
  414. } else if (this._true_color) {
  415. this._bgrxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  416. } else {
  417. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  418. }
  419. },
  420. blitRgbImage: function (x, y , width, height, arr, offset, from_queue) {
  421. if (this._renderQ.length !== 0 && !from_queue) {
  422. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  423. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  424. // this probably isn't getting called *nearly* as much
  425. var new_arr = new Uint8Array(width * height * 4);
  426. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  427. this.renderQ_push({
  428. 'type': 'blitRgb',
  429. 'data': new_arr,
  430. 'x': x,
  431. 'y': y,
  432. 'width': width,
  433. 'height': height,
  434. });
  435. } else if (this._true_color) {
  436. this._rgbImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  437. } else {
  438. // probably wrong?
  439. this._cmapImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  440. }
  441. },
  442. blitRgbxImage: function (x, y, width, height, arr, offset, from_queue) {
  443. if (this._renderQ.length !== 0 && !from_queue) {
  444. // NB(directxman12): it's technically more performant here to use preallocated arrays,
  445. // but it's a lot of extra work for not a lot of payoff -- if we're using the render queue,
  446. // this probably isn't getting called *nearly* as much
  447. var new_arr = new Uint8Array(width * height * 4);
  448. new_arr.set(new Uint8Array(arr.buffer, 0, new_arr.length));
  449. this.renderQ_push({
  450. 'type': 'blitRgbx',
  451. 'data': new_arr,
  452. 'x': x,
  453. 'y': y,
  454. 'width': width,
  455. 'height': height,
  456. });
  457. } else {
  458. this._rgbxImageData(x, y, this._viewportLoc.x, this._viewportLoc.y, width, height, arr, offset);
  459. }
  460. },
  461. blitStringImage: function (str, x, y) {
  462. var img = new Image();
  463. img.onload = function () {
  464. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  465. }.bind(this);
  466. img.src = str;
  467. return img; // for debugging purposes
  468. },
  469. // wrap ctx.drawImage but relative to viewport
  470. drawImage: function (img, x, y) {
  471. this._drawCtx.drawImage(img, x - this._viewportLoc.x, y - this._viewportLoc.y);
  472. },
  473. renderQ_push: function (action) {
  474. this._renderQ.push(action);
  475. if (this._renderQ.length === 1) {
  476. // If this can be rendered immediately it will be, otherwise
  477. // the scanner will start polling the queue (every
  478. // requestAnimationFrame interval)
  479. this._scan_renderQ();
  480. }
  481. },
  482. changeCursor: function (pixels, mask, hotx, hoty, w, h) {
  483. if (this._cursor_uri === false) {
  484. Util.Warn("changeCursor called but no cursor data URI support");
  485. return;
  486. }
  487. if (this._true_color) {
  488. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h);
  489. } else {
  490. Display.changeCursor(this._target, pixels, mask, hotx, hoty, w, h, this._colourMap);
  491. }
  492. },
  493. defaultCursor: function () {
  494. this._target.style.cursor = "default";
  495. },
  496. disableLocalCursor: function () {
  497. this._target.style.cursor = "none";
  498. },
  499. clippingDisplay: function () {
  500. var vp = this._viewportLoc;
  501. var fbClip = this._fb_width > vp.w || this._fb_height > vp.h;
  502. var limitedVp = this._maxWidth !== 0 && this._maxHeight !== 0;
  503. var clipping = false;
  504. if (limitedVp) {
  505. clipping = vp.w > this._maxWidth || vp.h > this._maxHeight;
  506. }
  507. return fbClip || (limitedVp && clipping);
  508. },
  509. // Overridden getters/setters
  510. get_context: function () {
  511. return this._drawCtx;
  512. },
  513. set_scale: function (scale) {
  514. this._rescale(scale);
  515. },
  516. set_width: function (w) {
  517. this._fb_width = w;
  518. },
  519. get_width: function () {
  520. return this._fb_width;
  521. },
  522. set_height: function (h) {
  523. this._fb_height = h;
  524. },
  525. get_height: function () {
  526. return this._fb_height;
  527. },
  528. autoscale: function (containerWidth, containerHeight, downscaleOnly) {
  529. var targetAspectRatio = containerWidth / containerHeight;
  530. var fbAspectRatio = this._fb_width / this._fb_height;
  531. var scaleRatio;
  532. if (fbAspectRatio >= targetAspectRatio) {
  533. scaleRatio = containerWidth / this._fb_width;
  534. } else {
  535. scaleRatio = containerHeight / this._fb_height;
  536. }
  537. var targetW, targetH;
  538. if (scaleRatio > 1.0 && downscaleOnly) {
  539. targetW = this._fb_width;
  540. targetH = this._fb_height;
  541. scaleRatio = 1.0;
  542. } else if (fbAspectRatio >= targetAspectRatio) {
  543. targetW = containerWidth;
  544. targetH = Math.round(containerWidth / fbAspectRatio);
  545. } else {
  546. targetW = Math.round(containerHeight * fbAspectRatio);
  547. targetH = containerHeight;
  548. }
  549. // NB(directxman12): If you set the width directly, or set the
  550. // style width to a number, the canvas is cleared.
  551. // However, if you set the style width to a string
  552. // ('NNNpx'), the canvas is scaled without clearing.
  553. this._target.style.width = targetW + 'px';
  554. this._target.style.height = targetH + 'px';
  555. this._scale = scaleRatio;
  556. return scaleRatio; // so that the mouse, etc scale can be set
  557. },
  558. // Private Methods
  559. _rescale: function (factor) {
  560. this._scale = factor;
  561. var w;
  562. var h;
  563. if (this._viewport &&
  564. this._maxWidth !== 0 && this._maxHeight !== 0) {
  565. w = Math.min(this._fb_width, this._maxWidth);
  566. h = Math.min(this._fb_height, this._maxHeight);
  567. } else {
  568. w = this._fb_width;
  569. h = this._fb_height;
  570. }
  571. this._target.style.width = Math.round(factor * w) + 'px';
  572. this._target.style.height = Math.round(factor * h) + 'px';
  573. },
  574. _setFillColor: function (color) {
  575. var bgr;
  576. if (this._true_color) {
  577. bgr = color;
  578. } else {
  579. bgr = this._colourMap[color];
  580. }
  581. var newStyle = 'rgb(' + bgr[2] + ',' + bgr[1] + ',' + bgr[0] + ')';
  582. if (newStyle !== this._prevDrawStyle) {
  583. this._drawCtx.fillStyle = newStyle;
  584. this._prevDrawStyle = newStyle;
  585. }
  586. },
  587. _rgbImageData: function (x, y, vx, vy, width, height, arr, offset) {
  588. var img = this._drawCtx.createImageData(width, height);
  589. var data = img.data;
  590. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 3) {
  591. data[i] = arr[j];
  592. data[i + 1] = arr[j + 1];
  593. data[i + 2] = arr[j + 2];
  594. data[i + 3] = 255; // Alpha
  595. }
  596. this._drawCtx.putImageData(img, x - vx, y - vy);
  597. },
  598. _bgrxImageData: function (x, y, vx, vy, width, height, arr, offset) {
  599. var img = this._drawCtx.createImageData(width, height);
  600. var data = img.data;
  601. for (var i = 0, j = offset; i < width * height * 4; i += 4, j += 4) {
  602. data[i] = arr[j + 2];
  603. data[i + 1] = arr[j + 1];
  604. data[i + 2] = arr[j];
  605. data[i + 3] = 255; // Alpha
  606. }
  607. this._drawCtx.putImageData(img, x - vx, y - vy);
  608. },
  609. _rgbxImageData: function (x, y, vx, vy, width, height, arr, offset) {
  610. // NB(directxman12): arr must be an Type Array view
  611. var img;
  612. if (SUPPORTS_IMAGEDATA_CONSTRUCTOR) {
  613. img = new ImageData(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4), width, height);
  614. } else {
  615. img = this._drawCtx.createImageData(width, height);
  616. img.data.set(new Uint8ClampedArray(arr.buffer, arr.byteOffset, width * height * 4));
  617. }
  618. this._drawCtx.putImageData(img, x - vx, y - vy);
  619. },
  620. _cmapImageData: function (x, y, vx, vy, width, height, arr, offset) {
  621. var img = this._drawCtx.createImageData(width, height);
  622. var data = img.data;
  623. var cmap = this._colourMap;
  624. for (var i = 0, j = offset; i < width * height * 4; i += 4, j++) {
  625. var bgr = cmap[arr[j]];
  626. data[i] = bgr[2];
  627. data[i + 1] = bgr[1];
  628. data[i + 2] = bgr[0];
  629. data[i + 3] = 255; // Alpha
  630. }
  631. this._drawCtx.putImageData(img, x - vx, y - vy);
  632. },
  633. _scan_renderQ: function () {
  634. var ready = true;
  635. while (ready && this._renderQ.length > 0) {
  636. var a = this._renderQ[0];
  637. switch (a.type) {
  638. case 'copy':
  639. this.copyImage(a.old_x, a.old_y, a.x, a.y, a.width, a.height, true);
  640. break;
  641. case 'fill':
  642. this.fillRect(a.x, a.y, a.width, a.height, a.color, true);
  643. break;
  644. case 'blit':
  645. this.blitImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  646. break;
  647. case 'blitRgb':
  648. this.blitRgbImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  649. break;
  650. case 'blitRgbx':
  651. this.blitRgbxImage(a.x, a.y, a.width, a.height, a.data, 0, true);
  652. break;
  653. case 'img':
  654. if (a.img.complete) {
  655. this.drawImage(a.img, a.x, a.y);
  656. } else {
  657. // We need to wait for this image to 'load'
  658. // to keep things in-order
  659. ready = false;
  660. }
  661. break;
  662. }
  663. if (ready) {
  664. this._renderQ.shift();
  665. }
  666. }
  667. if (this._renderQ.length > 0) {
  668. requestAnimFrame(this._scan_renderQ.bind(this));
  669. }
  670. },
  671. };
  672. Util.make_properties(Display, [
  673. ['target', 'wo', 'dom'], // Canvas element for rendering
  674. ['context', 'ro', 'raw'], // Canvas 2D context for rendering (read-only)
  675. ['logo', 'rw', 'raw'], // Logo to display when cleared: {"width": w, "height": h, "data": data}
  676. ['true_color', 'rw', 'bool'], // Use true-color pixel data
  677. ['colourMap', 'rw', 'arr'], // Colour map array (when not true-color)
  678. ['scale', 'rw', 'float'], // Display area scale factor 0.0 - 1.0
  679. ['viewport', 'rw', 'bool'], // Use viewport clipping
  680. ['width', 'rw', 'int'], // Display area width
  681. ['height', 'rw', 'int'], // Display area height
  682. ['maxWidth', 'rw', 'int'], // Viewport max width (0 if disabled)
  683. ['maxHeight', 'rw', 'int'], // Viewport max height (0 if disabled)
  684. ['render_mode', 'ro', 'str'], // Canvas rendering mode (read-only)
  685. ['prefer_js', 'rw', 'str'], // Prefer Javascript over canvas methods
  686. ['cursor_uri', 'rw', 'raw'] // Can we render cursor using data URI
  687. ]);
  688. // Class Methods
  689. Display.changeCursor = function (target, pixels, mask, hotx, hoty, w0, h0, cmap) {
  690. var w = w0;
  691. var h = h0;
  692. if (h < w) {
  693. h = w; // increase h to make it square
  694. } else {
  695. w = h; // increase w to make it square
  696. }
  697. var cur = [];
  698. // Push multi-byte little-endian values
  699. cur.push16le = function (num) {
  700. this.push(num & 0xFF, (num >> 8) & 0xFF);
  701. };
  702. cur.push32le = function (num) {
  703. this.push(num & 0xFF,
  704. (num >> 8) & 0xFF,
  705. (num >> 16) & 0xFF,
  706. (num >> 24) & 0xFF);
  707. };
  708. var IHDRsz = 40;
  709. var RGBsz = w * h * 4;
  710. var XORsz = Math.ceil((w * h) / 8.0);
  711. var ANDsz = Math.ceil((w * h) / 8.0);
  712. cur.push16le(0); // 0: Reserved
  713. cur.push16le(2); // 2: .CUR type
  714. cur.push16le(1); // 4: Number of images, 1 for non-animated ico
  715. // Cursor #1 header (ICONDIRENTRY)
  716. cur.push(w); // 6: width
  717. cur.push(h); // 7: height
  718. cur.push(0); // 8: colors, 0 -> true-color
  719. cur.push(0); // 9: reserved
  720. cur.push16le(hotx); // 10: hotspot x coordinate
  721. cur.push16le(hoty); // 12: hotspot y coordinate
  722. cur.push32le(IHDRsz + RGBsz + XORsz + ANDsz);
  723. // 14: cursor data byte size
  724. cur.push32le(22); // 18: offset of cursor data in the file
  725. // Cursor #1 InfoHeader (ICONIMAGE/BITMAPINFO)
  726. cur.push32le(IHDRsz); // 22: InfoHeader size
  727. cur.push32le(w); // 26: Cursor width
  728. cur.push32le(h * 2); // 30: XOR+AND height
  729. cur.push16le(1); // 34: number of planes
  730. cur.push16le(32); // 36: bits per pixel
  731. cur.push32le(0); // 38: Type of compression
  732. cur.push32le(XORsz + ANDsz);
  733. // 42: Size of Image
  734. cur.push32le(0); // 46: reserved
  735. cur.push32le(0); // 50: reserved
  736. cur.push32le(0); // 54: reserved
  737. cur.push32le(0); // 58: reserved
  738. // 62: color data (RGBQUAD icColors[])
  739. var y, x;
  740. for (y = h - 1; y >= 0; y--) {
  741. for (x = 0; x < w; x++) {
  742. if (x >= w0 || y >= h0) {
  743. cur.push(0); // blue
  744. cur.push(0); // green
  745. cur.push(0); // red
  746. cur.push(0); // alpha
  747. } else {
  748. var idx = y * Math.ceil(w0 / 8) + Math.floor(x / 8);
  749. var alpha = (mask[idx] << (x % 8)) & 0x80 ? 255 : 0;
  750. if (cmap) {
  751. idx = (w0 * y) + x;
  752. var rgb = cmap[pixels[idx]];
  753. cur.push(rgb[2]); // blue
  754. cur.push(rgb[1]); // green
  755. cur.push(rgb[0]); // red
  756. cur.push(alpha); // alpha
  757. } else {
  758. idx = ((w0 * y) + x) * 4;
  759. cur.push(pixels[idx + 2]); // blue
  760. cur.push(pixels[idx + 1]); // green
  761. cur.push(pixels[idx]); // red
  762. cur.push(alpha); // alpha
  763. }
  764. }
  765. }
  766. }
  767. // XOR/bitmask data (BYTE icXOR[])
  768. // (ignored, just needs to be the right size)
  769. for (y = 0; y < h; y++) {
  770. for (x = 0; x < Math.ceil(w / 8); x++) {
  771. cur.push(0);
  772. }
  773. }
  774. // AND/bitmask data (BYTE icAND[])
  775. // (ignored, just needs to be the right size)
  776. for (y = 0; y < h; y++) {
  777. for (x = 0; x < Math.ceil(w / 8); x++) {
  778. cur.push(0);
  779. }
  780. }
  781. var url = 'data:image/x-icon;base64,' + Base64.encode(cur);
  782. target.style.cursor = 'url(' + url + ')' + hotx + ' ' + hoty + ', default';
  783. };
  784. })();