From 872fd8ffb1e7dea9e277d2302614a69500757a00 Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Tue, 2 Aug 2016 19:56:57 -0400 Subject: [PATCH 1/7] Initial graphics implementation --- js/core/graphics/constants.js | 35 ++++++++++++++++++++++++++++++ js/core/graphics/index.js | 40 +++++++++++++++++++++++++++++++++++ js/core/index.js | 2 ++ 3 files changed, 77 insertions(+) create mode 100644 js/core/graphics/constants.js create mode 100644 js/core/graphics/index.js diff --git a/js/core/graphics/constants.js b/js/core/graphics/constants.js new file mode 100644 index 000000000..e19a05dab --- /dev/null +++ b/js/core/graphics/constants.js @@ -0,0 +1,35 @@ +'use strict'; + +module.exports = { + VBE_DISPI_IOPORT_INDEX: 0x01CE, + VBE_DISPI_IOPORT_DATA: 0x01CF, + VBE_DISPI_INDEX_ID: 0x0, + VBE_DISPI_INDEX_XRES: 0x1, + VBE_DISPI_INDEX_YRES: 0x2, + VBE_DISPI_INDEX_BPP: 0x3, + VBE_DISPI_INDEX_ENABLE: 0x4, + VBE_DISPI_INDEX_BANK: 0x5, + VBE_DISPI_INDEX_VIRT_WIDTH: 0x6, + VBE_DISPI_INDEX_VIRT_HEIGHT: 0x7, + VBE_DISPI_INDEX_X_OFFSET: 0x8, + VBE_DISPI_INDEX_Y_OFFSET: 0x9, + VBE_DISPI_INDEX_VIDEO_MEMORY_64K: 0xa, + VBE_DISPI_BPP_4: 0x04, + VBE_DISPI_BPP_8: 0x08, + VBE_DISPI_BPP_15: 0x0F, + VBE_DISPI_BPP_16: 0x10, + VBE_DISPI_BPP_24: 0x18, + VBE_DISPI_BPP_32: 0x20, + VBE_DISPI_NOCLEARMEM: 0x80, + VBE_DISPI_DISABLED: 0x00, + VBE_DISPI_ENABLED: 0x01, + VBE_DISPI_LFB_ENABLED: 0x40, + VBE_DISPI_ID0: 0xB0C0, + VBE_DISPI_ID1: 0xB0C1, + VBE_DISPI_ID2: 0xB0C2, + VBE_DISPI_ID3: 0xB0C3, + VBE_DISPI_ID4: 0xB0C4, + VBE_DISPI_ID5: 0xB0C5, + VBE_DISPI_GETCAP: 0x02, + VBE_DISPI_8BIT_DAC: 0x20, +} diff --git a/js/core/graphics/index.js b/js/core/graphics/index.js new file mode 100644 index 000000000..d51efcd5c --- /dev/null +++ b/js/core/graphics/index.js @@ -0,0 +1,40 @@ +'use strict'; + +const { ioPort, physicalMemory } = require('../driver-utils'); +const constants = require('./constants'); +const vbe_dispi_ioport_index = ioPort(constants.VBE_DISPI_IOPORT_INDEX); +const vbe_dispi_ioport_data = ioPort(constants.VBE_DISPI_IOPORT_DATA); + +function writeBgaRegister(index, data) { + vbe_dispi_ioport_index.write16(index >>> 0); + vbe_dispi_ioport_data.write16(data >>> 0); +} +function readBgaRegister(index) { + vbe_dispi_ioport_index.write16(index >>> 0); + return vbe_dispi_ioport_data.read16(); +} + +module.exports = { + graphicsAvailable() { + const bgaVersion = readBgaRegister(constants.VBE_DISPI_INDEX_ID); + for (const i of [0, 1, 2, 3, 4, 5]) { + if (bgaVersion === constants[`VBE_DISPI_ID${i}`]) { + return true; + } + } + return false; + }, + enableGraphics(width, height, bitDepth, useLFB, clearVideoMemory) { + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); + writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); + writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); + writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | + (useLFB ? constants.VBE_DISPI_LFB_ENABLED : 0) | + (clearVideoMemory ? 0 : constants.VBE_DISPI_NOCLEARMEM)); + }, + getDisplayBuffer(width, height) { + return physicalMemory(0xa0000, (width * height) * 2).buffer(); + }, + constants, +}; diff --git a/js/core/index.js b/js/core/index.js index ebddb3600..647f65af3 100644 --- a/js/core/index.js +++ b/js/core/index.js @@ -23,6 +23,7 @@ const ps2 = require('./ps2'); const pci = require('./pci'); const net = require('./net'); const stdio = require('./stdio'); +const graphics = require('./graphics'); class Runtime { constructor() { @@ -34,6 +35,7 @@ class Runtime { allocator, net, stdio, + graphics, machine: { reboot: __SYSCALL.reboot, shutdown: () => __SYSCALL.acpiEnterSleepState(5), From ad867f7b19092c33210182e57a35083e085d6255 Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Wed, 3 Aug 2016 14:35:59 -0400 Subject: [PATCH 2/7] Use LFB for graphics --- js/core/graphics/index.js | 12 +++++------- js/driver/bga/index.js | 25 +++++++++++++++++++++++++ js/index.js | 1 + 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 js/driver/bga/index.js diff --git a/js/core/graphics/index.js b/js/core/graphics/index.js index d51efcd5c..160a478b6 100644 --- a/js/core/graphics/index.js +++ b/js/core/graphics/index.js @@ -1,6 +1,6 @@ 'use strict'; -const { ioPort, physicalMemory } = require('../driver-utils'); +const { ioPort } = require('../driver-utils'); const constants = require('./constants'); const vbe_dispi_ioport_index = ioPort(constants.VBE_DISPI_IOPORT_INDEX); const vbe_dispi_ioport_data = ioPort(constants.VBE_DISPI_IOPORT_DATA); @@ -24,17 +24,15 @@ module.exports = { } return false; }, - enableGraphics(width, height, bitDepth, useLFB, clearVideoMemory) { + enableGraphics(width, height, bitDepth) { writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | - (useLFB ? constants.VBE_DISPI_LFB_ENABLED : 0) | - (clearVideoMemory ? 0 : constants.VBE_DISPI_NOCLEARMEM)); + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); }, - getDisplayBuffer(width, height) { - return physicalMemory(0xa0000, (width * height) * 2).buffer(); + disableGraphics() { + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); }, constants, }; diff --git a/js/driver/bga/index.js b/js/driver/bga/index.js new file mode 100644 index 000000000..02e74b7b3 --- /dev/null +++ b/js/driver/bga/index.js @@ -0,0 +1,25 @@ +'use strict'; + +let buf = null; +const callbacks = []; + +const driver = { + init(device) { + buf = device.bars[0].resource; + for (const cb of callbacks) { + cb(); + } + }, + reset() {}, +}; + +runtime.graphics.getDisplayBuffer = () => new Promise((resolve, reject) => { + if (buf) { + resolve(buf.buffer()); + } else { + callbacks.push(() => { + resolve(buf.buffer()); + }); + } +}); +runtime.pci.addDriver(0x1234, 0x1111, driver); diff --git a/js/index.js b/js/index.js index fd76ddecd..cb49cbbef 100644 --- a/js/index.js +++ b/js/index.js @@ -63,6 +63,7 @@ runtime.shell.setCommand('reboot', (args, env, cb) => { // Start device drivers require('./driver/ps2'); require('./driver/virtio'); +require('./driver/bga'); // Set time require('./core/cmos-time'); // load cmos From 7660783123e597838cd698e3ea3f102b7fdf9ae5 Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Thu, 4 Aug 2016 16:47:55 -0400 Subject: [PATCH 3/7] Backend-ify graphics subsystem --- js/core/graphics/graphics-renderer.js | 43 +++++++++++ js/core/graphics/index.js | 44 +++++------- js/core/graphics/renderers.js | 41 +++++++++++ js/core/graphics/screen.js | 66 +++++++++++++++++ js/{core/graphics => driver/bga}/constants.js | 0 js/driver/bga/index.js | 71 +++++++++++++++---- 6 files changed, 222 insertions(+), 43 deletions(-) create mode 100644 js/core/graphics/graphics-renderer.js create mode 100644 js/core/graphics/renderers.js create mode 100644 js/core/graphics/screen.js rename js/{core/graphics => driver/bga}/constants.js (100%) diff --git a/js/core/graphics/graphics-renderer.js b/js/core/graphics/graphics-renderer.js new file mode 100644 index 000000000..3e2b28e6f --- /dev/null +++ b/js/core/graphics/graphics-renderer.js @@ -0,0 +1,43 @@ +// Copyright 2016-present runtime.js project authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const nameSymbol = Symbol('name'); + +class GraphicsRenderer { + constructor(name = '') { + this[nameSymbol] = name; + this.ongetpixel = null; + this.onenablegraphics = null; + this.constants = {}; + } + get name() { + return this[nameSymbol]; + } + getPixel(x, y) { + if (!this.ongetpixel) { + throw new Error('renderer not initialized'); + } + return this.ongetpixel(x, y); + } + enableGraphics(width, height, bitDepth) { + if (!this.onenablegraphics) { + throw new Error('renderer not initialized'); + } + this.onenablegraphics(width, height, bitDepth); + } +} + +module.exports = GraphicsRenderer; diff --git a/js/core/graphics/index.js b/js/core/graphics/index.js index 160a478b6..142d88b96 100644 --- a/js/core/graphics/index.js +++ b/js/core/graphics/index.js @@ -1,38 +1,26 @@ 'use strict'; -const { ioPort } = require('../driver-utils'); -const constants = require('./constants'); -const vbe_dispi_ioport_index = ioPort(constants.VBE_DISPI_IOPORT_INDEX); -const vbe_dispi_ioport_data = ioPort(constants.VBE_DISPI_IOPORT_DATA); - -function writeBgaRegister(index, data) { - vbe_dispi_ioport_index.write16(index >>> 0); - vbe_dispi_ioport_data.write16(data >>> 0); -} -function readBgaRegister(index) { - vbe_dispi_ioport_index.write16(index >>> 0); - return vbe_dispi_ioport_data.read16(); -} +const GraphicsRenderer = require('./graphics-renderer'); +const { Screen, symbols: screenSymbols } = require('./screen'); +const renderers = require('./renderers'); +const screen = new Screen(); module.exports = { graphicsAvailable() { - const bgaVersion = readBgaRegister(constants.VBE_DISPI_INDEX_ID); - for (const i of [0, 1, 2, 3, 4, 5]) { - if (bgaVersion === constants[`VBE_DISPI_ID${i}`]) { - return true; - } - } - return false; + return renderers.renderersAvailable(); }, enableGraphics(width, height, bitDepth) { - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); - writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); - writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); - writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); + const renderer = renderers.getDefaultRenderer(); + renderer.enableGraphics(width, height, bitDepth); + screen[screenSymbols.reset](); + screen[screenSymbols.init](width, height, bitDepth, renderer); }, - disableGraphics() { - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); + GraphicsRenderer, + get screen() { + return screen; }, - constants, + addRenderer: renderers.addRenderer, + get constants() { + return renderers.getConstants(); + } }; diff --git a/js/core/graphics/renderers.js b/js/core/graphics/renderers.js new file mode 100644 index 000000000..bf1a67596 --- /dev/null +++ b/js/core/graphics/renderers.js @@ -0,0 +1,41 @@ +// Copyright 2016-present runtime.js project authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +let defaultRenderer = null; +const availableRenderers = new Map(); + +module.exports = { + addRenderer(renderer) { + availableRenderers.set(renderer.name, renderer); + + console.log(`[graphics] using renderer ${renderer.name}`); + + defaultRenderer = renderer; + }, + renderersAvailable() { + return availableRenderers.size !== 0; + }, + getDefaultRenderer() { + return defaultRenderer; + }, + getConstants() { + const consts = {}; + for (const renderer of availableRenderers.values()) { + Object.assign(consts, renderer.constants); + } + return consts; + } +}; diff --git a/js/core/graphics/screen.js b/js/core/graphics/screen.js new file mode 100644 index 000000000..908e4cfbc --- /dev/null +++ b/js/core/graphics/screen.js @@ -0,0 +1,66 @@ +// Copyright 2016-present runtime.js project authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const Pixel = require('./pixel'); + +// The properties `width`, `height`, and `bitDepth` are +// Symbols because they shouldn't be set. +const symbols = { + width: Symbol('width'), + height: Symbol('height'), + bitDepth: Symbol('bitDepth'), + init: Symbol('init'), + reset: Symbol('reset'), + initialized: Symbol('initialized'), + renderer: Symbol('renderer'), +}; + +class Screen { + constructor() { + this[symbols.reset](); + } + [symbols.init](width, height, bitDepth, renderer) { + this[symbols.width] = width; + this[symbols.height] = height; + this[symbols.bitDepth] = bitDepth; + this[symbols.renderer] = renderer; + this[symbols.initialized] = true; + } + [symbols.reset]() { + this[symbols.width] = 0; + this[symbols.height] = 0; + this[symbols.bitDepth] = 0; + this[symbols.renderer] = null; + this[symbols.initialized] = false; + } + get(x, y) { + return this[symbols.renderer].getPixel(x, y); + } + get width() { + return this[symbols.width]; + } + get height() { + return this[symbols.height]; + } + get bitDepth() { + return this[symbols.bitDepth]; + } +} + +module.exports = { + Screen, + symbols, +}; diff --git a/js/core/graphics/constants.js b/js/driver/bga/constants.js similarity index 100% rename from js/core/graphics/constants.js rename to js/driver/bga/constants.js diff --git a/js/driver/bga/index.js b/js/driver/bga/index.js index 02e74b7b3..276707e24 100644 --- a/js/driver/bga/index.js +++ b/js/driver/bga/index.js @@ -1,25 +1,66 @@ 'use strict'; -let buf = null; -const callbacks = []; +const { ioPort } = require('../../core/driver-utils'); +const constants = require('./constants'); +const vbe_dispi_ioport_index = ioPort(constants.VBE_DISPI_IOPORT_INDEX); +const vbe_dispi_ioport_data = ioPort(constants.VBE_DISPI_IOPORT_DATA); + +function writeBgaRegister(index, data) { + vbe_dispi_ioport_index.write16(index >>> 0); + vbe_dispi_ioport_data.write16(data >>> 0); +} +function readBgaRegister(index) { + vbe_dispi_ioport_index.write16(index >>> 0); + return vbe_dispi_ioport_data.read16(); +} +function bgaAvailable() { + const bgaVersion = readBgaRegister(constants.VBE_DISPI_INDEX_ID); + for (const i of [0, 1, 2, 3, 4, 5]) { + if (bgaVersion === constants[`VBE_DISPI_ID${i}`]) { + return true; + } + } + return false; +} const driver = { init(device) { - buf = device.bars[0].resource; - for (const cb of callbacks) { - cb(); - } + const buf = new Uint8Array(device.bars[0].resource.buffer()); + const renderer = new runtime.graphics.GraphicsRenderer('bga'); + renderer.onenablegraphics = (width, height, bitDepth) => { + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); + writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); + writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); + writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); + }; + renderer.ongetpixel = (x, y) => { + const where = ((y * runtime.graphics.screen.width) + x) * (runtime.graphics.screen.bitDepth / 8); + return { + get r() { + return buf[where + 2]; + }, + get g() { + return buf[where + 1]; + }, + get b() { + return buf[where]; + }, + set r(val) { + buf[where + 2] = val; + }, + set g(val) { + buf[where + 1] = val; + }, + set b(val) { + buf[where] = val; + }, + }; + }; + renderer.constants = constants; + runtime.graphics.addRenderer(renderer); }, reset() {}, }; -runtime.graphics.getDisplayBuffer = () => new Promise((resolve, reject) => { - if (buf) { - resolve(buf.buffer()); - } else { - callbacks.push(() => { - resolve(buf.buffer()); - }); - } -}); runtime.pci.addDriver(0x1234, 0x1111, driver); From 99b4d2549d1c4b069dedfd4e875fd6344a755eea Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Thu, 4 Aug 2016 16:51:58 -0400 Subject: [PATCH 4/7] Lint js --- js/core/graphics/index.js | 2 +- js/core/graphics/renderers.js | 2 +- js/core/graphics/screen.js | 2 - js/driver/bga/constants.js | 2 +- js/driver/bga/index.js | 92 ++++++++++++++++++----------------- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/js/core/graphics/index.js b/js/core/graphics/index.js index 142d88b96..95c12cd51 100644 --- a/js/core/graphics/index.js +++ b/js/core/graphics/index.js @@ -22,5 +22,5 @@ module.exports = { addRenderer: renderers.addRenderer, get constants() { return renderers.getConstants(); - } + }, }; diff --git a/js/core/graphics/renderers.js b/js/core/graphics/renderers.js index bf1a67596..8841dfb06 100644 --- a/js/core/graphics/renderers.js +++ b/js/core/graphics/renderers.js @@ -37,5 +37,5 @@ module.exports = { Object.assign(consts, renderer.constants); } return consts; - } + }, }; diff --git a/js/core/graphics/screen.js b/js/core/graphics/screen.js index 908e4cfbc..35e5f70d7 100644 --- a/js/core/graphics/screen.js +++ b/js/core/graphics/screen.js @@ -14,8 +14,6 @@ 'use strict'; -const Pixel = require('./pixel'); - // The properties `width`, `height`, and `bitDepth` are // Symbols because they shouldn't be set. const symbols = { diff --git a/js/driver/bga/constants.js b/js/driver/bga/constants.js index e19a05dab..a8d55f6e7 100644 --- a/js/driver/bga/constants.js +++ b/js/driver/bga/constants.js @@ -32,4 +32,4 @@ module.exports = { VBE_DISPI_ID5: 0xB0C5, VBE_DISPI_GETCAP: 0x02, VBE_DISPI_8BIT_DAC: 0x20, -} +}; diff --git a/js/driver/bga/index.js b/js/driver/bga/index.js index 276707e24..ca691742c 100644 --- a/js/driver/bga/index.js +++ b/js/driver/bga/index.js @@ -2,16 +2,16 @@ const { ioPort } = require('../../core/driver-utils'); const constants = require('./constants'); -const vbe_dispi_ioport_index = ioPort(constants.VBE_DISPI_IOPORT_INDEX); -const vbe_dispi_ioport_data = ioPort(constants.VBE_DISPI_IOPORT_DATA); +const vbeDispiIoportIndex = ioPort(constants.VBE_DISPI_IOPORT_INDEX); +const vbeDispiIoportData = ioPort(constants.VBE_DISPI_IOPORT_DATA); function writeBgaRegister(index, data) { - vbe_dispi_ioport_index.write16(index >>> 0); - vbe_dispi_ioport_data.write16(data >>> 0); + vbeDispiIoportIndex.write16(index >>> 0); + vbeDispiIoportData.write16(data >>> 0); } function readBgaRegister(index) { - vbe_dispi_ioport_index.write16(index >>> 0); - return vbe_dispi_ioport_data.read16(); + vbeDispiIoportIndex.write16(index >>> 0); + return vbeDispiIoportData.read16(); } function bgaAvailable() { const bgaVersion = readBgaRegister(constants.VBE_DISPI_INDEX_ID); @@ -23,44 +23,46 @@ function bgaAvailable() { return false; } -const driver = { - init(device) { - const buf = new Uint8Array(device.bars[0].resource.buffer()); - const renderer = new runtime.graphics.GraphicsRenderer('bga'); - renderer.onenablegraphics = (width, height, bitDepth) => { - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); - writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); - writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); - writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); - writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); - }; - renderer.ongetpixel = (x, y) => { - const where = ((y * runtime.graphics.screen.width) + x) * (runtime.graphics.screen.bitDepth / 8); - return { - get r() { - return buf[where + 2]; - }, - get g() { - return buf[where + 1]; - }, - get b() { - return buf[where]; - }, - set r(val) { - buf[where + 2] = val; - }, - set g(val) { - buf[where + 1] = val; - }, - set b(val) { - buf[where] = val; - }, +if (bgaAvailable()) { + const driver = { + init(device) { + const buf = new Uint8Array(device.bars[0].resource.buffer()); + const renderer = new runtime.graphics.GraphicsRenderer('bga'); + renderer.onenablegraphics = (width, height, bitDepth) => { + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_DISABLED); + writeBgaRegister(constants.VBE_DISPI_INDEX_XRES, width); + writeBgaRegister(constants.VBE_DISPI_INDEX_YRES, height); + writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); + writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); }; - }; - renderer.constants = constants; - runtime.graphics.addRenderer(renderer); - }, - reset() {}, -}; + renderer.ongetpixel = (x, y) => { + const where = ((y * runtime.graphics.screen.width) + x) * (runtime.graphics.screen.bitDepth / 8); + return { + get r() { + return buf[where + 2]; + }, + get g() { + return buf[where + 1]; + }, + get b() { + return buf[where]; + }, + set r(val) { + buf[where + 2] = val; + }, + set g(val) { + buf[where + 1] = val; + }, + set b(val) { + buf[where] = val; + }, + }; + }; + renderer.constants = constants; + runtime.graphics.addRenderer(renderer); + }, + reset() {}, + }; -runtime.pci.addDriver(0x1234, 0x1111, driver); + runtime.pci.addDriver(0x1234, 0x1111, driver); +} From 01eaf86460a08350ee58f5811cbe3782db599367 Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Sun, 14 Aug 2016 13:53:55 -0400 Subject: [PATCH 5/7] Expose raw buffer instead of abstracting --- js/core/graphics/graphics-renderer.js | 9 +++------ js/core/graphics/index.js | 3 +++ js/core/graphics/screen.js | 3 --- js/driver/bga/index.js | 24 +----------------------- 4 files changed, 7 insertions(+), 32 deletions(-) diff --git a/js/core/graphics/graphics-renderer.js b/js/core/graphics/graphics-renderer.js index 3e2b28e6f..bec84a42b 100644 --- a/js/core/graphics/graphics-renderer.js +++ b/js/core/graphics/graphics-renderer.js @@ -19,18 +19,15 @@ const nameSymbol = Symbol('name'); class GraphicsRenderer { constructor(name = '') { this[nameSymbol] = name; - this.ongetpixel = null; + this.ongetbuffer = null; this.onenablegraphics = null; this.constants = {}; } get name() { return this[nameSymbol]; } - getPixel(x, y) { - if (!this.ongetpixel) { - throw new Error('renderer not initialized'); - } - return this.ongetpixel(x, y); + get displayBuffer() { + return this.ongetbuffer(); } enableGraphics(width, height, bitDepth) { if (!this.onenablegraphics) { diff --git a/js/core/graphics/index.js b/js/core/graphics/index.js index 95c12cd51..6a9744625 100644 --- a/js/core/graphics/index.js +++ b/js/core/graphics/index.js @@ -19,6 +19,9 @@ module.exports = { get screen() { return screen; }, + get displayBuffer() { + return renderers.getDefaultRenderer().displayBuffer; + }, addRenderer: renderers.addRenderer, get constants() { return renderers.getConstants(); diff --git a/js/core/graphics/screen.js b/js/core/graphics/screen.js index 35e5f70d7..6c4db80fb 100644 --- a/js/core/graphics/screen.js +++ b/js/core/graphics/screen.js @@ -44,9 +44,6 @@ class Screen { this[symbols.renderer] = null; this[symbols.initialized] = false; } - get(x, y) { - return this[symbols.renderer].getPixel(x, y); - } get width() { return this[symbols.width]; } diff --git a/js/driver/bga/index.js b/js/driver/bga/index.js index ca691742c..ce6c8ded3 100644 --- a/js/driver/bga/index.js +++ b/js/driver/bga/index.js @@ -35,29 +35,7 @@ if (bgaAvailable()) { writeBgaRegister(constants.VBE_DISPI_INDEX_BPP, bitDepth); writeBgaRegister(constants.VBE_DISPI_INDEX_ENABLE, constants.VBE_DISPI_ENABLED | constants.VBE_DISPI_LFB_ENABLED); }; - renderer.ongetpixel = (x, y) => { - const where = ((y * runtime.graphics.screen.width) + x) * (runtime.graphics.screen.bitDepth / 8); - return { - get r() { - return buf[where + 2]; - }, - get g() { - return buf[where + 1]; - }, - get b() { - return buf[where]; - }, - set r(val) { - buf[where + 2] = val; - }, - set g(val) { - buf[where + 1] = val; - }, - set b(val) { - buf[where] = val; - }, - }; - }; + renderer.ongetbuffer = () => buf; renderer.constants = constants; runtime.graphics.addRenderer(renderer); }, From f2feb74e08f3d447d5266b01d06fa4957f16c873 Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Sun, 14 Aug 2016 13:58:16 -0400 Subject: [PATCH 6/7] Check for ongetbuffer before trying to use it --- js/core/graphics/graphics-renderer.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js/core/graphics/graphics-renderer.js b/js/core/graphics/graphics-renderer.js index bec84a42b..83feb24b7 100644 --- a/js/core/graphics/graphics-renderer.js +++ b/js/core/graphics/graphics-renderer.js @@ -27,6 +27,9 @@ class GraphicsRenderer { return this[nameSymbol]; } get displayBuffer() { + if (!this.ongetbuffer) { + throw new Error('renderer not initialized'); + } return this.ongetbuffer(); } enableGraphics(width, height, bitDepth) { From 6828f3f5accb7747a0c6b2cb1aaa02f5ed0d139e Mon Sep 17 00:00:00 2001 From: Face Kapow Date: Sun, 14 Aug 2016 14:00:52 -0400 Subject: [PATCH 7/7] Add runtime.graphics.screen.bufferFormat --- js/core/graphics/screen.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js/core/graphics/screen.js b/js/core/graphics/screen.js index 6c4db80fb..8f767a8ba 100644 --- a/js/core/graphics/screen.js +++ b/js/core/graphics/screen.js @@ -53,6 +53,9 @@ class Screen { get bitDepth() { return this[symbols.bitDepth]; } + get bufferFormat() { + return this[symbols.renderer].name; + } } module.exports = {