README.md
1# Rust bindings for EGL
2
3<table><tr>
4 <td><a href="https://docs.rs/khronos-egl">Documentation</a></td>
5 <td><a href="https://crates.io/crates/khronos-egl">Crate informations</a></td>
6 <td><a href="https://github.com/timothee-haudebourg/khronos-egl">Repository</a></td>
7</tr></table>
8
9This crate provides a binding for the Khronos EGL 1.5 API.
10It was originally a fork of the [egl](https://crates.io/crates/egl) crate,
11which is left unmaintained.
12
13## Usage
14
15You can access the EGL API using an [`Instance`](https://docs.rs/khronos-egl/latest/khronos-egl/struct.Instance.html)
16object defined by either statically linking with `libEGL.so.1` at compile time,
17or dynamically loading the EGL library at runtime.
18
19### Static linking
20
21You must enable static linking using the `static` feature in your `Cargo.toml`:
22```toml
23khronos-egl = { version = ..., features = ["static"] }
24```
25
26This will add a dependency to the [`pkg-config`](https://crates.io/crates/pkg-config) crate,
27necessary to find the EGL library at compile time.
28
29If you wish to disable linking EGL in this crate, and provide linking in
30your crate instead, enable the `no-pkg-config` feature.
31```toml
32khronos-egl = {version = ..., features = ["static", "no-pkg-config"]}
33```
34
35Here is a simple example showing how to use this library to create an EGL context when static linking is enabled.
36
37```rust
38extern crate khronos_egl as egl;
39
40fn main() -> Result<(), egl::Error> {
41 // Create an EGL API instance.
42 // The `egl::Static` API implementation is only available when the `static` feature is enabled.
43 let egl = egl::Instance::new(egl::Static);
44
45 let wayland_display = wayland_client::Display::connect_to_env().expect("unable to connect to the wayland server");
46 let display = egl.get_display(wayland_display.get_display_ptr() as *mut std::ffi::c_void).unwrap();
47 egl.initialize(display)?;
48
49 let attributes = [
50 egl::RED_SIZE, 8,
51 egl::GREEN_SIZE, 8,
52 egl::BLUE_SIZE, 8,
53 egl::NONE
54 ];
55
56 let config = egl.choose_first_config(display, &attributes)?.expect("unable to find an appropriate ELG configuration");
57
58 let context_attributes = [
59 egl::CONTEXT_MAJOR_VERSION, 4,
60 egl::CONTEXT_MINOR_VERSION, 0,
61 egl::CONTEXT_OPENGL_PROFILE_MASK, egl::CONTEXT_OPENGL_CORE_PROFILE_BIT,
62 egl::NONE
63 ];
64
65 egl.create_context(display, config, None, &context_attributes);
66
67 Ok(())
68}
69```
70
71The creation of a `Display` instance is not detailed here since it depends on your display server.
72It is created using the `get_display` function with a pointer to the display server connection handle.
73For instance, if you are using the [wayland-client](https://crates.io/crates/wayland-client) crate,
74you can get this pointer using the `Display::get_display_ptr` method.
75
76#### Static API Instance
77
78It may be bothering in some applications to pass the `Instance` to every fonction that needs to call the EGL API.
79One workaround would be to define a static `Instance`,
80which should be possible to define at compile time using static linking.
81However this is not yet supported by the stable `rustc` compiler.
82With the nightly compiler,
83you can combine the `nightly` and `static` features so that this crate
84can provide a static `Instance`, called `API` that can then be accessed everywhere.
85
86```rust
87use egl::API as egl;
88```
89
90### Dynamic Linking
91
92Dynamic linking allows your application to accept multiple versions of EGL and be more flexible.
93You must enable dynamic linking using the `dynamic` feature in your `Cargo.toml`:
94```toml
95khronos-egl = { version = ..., features = ["dynamic"] }
96```
97
98This will add a dependency to the [`libloading`](https://crates.io/crates/libloading) crate,
99necessary to find the EGL library at runtime.
100You can then load the EGL API into a `Instance<Dynamic<libloading::Library>>` as follows:
101
102```rust
103let lib = libloading::Library::new("libEGL.so.1").expect("unable to find libEGL.so.1");
104let egl = unsafe { egl::DynamicInstance::<egl::EGL1_4>::load_required_from(lib).expect("unable to load libEGL.so.1") };
105```
106
107Here, `egl::EGL1_4` is used to specify what is the minimum required version of EGL that must be provided by `libEGL.so.1`.
108This will return a `DynamicInstance<egl::EGL1_4>`, however in that case where `libEGL.so.1` provides a more recent version of EGL,
109you can still upcast ths instance to provide version specific features:
110```rust
111match egl.upcast::<egl::EGL1_5>() {
112 Some(egl1_5) => {
113 // do something with EGL 1.5
114 }
115 None => {
116 // do something with EGL 1.4 instead.
117 }
118};
119```
120
121### NixOS
122
123A `shell.nix` file is present for nix users to build the crate easily.
124Just enter a new nix shell using the given configuration file,
125and `cargo build` should work.
126If you want to run the tests and examples you will need to use `shell-wayland.nix` instead
127that will also load wayland since most of them depend on it.
128
129## Testing
130
131Most test and examples most be compiled with the `static` feature.
132
133## Troubleshooting
134
135### Static Linking with OpenGL ES
136
137When using OpenGL ES with `khronos-egl` with the `static` feature,
138it is necessary to place a dummy extern at the top of your application which links libEGL first, then GLESv1/2.
139This is because libEGL provides symbols required by GLESv1/2.
140Here's how to work around this:
141
142```rust
143#[link(name = "EGL")]
144#[link(name = "GLESv2")]
145extern {}
146```
147
148## License
149
150Licensed under either of
151
152 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
153 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
154
155at your option.
156
157If the original `egl` crate was licensed only under the Apache 2.0 license,
158I believe I have made enough breaking changes so that no relevant code from the
159original code remains and the rest can be relicensed.
160
161### Contribution
162
163Unless you explicitly state otherwise, any contribution intentionally submitted
164for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
165additional terms or conditions.
166