diff --git a/examples/drm.rs b/examples/drm.rs index 3c1d4eb..bb2be21 100644 --- a/examples/drm.rs +++ b/examples/drm.rs @@ -25,6 +25,7 @@ use smithay::compositor::{compositor_init, CompositorToken, SubsurfaceRole, Trav use smithay::compositor::roles::Role; use smithay::shell::{shell_init, ShellState}; use smithay::shm::init_shm_global; +use std::borrow::Borrow; use std::fs::OpenOptions; use std::io::Error as IoError; use std::time::Duration; @@ -147,7 +148,7 @@ impl DrmHandler> for DrmHandlerImpl { frame.clear_color(0.8, 0.8, 0.9, 1.0); // redraw the frame, in a simple but inneficient way { - let screen_dimensions = drawer.get_framebuffer_dimensions(); + let screen_dimensions = (drawer.borrow() as &DrmBackend).get_framebuffer_dimensions(); for toplevel_surface in state.get(&self.shell_state_token).toplevel_surfaces() { if let Some(wl_surface) = toplevel_surface.get_surface() { // this surface is a root of a subsurface tree that needs to be drawn diff --git a/examples/helpers/glium.rs b/examples/helpers/glium.rs index a70f91f..5913827 100644 --- a/examples/helpers/glium.rs +++ b/examples/helpers/glium.rs @@ -1,9 +1,10 @@ use glium; -use glium::{Frame, Surface}; +use glium::Surface; use glium::index::PrimitiveType; use smithay::backend::graphics::egl::EGLGraphicsBackend; use smithay::backend::graphics::glium::GliumGraphicsBackend; use std::ops::Deref; +use std::borrow::Borrow; #[derive(Copy, Clone)] struct Vertex { @@ -21,10 +22,16 @@ pub struct GliumDrawer { } impl Deref for GliumDrawer { - type Target = F; + type Target = GliumGraphicsBackend; - fn deref(&self) -> &F { - &*self.display + fn deref(&self) -> &GliumGraphicsBackend { + &self.display + } +} + +impl Borrow for GliumDrawer { + fn borrow(&self) -> &F { + self.display.borrow() } } @@ -135,9 +142,4 @@ impl GliumDrawer { ) .unwrap(); } - - #[inline] - pub fn draw(&self) -> Frame { - self.display.draw() - } } diff --git a/src/backend/drm/mod.rs b/src/backend/drm/mod.rs index fea1d12..10a16ff 100644 --- a/src/backend/drm/mod.rs +++ b/src/backend/drm/mod.rs @@ -42,19 +42,19 @@ //! ```rust,no_run //! extern crate drm; //! extern crate smithay; -//! # extern crate wayland_server; +//! extern crate wayland_server; //! //! use drm::control::{Device as ControlDevice, ResourceInfo}; //! use drm::control::connector::{Info as ConnectorInfo, State as ConnectorState}; //! use drm::control::encoder::{Info as EncoderInfo}; -//! # use std::io::Error as IoError; //! use std::fs::OpenOptions; -//! # use std::time::Duration; -//! use smithay::backend::drm::DrmDevice; -//! # use smithay::backend::drm::{DrmHandler, Id}; -//! # use wayland_server::EventLoopHandle; +//! use smithay::backend::drm::{DrmDevice, DrmBackend}; +//! use wayland_server::StateToken; //! //! # fn main() { +//! +//! let (_display, mut event_loop) = wayland_server::create_display(); +//! //! // Open the drm device //! let mut options = OpenOptions::new(); //! options.read(true); @@ -89,19 +89,12 @@ //! let mode = connector_info.modes()[0]; //! //! // Create the backend -//! let backend = device.create_backend( -//! crtc, -//! mode, -//! vec![connector_info.handle()] -//! ).unwrap(); -//! # struct MyDrmHandler; -//! # -//! # impl DrmHandler for MyDrmHandler { -//! # fn ready(&mut self, _: &mut EventLoopHandle, id: Id, _frame: u32, _duration: Duration) {} -//! # fn error(&mut self, _: &mut EventLoopHandle, error: IoError) {} -//! # } -//! # -//! # device.set_handler(MyDrmHandler); +//! let backend: StateToken = device.create_backend( +//! &mut event_loop, +//! crtc, +//! mode, +//! vec![connector_info.handle()] +//! ).unwrap(); //! # } //! ``` //! @@ -126,13 +119,11 @@ //! # use drm::control::{Device as ControlDevice, ResourceInfo}; //! # use drm::control::connector::{Info as ConnectorInfo, State as ConnectorState}; //! use std::io::Error as IoError; -//! use std::os::unix::io::AsRawFd; //! # use std::fs::OpenOptions; //! # use std::time::Duration; -//! use smithay::backend::drm::{DrmDevice, DrmBackend, DrmHandler, Id}; +//! use smithay::backend::drm::{DrmDevice, DrmBackend, DrmHandler, drm_device_bind}; //! use smithay::backend::graphics::egl::EGLGraphicsBackend; -//! use wayland_server::sources::READ; -//! # use wayland_server::EventLoopHandle; +//! use wayland_server::{EventLoopHandle, StateToken}; //! # //! # fn main() { //! # @@ -153,33 +144,39 @@ //! # .unwrap(); //! # let crtc = res_handles.crtcs()[0]; //! # let mode = connector_info.modes()[0]; -//! # let backend = device.create_backend( -//! # crtc, -//! # mode, -//! # vec![connector_info.handle()] -//! # ).unwrap(); +//! # let backend: StateToken = device.create_backend( +//! # &mut event_loop, +//! # crtc, +//! # mode, +//! # vec![connector_info.handle()] +//! # ).unwrap(); //! -//! struct MyDrmHandler(DrmBackend); +//! struct MyDrmHandler; //! -//! impl DrmHandler for MyDrmHandler { -//! fn ready(&mut self, _: &mut EventLoopHandle, id: Id, _frame: u32, _duration: Duration) { -//! if self.0.is(id) { // check id in case you got multiple backends -//! // ... render surfaces ... -//! self.0.swap_buffers().unwrap(); // trigger the swap -//! } +//! impl DrmHandler for MyDrmHandler { +//! fn ready(&mut self, +//! evlh: &mut EventLoopHandle, +//! _device: &mut DrmDevice, +//! backend: &StateToken, +//! _frame: u32, +//! _duration: Duration) +//! { +//! // render surfaces and swap again +//! evlh.state().get(backend).swap_buffers().unwrap(); //! } -//! fn error(&mut self, _: &mut EventLoopHandle, error: IoError) { +//! fn error(&mut self, +//! _: &mut EventLoopHandle, +//! device: &mut DrmDevice, +//! error: IoError) +//! { //! panic!("DrmDevice errored: {}", error); //! } //! } //! //! // render something (like clear_color) -//! backend.swap_buffers().unwrap(); +//! event_loop.state().get(&backend).swap_buffers().unwrap(); //! -//! device.set_handler(MyDrmHandler(backend)); -//! let fd = device.as_raw_fd(); -//! let drm_device_id = event_loop.add_handler(device); -//! let _drm_event_source = event_loop.add_fd_event_source::>(fd, drm_device_id, READ); +//! let _source = drm_device_bind(&mut event_loop, device, MyDrmHandler).unwrap(); //! //! event_loop.run().unwrap(); //! # } @@ -191,11 +188,11 @@ use drm::control::{connector, crtc, encoder, Mode, ResourceInfo}; use drm::control::Device as ControlDevice; use gbm::Device as GbmDevice; use nix; +use std::borrow::Borrow; use std::collections::HashMap; use std::fs::File; use std::io::{Error as IoError, Result as IoResult}; use std::marker::PhantomData; -use std::ops::Deref; use std::os::unix::io::{AsRawFd, RawFd}; use std::rc::Rc; use std::time::Duration; @@ -256,13 +253,13 @@ rental! { use self::devices::{Context, Devices}; /// Representation of an open drm device node to create rendering backends -pub struct DrmDevice + 'static> { +pub struct DrmDevice + 'static> { context: Rc, backends: HashMap>, logger: ::slog::Logger, } -impl + Deref + 'static> DrmDevice { +impl + Borrow + 'static> DrmDevice { /// Create a new `DrmDevice` from a raw file descriptor /// /// Returns an error of opening the device failed or context creation was not @@ -440,18 +437,18 @@ impl + Deref + 'static> DrmDevice { } // for users convinience and FdEventSource registering -impl + 'static> AsRawFd for DrmDevice { +impl + 'static> AsRawFd for DrmDevice { fn as_raw_fd(&self) -> RawFd { self.context.head().head().as_raw_fd() } } -impl + 'static> BasicDevice for DrmDevice {} -impl + 'static> ControlDevice for DrmDevice {} +impl + 'static> BasicDevice for DrmDevice {} +impl + 'static> ControlDevice for DrmDevice {} /// Handler for drm node events /// /// See module-level documentation for its use -pub trait DrmHandler + 'static> { +pub trait DrmHandler + 'static> { /// A `DrmBackend` has finished swapping buffers and new frame can now /// (and should be immediately) be rendered. /// @@ -472,7 +469,7 @@ pub trait DrmHandler + 'static> { pub fn drm_device_bind(evlh: &mut EventLoopHandle, device: DrmDevice, handler: H) -> IoResult, H)>> where - B: Deref + 'static, + B: Borrow + 'static, H: DrmHandler + 'static, { evlh.add_fd_event_source( @@ -485,7 +482,7 @@ where fn fd_event_source_implementation() -> FdEventSourceImpl<(DrmDevice, H)> where - B: Deref + 'static, + B: Borrow + 'static, H: DrmHandler + 'static, { FdEventSourceImpl { @@ -497,7 +494,7 @@ where struct PageFlipHandler< 'a, 'b, - B: Deref + 'static, + B: Borrow + 'static, H: DrmHandler + 'static, > { handler: &'a mut H, @@ -507,7 +504,7 @@ where impl<'a, 'b, B, H> crtc::PageFlipHandler> for PageFlipHandler<'a, 'b, B, H> where - B: Deref + 'static, + B: Borrow + 'static, H: DrmHandler + 'static, { fn handle_event(&mut self, device: &mut DrmDevice, frame: u32, duration: Duration, @@ -516,7 +513,7 @@ where let token = device.backends.get(&crtc_id).cloned(); if let Some(token) = token { // we can now unlock the buffer - (**self.evlh.state().get(&token)).unlock_buffer(); + self.evlh.state().get(&token).borrow().unlock_buffer(); trace!(device.logger, "Handling event for backend {:?}", crtc_id); // and then call the user to render the next frame self.handler diff --git a/src/backend/graphics/glium.rs b/src/backend/graphics/glium.rs index c7ea06c..42ffedf 100644 --- a/src/backend/graphics/glium.rs +++ b/src/backend/graphics/glium.rs @@ -5,7 +5,7 @@ use glium::Frame; use glium::SwapBuffersError as GliumSwapBuffersError; use glium::backend::{Backend, Context, Facade}; use glium::debug::DebugCallbackBehavior; -use std::ops::Deref; +use std::borrow::Borrow; use std::os::raw::c_void; use std::rc::Rc; @@ -54,10 +54,8 @@ impl GliumGraphicsBackend { } } -impl Deref for GliumGraphicsBackend { - type Target = T; - - fn deref(&self) -> &T { +impl Borrow for GliumGraphicsBackend { + fn borrow(&self) -> &T { &self.backend.0 } }