summaryrefslogtreecommitdiff
path: root/crates/renderer/src/texture_server.rs
blob: ce14262881312e0196b15e5fb690829ac507945d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use teleia::*;

use std::io::Write;
use std::os::fd::FromRawFd;
use std::os::unix::net::{AncillaryData, SocketAncillary};
use std::sync::mpsc::{Receiver, channel};
use std::{os::unix::net::UnixDatagram, thread::{spawn, JoinHandle}};

use glow::HasContext;
use crate::ffi::egl;
use crate::ffi::glfw;

const KEY: usize = 42;
const SOCK_PATH: &'static str = "/tmp/newton.sock";


pub struct Server {
    pub tex: texture::Texture,
    thread: JoinHandle<()>,
    receiver: Receiver<i32>,
}
impl Server {
    pub fn new(ctx: &context::Context) -> Self {
        let _ = std::fs::remove_file(SOCK_PATH);
        let sock = UnixDatagram::bind(SOCK_PATH).expect("failed to bind texture server socket");

        let (sender, receiver) = channel::<i32>();
        let thread = spawn(move || {
            let mut ancillary_buf = [0; 128];
            let mut ancillary = SocketAncillary::new(&mut ancillary_buf);
            loop {
                match sock.recv_vectored_with_ancillary(&mut [], &mut ancillary) {
                    Ok(_) => {
                        for ar in ancillary.messages() {
                            match ar {
                                Ok(AncillaryData::ScmRights(mut sr)) => {
                                    if let Some(fd) = sr.next() {
                                        if let Err(e) = sender.send(fd) {
                                            log::warn!("error on channel: {:?}", e);
                                        }
                                    }
                                },
                                Ok(_) => log::info!("message is not SCM_RIGHTS"),
                                Err(e) => log::warn!("failed to read ancillary message: {:?}", e),
                            }
                        }
                    },
                    Err(e) => log::warn!("texture server failed to receive: {}", e),
                }
            }
        });
        Self {
            tex: texture::Texture::new_empty(ctx),
            thread,
            receiver,
        }
    }
    pub fn update(&self, ctx: &context::Context) {
        if let Ok(fd) = self.receiver.try_recv() {
            log::info!("received file descriptor: {}", fd);
            unsafe {
                let window = ctx.window.borrow_mut().0.ptr;
                let d = glfw::glfwGetEGLDisplay();
                let c = glfw::glfwGetEGLContext(window);
                let img = egl::eglCreateImage(d, c, egl::EGL_LINUX_DMA_BUF_EXT, std::ptr::null_mut(), [
                    egl::EGL_WIDTH as _, 16,
                    egl::EGL_HEIGHT as _, 16,
                    egl::EGL_LINUX_DRM_FOURCC_EXT as _, 875708993,
                    egl::EGL_DMA_BUF_PLANE0_FD_EXT as _, fd as _,
                    egl::EGL_DMA_BUF_PLANE0_OFFSET_EXT as _, 0,
                    egl::EGL_DMA_BUF_PLANE0_PITCH_EXT as _, 128,
                    egl::EGL_NONE as _,
                ].as_ptr());
                self.tex.bind(ctx);
                egl::glEGLImageTargetTexture2DOES(glow::TEXTURE_2D, img);
                log::info!("set up texture");
            }
        }
    }
}