egli/
window_surface.rs

1// Copyright 2016 The EGLI Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use egl;
9use error::Result;
10
11/// `[EGL 1.0]` [RAII](https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) wrapper for
12/// EGLSurface.
13///
14/// When dropped, frees up the surface with `eglDestroySurface` call.
15///
16/// Note that the surface would not be immediately freed if it is current to any thread.
17/// In such a case, the surface will be freed when it is no longer used.
18pub struct Surface {
19    terminated: bool,
20    display_handle: egl::EGLDisplay,
21    handle: egl::EGLSurface,
22}
23
24impl Drop for Surface {
25    fn drop(&mut self) {
26        if !self.terminated {
27            let _ = egl::destroy_surface(self.display_handle, self.handle);
28        }
29    }
30}
31
32impl Into<egl::EGLSurface> for Surface {
33    fn into(self) -> egl::EGLSurface {
34        self.forget()
35    }
36}
37
38impl Surface {
39    /// Create a `Surface` from an existing EGL display and surface handles.
40    pub fn from_handle(display_handle: egl::EGLDisplay,
41                       surface_handle: egl::EGLSurface)
42                       -> Surface {
43        Surface {
44            terminated: false,
45            display_handle: display_handle,
46            handle: surface_handle,
47        }
48    }
49
50    /// Get raw handle.
51    pub fn handle(&self) -> egl::EGLSurface {
52        self.handle
53    }
54
55    /// [EGL 1.0] Returns the width of the surface in pixels.
56    ///
57    /// Result of `eglQuerySurface` with `EGL_WIDTH` parameter.
58    pub fn query_width(&self) -> Result<i32> {
59        let mut value: egl::EGLint = 0;
60        egl::query_surface(self.display_handle, self.handle, egl::EGL_WIDTH, &mut value)?;
61        Ok(value as i32)
62    }
63
64    /// [EGL 1.0] Returns the height of the surface in pixels.
65    ///
66    /// Result of `eglQuerySurface` with `EGL_HEIGHT` parameter.
67    pub fn query_height(&self) -> Result<i32> {
68        let mut value: egl::EGLint = 0;
69        egl::query_surface(self.display_handle,
70                                self.handle,
71                                egl::EGL_HEIGHT,
72                                &mut value)?;
73        Ok(value as i32)
74    }
75
76    /// Drops `Surface` without cleaning up any resources.
77    ///
78    /// Returns `EGLSurface` handle.
79    ///
80    /// Alias for `Into<egl::EGLSurface>`.
81    pub fn forget(mut self) -> egl::EGLSurface {
82        self.terminated = true;
83        self.handle
84    }
85}