start hacking on OpenGL ES2 rendering
This commit is contained in:
1 parent
20f4b21f13
commit
20965e786f
5 files changed
+307
-4
No files matched your search
+11
@@ -5,8 +5,11 @@ version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1.0.1"
|
||||
egli = "0.4.0"
|
||||
euclid = "0.17.0"
|
||||
failure = "0.1.1"
|
||||
freetype = "0.3.0"
|
||||
gl = "0.10.0"
|
||||
harfbuzz-sys = "0.1.15"
|
||||
libc = "0.2.36"
|
||||
mio = "0.6.12"
|
||||
@@ -18,9 +21,17 @@ servo-fontconfig = "0.4.0"
|
||||
toml = "0.4.5"
|
||||
unicode-width = "0.1.4"
|
||||
|
||||
[dependencies.glium]
|
||||
default-features = false
|
||||
version = "0.20.0"
|
||||
|
||||
[dependencies.term]
|
||||
path = "term"
|
||||
|
||||
[dependencies.x11]
|
||||
features = ["xlib"]
|
||||
version = "2.17.2"
|
||||
|
||||
[dependencies.xcb]
|
||||
features = ["shm"]
|
||||
version = "0.8.1"
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
extern crate gl;
|
||||
extern crate egli;
|
||||
extern crate euclid;
|
||||
#[macro_use]
|
||||
extern crate glium;
|
||||
extern crate unicode_width;
|
||||
extern crate harfbuzz_sys;
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
@@ -15,6 +20,7 @@ extern crate serde_derive;
|
||||
extern crate palette;
|
||||
extern crate term;
|
||||
extern crate toml;
|
||||
extern crate x11;
|
||||
#[macro_use]
|
||||
pub mod log;
|
||||
|
||||
|
||||
+162
-2
@@ -1,10 +1,18 @@
|
||||
use egli;
|
||||
use gl;
|
||||
use glium;
|
||||
use glium::backend::Backend;
|
||||
use libc;
|
||||
use resize;
|
||||
use std::convert::From;
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::ops::Deref;
|
||||
use std::os;
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
use std::result;
|
||||
use x11;
|
||||
use xcb;
|
||||
use xcb_util;
|
||||
use xcb_util::ffi::keysyms::{xcb_key_press_lookup_keysym, xcb_key_symbols_alloc,
|
||||
@@ -19,6 +27,7 @@ use palette::Blend;
|
||||
use palette::pixel::Srgb;
|
||||
|
||||
pub struct Connection {
|
||||
pub display: *mut x11::xlib::Display,
|
||||
conn: xcb::Connection,
|
||||
screen_num: i32,
|
||||
pub atom_protocols: xcb::Atom,
|
||||
@@ -28,6 +37,8 @@ pub struct Connection {
|
||||
pub atom_targets: xcb::Atom,
|
||||
keysyms: *mut xcb_key_symbols_t,
|
||||
shm_available: bool,
|
||||
egl_display: Rc<egli::Display>,
|
||||
egl_config: egli::FrameBufferConfigRef,
|
||||
}
|
||||
|
||||
impl Deref for Connection {
|
||||
@@ -38,9 +49,26 @@ impl Deref for Connection {
|
||||
}
|
||||
}
|
||||
|
||||
#[link(name = "X11-xcb")]
|
||||
extern "C" {
|
||||
fn XGetXCBConnection(display: *mut x11::xlib::Display) -> *mut xcb::ffi::xcb_connection_t;
|
||||
fn XSetEventQueueOwner(display: *mut x11::xlib::Display, owner: i32);
|
||||
}
|
||||
|
||||
fn egli_err(err: egli::error::Error) -> Error {
|
||||
format_err!("egli error: {:?}", err)
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn new() -> Result<Connection> {
|
||||
let (conn, screen_num) = xcb::Connection::connect(None)?;
|
||||
let display = unsafe { x11::xlib::XOpenDisplay(ptr::null()) };
|
||||
if display.is_null() {
|
||||
bail!("failed to open display");
|
||||
}
|
||||
let screen_num = unsafe { x11::xlib::XDefaultScreen(display) };
|
||||
let conn = unsafe { xcb::Connection::from_raw_conn(XGetXCBConnection(display)) };
|
||||
unsafe { XSetEventQueueOwner(display, 1) };
|
||||
|
||||
let atom_protocols = xcb::intern_atom(&conn, false, "WM_PROTOCOLS")
|
||||
.get_reply()?
|
||||
.atom();
|
||||
@@ -62,7 +90,33 @@ impl Connection {
|
||||
let reply = xcb::shm::query_version(&conn).get_reply()?;
|
||||
let shm_available = reply.shared_pixmaps();
|
||||
|
||||
let egl_display = egli::Display::from_display_id(display as *mut _).map_err(
|
||||
egli_err,
|
||||
)?;
|
||||
|
||||
let egl_version = egl_display.initialize_and_get_version().map_err(egli_err)?;
|
||||
println!("Using EGL {}", egl_version);
|
||||
|
||||
let configs = egl_display
|
||||
.config_filter()
|
||||
.with_red_size(8)
|
||||
.with_green_size(8)
|
||||
.with_blue_size(8)
|
||||
.with_depth_size(24)
|
||||
.with_surface_type(
|
||||
egli::SurfaceType::WINDOW | egli::SurfaceType::PBUFFER | egli::SurfaceType::PIXMAP,
|
||||
)
|
||||
.with_renderable_type(egli::RenderableType::OPENGL_ES2)
|
||||
.with_conformant(egli::RenderableType::OPENGL_ES2)
|
||||
.choose_configs()
|
||||
.map_err(|e| format_err!("failed to get EGL config: {:?}", e))?;
|
||||
|
||||
let first_config = *configs.first().ok_or(failure::err_msg(
|
||||
"no compatible EGL configuration was found",
|
||||
))?;
|
||||
|
||||
Ok(Connection {
|
||||
display,
|
||||
conn,
|
||||
screen_num,
|
||||
atom_protocols,
|
||||
@@ -72,6 +126,8 @@ impl Connection {
|
||||
atom_utf8_string,
|
||||
atom_xsel_data,
|
||||
atom_targets,
|
||||
egl_display: Rc::new(egl_display),
|
||||
egl_config: first_config,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -115,10 +171,18 @@ pub trait Drawable {
|
||||
fn get_conn(&self) -> &Connection;
|
||||
}
|
||||
|
||||
struct GlState {
|
||||
display: Rc<egli::Display>,
|
||||
surface: egli::Surface,
|
||||
egl_context: egli::Context,
|
||||
}
|
||||
|
||||
/// A Window!
|
||||
pub struct Window<'a> {
|
||||
window_id: xcb::xproto::Window,
|
||||
conn: &'a Connection,
|
||||
gl: Rc<GlState>,
|
||||
glium_context: Rc<glium::backend::Context>,
|
||||
}
|
||||
|
||||
impl<'a> Drawable for Window<'a> {
|
||||
@@ -139,6 +203,7 @@ impl<'a> Window<'a> {
|
||||
failure::err_msg("no screen?"),
|
||||
)?;
|
||||
let window_id = conn.conn().generate_id();
|
||||
|
||||
xcb::create_window_checked(
|
||||
conn.conn(),
|
||||
xcb::COPY_FROM_PARENT as u8,
|
||||
@@ -178,8 +243,51 @@ impl<'a> Window<'a> {
|
||||
&[conn.atom_delete],
|
||||
);
|
||||
|
||||
let surface = conn.egl_display
|
||||
.create_window_surface(conn.egl_config, window_id as *mut _)
|
||||
.map_err(egli_err)?;
|
||||
|
||||
Ok(Window { conn, window_id })
|
||||
let egl_context = conn.egl_display
|
||||
.create_context_with_client_version(
|
||||
conn.egl_config,
|
||||
egli::ContextClientVersion::OpenGlEs2,
|
||||
)
|
||||
.map_err(egli_err)?;
|
||||
|
||||
conn.egl_display
|
||||
.make_current(&surface, &surface, &egl_context)
|
||||
.map_err(egli_err)?;
|
||||
|
||||
gl::load_with(
|
||||
|s| unsafe { mem::transmute(egli::egl::get_proc_address(s)) },
|
||||
);
|
||||
|
||||
let gl_state = Rc::new(GlState {
|
||||
display: Rc::clone(&conn.egl_display),
|
||||
egl_context,
|
||||
surface,
|
||||
});
|
||||
|
||||
let glium_context = unsafe {
|
||||
glium::backend::Context::new(
|
||||
Rc::clone(&gl_state),
|
||||
// we're single threaded, so no need to check contexts
|
||||
false,
|
||||
if cfg!(debug_assertions) {
|
||||
glium::debug::DebugCallbackBehavior::PrintAll
|
||||
//glium::debug::DebugCallbackBehavior::DebugMessageOnError
|
||||
} else {
|
||||
glium::debug::DebugCallbackBehavior::Ignore
|
||||
},
|
||||
)?
|
||||
};
|
||||
|
||||
Ok(Window {
|
||||
conn,
|
||||
window_id,
|
||||
gl: gl_state,
|
||||
glium_context,
|
||||
})
|
||||
}
|
||||
|
||||
/// Change the title for the window manager
|
||||
@@ -191,6 +299,20 @@ impl<'a> Window<'a> {
|
||||
pub fn show(&self) {
|
||||
xcb::map_window(self.conn.conn(), self.window_id);
|
||||
}
|
||||
|
||||
pub fn swap_buffers(&self) {
|
||||
self.conn
|
||||
.egl_display
|
||||
.swap_buffers(&self.gl.surface)
|
||||
.expect("failed to swap buffers");
|
||||
}
|
||||
|
||||
pub fn draw(&self) -> glium::Frame {
|
||||
glium::Frame::new(
|
||||
self.glium_context.clone(),
|
||||
self.gl.get_framebuffer_dimensions(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Window<'a> {
|
||||
@@ -199,6 +321,44 @@ impl<'a> Drop for Window<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> glium::backend::Facade for Window<'a> {
|
||||
fn get_context(&self) -> &Rc<glium::backend::Context> {
|
||||
&self.glium_context
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl glium::backend::Backend for GlState {
|
||||
fn swap_buffers(&self) -> result::Result<(), glium::SwapBuffersError> {
|
||||
self.display.swap_buffers(&self.surface).map_err(|_| {
|
||||
// We're guessing that this is the case as the other option
|
||||
// that glium recognizes is threading related and we're
|
||||
// single threaded.
|
||||
glium::SwapBuffersError::AlreadySwapped
|
||||
})
|
||||
}
|
||||
|
||||
unsafe fn get_proc_address(&self, symbol: &str) -> *const os::raw::c_void {
|
||||
mem::transmute(egli::egl::get_proc_address(symbol))
|
||||
}
|
||||
|
||||
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
|
||||
(
|
||||
self.surface.query_width().unwrap() as u32,
|
||||
self.surface.query_height().unwrap() as u32,
|
||||
)
|
||||
}
|
||||
|
||||
fn is_current(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
unsafe fn make_current(&self) {
|
||||
self.display
|
||||
.make_current(&self.surface, &self.surface, &self.egl_context)
|
||||
.expect("make_current failed");
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Context<'a> {
|
||||
gc_id: xcb::xproto::Gcontext,
|
||||
conn: &'a Connection,
|
||||
|
||||
+124
-1
@@ -1,12 +1,14 @@
|
||||
use config::TextStyle;
|
||||
use euclid;
|
||||
use failure::{self, Error};
|
||||
use font::{FontConfiguration, GlyphInfo, ftwrap};
|
||||
use glium::{self, Surface};
|
||||
use pty::MasterPty;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::mem;
|
||||
use std::ops::Range;
|
||||
use std::ops::{Deref, Range};
|
||||
use std::process::Child;
|
||||
use std::process::Command;
|
||||
use std::rc::Rc;
|
||||
@@ -20,6 +22,63 @@ use xcb_util;
|
||||
use xgfx::{self, BitmapImage, Connection, Drawable};
|
||||
use xkeysyms;
|
||||
|
||||
type Transform2D = euclid::Transform2D<f32>;
|
||||
type Transform3D = euclid::Transform3D<f32>;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct Point(euclid::Point2D<f32>);
|
||||
|
||||
impl Deref for Point {
|
||||
type Target = euclid::Point2D<f32>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl glium::vertex::Attribute for Point {
|
||||
#[inline]
|
||||
fn get_type() -> glium::vertex::AttributeType {
|
||||
glium::vertex::AttributeType::F32F32
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {
|
||||
fn new(x: f32, y: f32) -> Self {
|
||||
Self { 0: euclid::point2(x, y) }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct Vertex {
|
||||
position: Point,
|
||||
}
|
||||
|
||||
implement_vertex!(Vertex, position);
|
||||
|
||||
const VERTEX_SHADER: &str = r#"
|
||||
#version 300 es
|
||||
in vec2 position;
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 translation;
|
||||
|
||||
void main() {
|
||||
vec4 pos = vec4(position, 0.0, 1.0) * translation;
|
||||
gl_Position = projection * pos;
|
||||
}
|
||||
"#;
|
||||
|
||||
const FRAGMENT_SHADER: &str = r#"
|
||||
#version 300 es
|
||||
precision mediump float;
|
||||
uniform vec3 fg_color;
|
||||
out vec4 color;
|
||||
void main() {
|
||||
color = vec4(fg_color, 1.0);
|
||||
}
|
||||
"#;
|
||||
|
||||
|
||||
/// BufferImage is used to hold the bitmap of our rendered screen.
|
||||
/// If SHM is available we store it there and save the overhead of
|
||||
/// sending the bitmap to the server each time something is rendered.
|
||||
@@ -325,7 +384,69 @@ impl<'a> TerminalWindow<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn really_paint(&mut self, target: &mut glium::Frame) -> Result<(), Error> {
|
||||
let cell_pos = Point::new(2.0, 0.0);
|
||||
let cell_width = self.cell_width as f32;
|
||||
let cell_height = self.cell_height as f32;
|
||||
let top_left = Vertex { position: Point::new(0.0, 0.0) };
|
||||
let top_right = Vertex { position: Point::new(cell_width, 0.0) };
|
||||
let bot_left = Vertex { position: Point::new(0.0, cell_height) };
|
||||
let bot_right = Vertex { position: Point::new(cell_width, cell_height) };
|
||||
let shape = [top_left, top_right, bot_left, bot_right];
|
||||
|
||||
let width = self.width as f32;
|
||||
let height = self.height as f32;
|
||||
|
||||
// Translate cell coordinate from top-left origin in cell coords
|
||||
// to center origin pixel coords
|
||||
let xlate_model = Transform2D::create_translation(
|
||||
(cell_pos.x * cell_width) - width / 2.0,
|
||||
(cell_pos.y * cell_height) - height / 2.0,
|
||||
).to_3d();
|
||||
|
||||
// The projection corrects for the aspect ratio and flips the y-axis
|
||||
let projection = Transform3D::ortho(
|
||||
-width / 2.0,
|
||||
width / 2.0,
|
||||
height / 2.0,
|
||||
-height / 2.0,
|
||||
-1.0,
|
||||
1.0,
|
||||
);
|
||||
|
||||
let vertex_buffer = glium::VertexBuffer::new(&self.host.window, &shape)?;
|
||||
|
||||
let program =
|
||||
glium::Program::from_source(&self.host.window, VERTEX_SHADER, FRAGMENT_SHADER, None)?;
|
||||
|
||||
target.clear_color(0.1, 0.1, 0.3, 1.0);
|
||||
target.draw(
|
||||
&vertex_buffer,
|
||||
glium::index::NoIndices(
|
||||
glium::index::PrimitiveType::TriangleStrip,
|
||||
),
|
||||
&program,
|
||||
&uniform! {
|
||||
fg_color: (0.5, 0.0, 0.8f32),
|
||||
projection: projection.to_column_arrays(),
|
||||
translation: xlate_model.to_column_arrays(),
|
||||
},
|
||||
&Default::default(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn paint_gl(&mut self) -> Result<(), Error> {
|
||||
let mut target = self.host.window.draw();
|
||||
let res = self.really_paint(&mut target);
|
||||
target.finish().unwrap();
|
||||
res
|
||||
}
|
||||
|
||||
pub fn expose(&mut self, x: u16, y: u16, width: u16, height: u16) -> Result<(), Error> {
|
||||
return self.paint_gl();
|
||||
|
||||
debug!("expose {},{}, {},{}", x, y, width, height);
|
||||
|
||||
match &*self.buffer_image.borrow() {
|
||||
@@ -856,6 +977,8 @@ impl<'a> TerminalWindow<'a> {
|
||||
}
|
||||
|
||||
pub fn paint(&mut self) -> Result<(), Error> {
|
||||
return Ok(());
|
||||
|
||||
let cursor = self.terminal.cursor_pos();
|
||||
{
|
||||
let dirty_lines = self.terminal.get_dirty_lines();
|
||||
|
||||
@@ -194,7 +194,10 @@ impl TerminalState {
|
||||
) -> Option<Rc<Hyperlink>> {
|
||||
let screen = self.screen();
|
||||
let idx = screen.scrollback_or_visible_row(y);
|
||||
let line = &screen.lines[idx];
|
||||
let line = match &screen.lines.get(idx) {
|
||||
&Some(line) => line,
|
||||
&None => return None,
|
||||
};
|
||||
match line.cells.get(x) {
|
||||
Some(cell) => cell.attrs.hyperlink.as_ref().cloned(),
|
||||
None => None,
|
||||
|
||||
Reference in new issue
Block a user