cogl/auto/
texture2_d.rs

1use crate::{Bitmap, Context, Object, PixelFormat, Texture};
2
3use glib::translate::*;
4use std::{fmt, ptr};
5
6glib_wrapper! {
7    pub struct Texture2D(Object<ffi::CoglTexture2D, Texture2DClass>) @extends Object, @implements Texture;
8
9    match fn {
10        get_type => || ffi::cogl_texture_2d_get_gtype(),
11    }
12}
13
14impl Texture2D {
15    /// Wraps an existing GL_TEXTURE_2D texture object as a `Texture2D`.
16    /// This can be used for integrating Cogl with software using OpenGL
17    /// directly.
18    ///
19    /// The texture is still configurable until it has been allocated so
20    /// for example you can declare whether the texture is premultiplied
21    /// with `Texture::set_premultiplied`.
22    ///
23    /// `<note>`The results are undefined for passing an invalid `gl_handle`
24    /// or if `width` or `height` don't have the correct texture
25    /// geometry.`</note>`
26    ///
27    /// ## `ctx`
28    /// A `Context`
29    /// ## `gl_handle`
30    /// A GL handle for a GL_TEXTURE_2D texture object
31    /// ## `width`
32    /// Width of the foreign GL texture
33    /// ## `height`
34    /// Height of the foreign GL texture
35    /// ## `format`
36    /// The format of the texture
37    ///
38    /// # Returns
39    ///
40    /// A newly allocated `Texture2D`
41    pub fn gl_new_from_foreign(
42        ctx: &Context,
43        gl_handle: u32,
44        width: i32,
45        height: i32,
46        format: PixelFormat,
47    ) -> Texture2D {
48        unsafe {
49            from_glib_full(ffi::cogl_texture_2d_gl_new_from_foreign(
50                ctx.to_glib_none().0,
51                gl_handle,
52                width,
53                height,
54                format.to_glib(),
55            ))
56        }
57    }
58
59    pub fn from_bitmap(bitmap: &Bitmap) -> Texture2D {
60        unsafe {
61            from_glib_full(ffi::cogl_texture_2d_new_from_bitmap(
62                bitmap.to_glib_none().0,
63            ))
64        }
65    }
66
67    pub fn from_data(
68        ctx: &Context,
69        width: i32,
70        height: i32,
71        format: PixelFormat,
72        rowstride: i32,
73        data: &[u8],
74    ) -> Result<Texture2D, glib::Error> {
75        unsafe {
76            let mut error = ptr::null_mut();
77            let ret = ffi::cogl_texture_2d_new_from_data(
78                ctx.to_glib_none().0,
79                width,
80                height,
81                format.to_glib(),
82                rowstride,
83                data.as_ptr(),
84                &mut error,
85            );
86            if error.is_null() {
87                Ok(from_glib_full(ret))
88            } else {
89                Err(from_glib_full(error))
90            }
91        }
92    }
93
94    pub fn from_file(ctx: &Context, filename: &str) -> Result<Texture2D, glib::Error> {
95        unsafe {
96            let mut error = ptr::null_mut();
97            let ret = ffi::cogl_texture_2d_new_from_file(
98                ctx.to_glib_none().0,
99                filename.to_glib_none().0,
100                &mut error,
101            );
102            if error.is_null() {
103                Ok(from_glib_full(ret))
104            } else {
105                Err(from_glib_full(error))
106            }
107        }
108    }
109
110    pub fn with_size(ctx: &Context, width: i32, height: i32) -> Texture2D {
111        unsafe {
112            from_glib_full(ffi::cogl_texture_2d_new_with_size(
113                ctx.to_glib_none().0,
114                width,
115                height,
116            ))
117        }
118    }
119}
120
121impl fmt::Display for Texture2D {
122    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123        write!(f, "Texture2D")
124    }
125}