Setting up a surface with OpenGL rendering capabilities in a Weston Wayland environment generally involves creating a Wayland surface, wrapping it with wl_egl_window, and then using EGL to manage both OpenGL context and surfaces. Below is a step-by-step guide showing how to properly initialize these components together. This guide assumes that you have the necessary buffering for a complete OpenGL application and focuses on the integration with Weston as a Wayland compositor.
Step 1: Install and Include Necessary Libraries
Ensure you have the following libraries installed:
- Wayland development libraries (
libwayland-client)
- Wayland-EGL bridge (
libwayland-egl)
- EGL and OpenGL ES libraries (e.g.,
mesa packages for EGL and GLES)
Include the necessary headers in your C or C++ source file:
#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
Step 2: Initialize Wayland Client
Connect to the Wayland display and create a wl_display and wl_registry object, then bind the compositor and shell:
struct wl_display* display = wl_display_connect(NULL);
assert(display);
struct wl_registry* registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, NULL);
wl_display_roundtrip(display);
// Assume `registry_listener` setups `wl_compositor` and `wl_shell` globally
In the listener, handle global objects, looking for at least "wl_compositor" and potentially "xdg_wm_base" or whichever shell protocol you target.
Step 3: Create Wayland Surface and EGL Window
After setting up the compositor, create a Wayland surface, and then wrap it using wl_egl_window_create:
struct wl_surface* surface = wl_compositor_create_surface(compositor);
struct wl_egl_window* egl_window = wl_egl_window_create(surface, width, height);
Step 4: Set Up EGL
Initialize EGL, select an appropriate configuration, and create an EGL context:
EGLDisplay egl_display = eglGetDisplay((EGLNativeDisplayType)display);
eglInitialize(egl_display, NULL, NULL);
EGLConfig config;
EGLint num_config;
static const EGLint config_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
eglChooseConfig(egl_display, config_attribs, &config, 1, &num_config);
EGLContext egl_context = eglCreateContext(egl_display, config, EGL_NO_CONTEXT, NULL);
EGLSurface egl_surface = eglCreateWindowSurface(egl_display, config, egl_window, NULL);
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
Step 5: Render Using OpenGL ES
Setup your OpenGL environment (e.g., viewport, shaders) and start your rendering loop:
glViewport(0, 0, width, height);
// Your rendering loop
while (!done) {
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw something
eglSwapBuffers(egl_display, egl_surface);
}
Step 6: Cleanup
Properly clean up resources when done:
eglDestroySurface(egl_display, egl_surface);
wl_egl_window_destroy(egl_window);
wl_surface_destroy(surface);
eglDestroyContext(egl_display, egl_context);
eglTerminate(egl_display);
wl_display_disconnect(display);
Notes
- Be sure to register and handle any Wayland protocol listener functions needed for interacting with the Wayland server.
- Note that synchronization and error handling are quite important in real-world applications but are simplified here for clarity.
- Depending on your configuration, you may need to handle other setups like the shell surface interface, etc.
This setup allows you to render using OpenGL in a Weston compositor running under Wayland, ensuring proper integration and efficient handling of graphics resources.