Path: blob/main/misc/emulator/gba/user_scripts/IodineGBAGraphicsGlueCode.js
28515 views
"use strict";1/*2Copyright (C) 2012-2014 Grant Galitz34Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:56The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.78THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.9*/10function GlueCodeGfx() {11this.didRAF = false; //Set when rAF has been used.12this.graphicsFound = 0; //Do we have graphics output sink found yet?13this.offscreenWidth = 240; //Width of the GBA screen.14this.offscreenHeight = 160; //Height of the GBA screen.15this.doSmoothing = true;16//Cache some frame buffer lengths:17var offscreenRGBCount = this.offscreenWidth * this.offscreenHeight * 3;18this.swizzledFrameFree = [getUint8Array(offscreenRGBCount), getUint8Array(offscreenRGBCount)];19this.swizzledFrameReady = [];20this.initializeGraphicsBuffer(); //Pre-set the swizzled buffer for first frame.21}22GlueCodeGfx.prototype.attachCanvas = function (canvas) {23this.canvas = canvas;24this.graphicsFound = this.initializeCanvasTarget();25this.setSmoothScaling(this.doSmoothing);26}27GlueCodeGfx.prototype.detachCanvas = function () {28this.canvas = null;29}30GlueCodeGfx.prototype.recomputeDimension = function () {31//Cache some dimension info:32this.canvasLastWidth = this.canvas.clientWidth;33this.canvasLastHeight = this.canvas.clientHeight;34if (window.mozRequestAnimationFrame) { //Sniff out firefox for selecting this path.35//Set target as unscaled:36this.onscreenWidth = this.canvas.width = this.offscreenWidth;37this.onscreenHeight = this.canvas.height = this.offscreenHeight;38}39else {40//Set target canvas as scaled:41this.onscreenWidth = this.canvas.width = this.canvas.clientWidth;42this.onscreenHeight = this.canvas.height = this.canvas.clientHeight;43}44}45GlueCodeGfx.prototype.initializeCanvasTarget = function () {46try {47//Obtain dimensional information:48this.recomputeDimension();49//Get handles on the canvases:50this.canvasOffscreen = document.createElement("canvas");51this.canvasOffscreen.width = this.offscreenWidth;52this.canvasOffscreen.height = this.offscreenHeight;53this.drawContextOffscreen = this.canvasOffscreen.getContext("2d");54this.drawContextOnscreen = this.canvas.getContext("2d");55//Get a CanvasPixelArray buffer:56this.canvasBuffer = this.getBuffer(this.drawContextOffscreen, this.offscreenWidth, this.offscreenHeight);57//Initialize Alpha Channel:58this.initializeAlpha(this.canvasBuffer.data);59//Draw swizzled buffer out as a test:60this.requestDraw();61this.checkRAF();62//Success:63return true;64}65catch (error) {66//Failure:67return false;68}69}70GlueCodeGfx.prototype.setSmoothScaling = function (doSmoothing) {71this.doSmoothing = doSmoothing;72if (this.graphicsFound) {73this.canvas.setAttribute("style", (this.canvas.getAttribute("style") || "") + "; image-rendering: " + ((doSmoothing) ? "auto" : "-webkit-optimize-contrast") + ";" +74"image-rendering: " + ((doSmoothing) ? "optimizeQuality" : "-o-crisp-edges") + ";" +75"image-rendering: " + ((doSmoothing) ? "optimizeQuality" : "-moz-crisp-edges") + ";" +76"-ms-interpolation-mode: " + ((doSmoothing) ? "bicubic" : "nearest-neighbor") + ";");77this.drawContextOnscreen.mozImageSmoothingEnabled = doSmoothing;78this.drawContextOnscreen.webkitImageSmoothingEnabled = doSmoothing;79this.drawContextOnscreen.imageSmoothingEnabled = doSmoothing;80}81}82GlueCodeGfx.prototype.initializeAlpha = function (canvasData) {83var length = canvasData.length;84for (var indexGFXIterate = 3; indexGFXIterate < length; indexGFXIterate += 4) {85canvasData[indexGFXIterate] = 0xFF;86}87}88GlueCodeGfx.prototype.getBuffer = function (canvasContext, width, height) {89//Get a CanvasPixelArray buffer:90var buffer = null;91try {92buffer = this.drawContextOffscreen.createImageData(width, height);93}94catch (error) {95buffer = this.drawContextOffscreen.getImageData(0, 0, width, height);96}97return buffer;98}99GlueCodeGfx.prototype.copyBuffer = function (buffer) {100if (this.graphicsFound) {101if (this.swizzledFrameFree.length == 0) {102if (this.didRAF) {103this.requestDrawSingle();104}105else {106this.swizzledFrameFree.push(this.swizzledFrameReady.shift());107}108}109var swizzledFrame = this.swizzledFrameFree.shift();110var length = swizzledFrame.length;111if (buffer.buffer) {112swizzledFrame.set(buffer);113}114else {115for (var bufferIndex = 0; bufferIndex < length; ++bufferIndex) {116swizzledFrame[bufferIndex] = buffer[bufferIndex];117}118}119this.swizzledFrameReady.push(swizzledFrame);120if (!window.requestAnimationFrame) {121this.requestDraw();122}123else if (!this.didRAF) {124//Prime RAF draw:125var parentObj = this;126window.requestAnimationFrame(function () {127if (parentObj.canvas) {128parentObj.requestRAFDraw();129}130});131}132}133}134GlueCodeGfx.prototype.requestRAFDraw = function () {135this.didRAF = true;136this.requestDraw();137}138GlueCodeGfx.prototype.requestDrawSingle = function () {139if (this.swizzledFrameReady.length > 0) {140var canvasData = this.canvasBuffer.data;141var bufferIndex = 0;142var swizzledFrame = this.swizzledFrameReady.shift();143var length = canvasData.length;144for (var canvasIndex = 0; canvasIndex < length; ++canvasIndex) {145canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];146canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];147canvasData[canvasIndex++] = swizzledFrame[bufferIndex++];148}149this.swizzledFrameFree.push(swizzledFrame);150this.graphicsBlit();151}152}153GlueCodeGfx.prototype.requestDraw = function () {154this.requestDrawSingle();155if (this.didRAF) {156var parentObj = this;157window.requestAnimationFrame(function () {158if (parentObj.canvas) {159parentObj.requestDraw();160}161});162}163}164GlueCodeGfx.prototype.graphicsBlit = function () {165if (this.canvasLastWidth != this.canvas.clientWidth || this.canvasLastHeight != this.canvas.clientHeight) {166this.recomputeDimension();167this.setSmoothScaling(this.doSmoothing);168}169if (this.offscreenWidth == this.onscreenWidth && this.offscreenHeight == this.onscreenHeight) {170//Canvas does not need to scale, draw directly to final:171this.drawContextOnscreen.putImageData(this.canvasBuffer, 0, 0);172}173else {174//Canvas needs to scale, draw to offscreen first:175this.drawContextOffscreen.putImageData(this.canvasBuffer, 0, 0);176//Scale offscreen canvas image onto the final:177this.drawContextOnscreen.drawImage(this.canvasOffscreen, 0, 0, this.onscreenWidth, this.onscreenHeight);178}179}180GlueCodeGfx.prototype.initializeGraphicsBuffer = function () {181//Initialize the first frame to a white screen:182var swizzledFrame = this.swizzledFrameFree.shift();183var length = swizzledFrame.length;184for (var bufferIndex = 0; bufferIndex < length; ++bufferIndex) {185swizzledFrame[bufferIndex] = 0xF8;186}187this.swizzledFrameReady.push(swizzledFrame);188}189GlueCodeGfx.prototype.checkRAF = function () {190window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||191window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;192}193194