Skip to content

Commit

Permalink
make creation of context synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Sidorov committed Oct 20, 2015
1 parent ee73e48 commit dbf0ee2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 76 deletions.
50 changes: 24 additions & 26 deletions examples/text-rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,34 @@ ntk.createClient(main);

function main(err, app) {
var wnd = app.createWindow({}).map();

var pixmap = new Pixmap(app, {parent: wnd, depth: 32, width: 1800, height: 1800});
debugger;
pixmap.getContext('2d', function(err, ctx) {
wnd.getContext('2d', function(err, ctx1) {
var shadow = ctx.createSolidPicture(0, 0, 0, 0.1);
var black = ctx.createSolidPicture(0, 0, 0, 0.9);
debugger
var ctx = pixmap.getContext('2d');
var ctx1 = wnd.getContext('2d');

var shadow = ctx.createSolidPicture(0, 0, 0, 0.1);
var black = ctx.createSolidPicture(0, 0, 0, 0.9);

/*
//TODO
var grad = ctx.createLinearGradient(0,0,200,0);
grad.addColorStop(0, 1, 0, 0, 1);
grad.addColorStop(0.5, 0, 1, 0, 1);
grad.addColorStop(1, 0, 0, 1, 1);
*/
/*
//TODO
var grad = ctx.createLinearGradient(0,0,200,0);
grad.addColorStop(0, 1, 0, 0, 1);
grad.addColorStop(0.5, 0, 1, 0, 1);
grad.addColorStop(1, 0, 0, 1, 1);
*/

//var glyphs = ctx.loadFont('/Library/Fonts/Arial.ttf', 32, 96, 96);
var glyphs = ctx.loadFont('/Library/Fonts/Times New Roman Italic.ttf', 92, 96, 96);
ctx.setFont(glyphs);
//var glyphs = ctx.loadFont('/Library/Fonts/Arial.ttf', 32, 96, 96);
var glyphs = ctx.loadFont('/Library/Fonts/Times New Roman Italic.ttf', 92, 96, 96);
ctx.setFont(glyphs);

wnd.on('mousemove', function(ev) {
console.log(ev.x, ev.y);
ctx.fillRect(0, 0, 1000, 1000, 1, 1, 1, 1)
ctx.setBackground(shadow);
ctx.fillText(process.argv[2], ev.x - 2, ev.y + 5);
ctx.setBackground(black);
ctx.fillText(process.argv[2], ev.x, ev.y);
wnd.on('mousemove', function(ev) {
console.log(ev.x, ev.y);
ctx.fillRect(0, 0, 1000, 1000, 1, 1, 1, 1)
ctx.setBackground(shadow);
ctx.fillText(process.argv[2], ev.x - 2, ev.y + 5);
ctx.setBackground(black);
ctx.fillText(process.argv[2], ev.x, ev.y);

ctx1.draw(ctx);
});
});
ctx1.draw(ctx);
});
}
5 changes: 2 additions & 3 deletions lib/drawable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ util.inherits(Drawable, EventEmitter);

Drawable.prototype.renderingContextFactory = {};

Drawable.prototype.getContext = function(name, callback) {
Drawable.prototype.getContext = function(name, arg1, arg2, arg3) {
var renderingContextFactory = this.renderingContextFactory[name];
if (!renderingContextFactory)
throw new Error('Unknown rendering context:', name);
renderingContextFactory(this, callback);
return this;
return renderingContextFactory(this, arg1, arg2, arg3);
}
4 changes: 2 additions & 2 deletions lib/renderingcontext_2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var GlyphSet = require('./glyphset.js');
var Pixmap = require('./pixmap.js');

module.exports = function(window, callback) {
callback(null, new RenderingContext2d(window));
return new RenderingContext2d(window);
}

function RenderingContext2d(window) {
Expand Down Expand Up @@ -78,7 +78,7 @@ RC.fillText = function(text, x, y) {
RC.draw = function(ctxOther) {
var Render = this.Render;
//ext.Composite = function(op, src, mask, dst, srcX, srcY, maskX, maskY, dstX, dstY, width, height)
console.log('COMPOSITE', ctxOther.picture.id, this.picture.id);
//console.log('COMPOSITE', ctxOther.picture.id, this.picture.id);
Render.Composite(3, ctxOther.picture.id, 0, this.picture.id, 0, 0, 0, 0, 0, 0, 1000, 1000);
}

Expand Down
18 changes: 5 additions & 13 deletions lib/renderingcontext_opengl.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
module.exports = function(window, callback) {
module.exports = function(window, visual) {
// TODO: 1) use glx api to get best visual; 2) cache
require('child_process').exec('glxinfo -i -b', function(error, stdout, stderr) {
if (error)
return callback(error);
var visual = parseInt(stdout);
var gl = new RenderingContextOpenGL(window, visual);
window.display.GLX.QueryExtensionsString(0, function(err, exts) {
if (error)
return callback(error);
gl.extensions = exts.split(' ');
callback(null, gl);
});
});
//require('child_process').exec('glxinfo -i -b', function(error, stdout, stderr) {
var gl = new RenderingContextOpenGL(window, visual);
return gl;
}

function RenderingContextOpenGL(window, visual) {
Expand All @@ -23,6 +14,7 @@ function RenderingContextOpenGL(window, visual) {
var ctx = X.AllocID();
GLX.CreateContext(ctx, visual, 0, 0, 0);
GLX.MakeCurrent(window.id, ctx, 0, function() {});

var gl = GLX.renderPipeline(ctx);
for(key in gl) {
if (key !== 'SwapBuffers') {
Expand Down
5 changes: 3 additions & 2 deletions lib/renderingcontext_x11.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = function(window, callback) {
callback(null, new RenderingContextX11(window));
module.exports = function(window) {
//callback(null, new RenderingContextX11(window));
return new RenderingContextX11(window);
}

function RenderingContextX11(window) {
Expand Down
48 changes: 18 additions & 30 deletions lib/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Window(app, args)

Drawable.call(this);

// shortcuts
// shortcuts
var X = app.X;
this.X = X
this.app = app;
Expand Down Expand Up @@ -51,42 +51,30 @@ function Window(app, args)
Window.cache[this.id] = this;

if (args.title) {
this.setTitle(args.title);
this.setTitle(args.title);
}

X.on('error', console.log);

X.on('event', function(ev) {
// some events more naturally should be routed to parent window, not source window
if ([20, 23].indexOf(ev.type) != -1) {
var listener = X.event_consumers[ev.parent];
if (listener) {
var eventName = xevents.eventName[ev.type];
var win = new Window(app, {id: ev.wid});
ev.window = win;
listener.emit(eventName, ev);
}
}
});

X.event_consumers[this.id] = this;
this.on('event', function(ev) {
var ntkev = ev; // todo: clone
ntkev.window = this;
var eventName = xevents.eventName[ev.type];
if (eventName)
this.emit(eventName, ntkev);
else
console.log("Unknown event: ", ev);
var ntkev = ev; // todo: clone
ntkev.window = this;
var eventName = xevents.eventName[ev.type];
if (eventName)
this.emit(eventName, ntkev);
else
console.log("Unknown event: ", ev);
});
this.on('child-event', function(ev) {
console.log('CHILD EVENT', ee);
})

this.on('newListener', function(name) {
// check eventMask and add handler if missing
var eventMask = xevents.mask[name];
if ( (eventMask & self.eventMask) == 0) {
self.eventMask |= eventMask;
X.ChangeWindowAttributes(self.id, { eventMask: this.eventMask}, function() {});
}
// check eventMask and add handler if missing
var eventMask = xevents.mask[name];
if ( (eventMask & self.eventMask) == 0) {
self.eventMask |= eventMask;
X.ChangeWindowAttributes(self.id, { eventMask: this.eventMask}, function() {});
}
});
}
util.inherits(Window, Drawable);
Expand Down

0 comments on commit dbf0ee2

Please sign in to comment.