參考資訊:
https://jan.newmarch.name/Wayland/index.html
https://wayland.freedesktop.org/docs/html/apa.html
https://bugaevc.gitbooks.io/writing-wayland-clients/content/
https://gist.github.com/Miouyouyou/ca15af1c7f2696f66b0e013058f110b4
main.c
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <wayland-server.h>
#include <wayland-client.h>
#include <wayland-client-protocol.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>
#include <GLES2/gl2.h>
#define LCD_W 540
#define LCD_H 960
struct wl_shell *wl_shell = NULL;
struct wl_region *wl_region = NULL;
struct wl_display *wl_display = NULL;
struct wl_surface *wl_surface = NULL;
struct wl_registry *wl_registry = NULL;
struct wl_egl_window *wl_egl_window = NULL;
struct wl_compositor *wl_compositor = NULL;
struct wl_shell_surface *wl_shell_surface = NULL;
const char *vShaderSrc =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
const char *fShaderSrc =
"#ifdef GL_ES \n"
"precision mediump float; \n"
"#endif \n"
"varying vec2 v_texCoord; \n"
"uniform float angle; \n"
"uniform float aspect; \n"
"uniform sampler2D s_texture; \n"
"const vec2 HALF = vec2(0.5); \n"
"void main() \n"
"{ \n"
" float aSin = sin(angle); \n"
" float aCos = cos(angle); \n"
" vec2 tc = v_texCoord; \n"
" mat2 rotMat = mat2(aCos, -aSin, aSin, aCos); \n"
" mat2 scaleMat = mat2(aspect, 0.0, 0.0, 1.0); \n"
" mat2 scaleMatInv = mat2(1.0 / aspect, 0.0, 0.0, 1.0); \n"
" tc -= HALF.xy; \n"
" tc = scaleMatInv * rotMat * scaleMat * tc; \n"
" tc += HALF.xy; \n"
" vec3 tex = texture2D(s_texture, tc).rgb; \n"
" gl_FragColor = vec4(tex, 1.0); \n"
"} \n";
static void global_registry_handler(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version)
{
if (strcmp(interface, "wl_compositor") == 0) {
wl_compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
}
else if (strcmp(interface, "wl_shell") == 0) {
wl_shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
}
}
static void global_registry_remover(void *data, struct wl_registry *registry, uint32_t id)
{
}
const struct wl_registry_listener listener = {
global_registry_handler,
global_registry_remover
};
int main(int argc, char **argv)
{
wl_display = wl_display_connect(NULL);
wl_registry = wl_display_get_registry(wl_display);
wl_registry_add_listener(wl_registry, &listener, NULL);
wl_display_dispatch(wl_display);
wl_display_roundtrip(wl_display);
wl_surface = wl_compositor_create_surface(wl_compositor);
wl_shell_surface = wl_shell_get_shell_surface(wl_shell, wl_surface);
wl_shell_surface_set_toplevel(wl_shell_surface);
wl_region = wl_compositor_create_region(wl_compositor);
wl_region_add(wl_region, 0, 0, LCD_W, LCD_H);
wl_surface_set_opaque_region(wl_surface, wl_region);
wl_egl_window = wl_egl_window_create(wl_surface, LCD_W, LCD_H);
EGLContext egl_context;
EGLSurface egl_surface;
EGLint egl_numConfigs;
EGLint egl_majorVersion;
EGLint egl_minorVersion;
EGLConfig egl_config;
EGLint egl_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_NONE
};
EGLint egl_context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE, EGL_NONE
};
EGLDisplay egl_display = eglGetDisplay(wl_display);
eglInitialize(egl_display, &egl_majorVersion, &egl_minorVersion);
eglGetConfigs(egl_display, NULL, 0, &egl_numConfigs);
eglChooseConfig(egl_display, egl_attribs, &egl_config, 1, &egl_numConfigs);
egl_surface = eglCreateWindowSurface(egl_display, egl_config, wl_egl_window, NULL);
egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, egl_context_attribs);
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
wl_display_dispatch_pending(wl_display);
glViewport(0, 0, LCD_W, LCD_H);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
GLuint vShader = 0;
GLuint fShader = 0;
GLuint pObject = 0;
vShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vShader, 1, &vShaderSrc, NULL);
glCompileShader(vShader);
fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fShader, 1, &fShaderSrc, NULL);
glCompileShader(fShader);
pObject = glCreateProgram();
glAttachShader(pObject, vShader);
glAttachShader(pObject, fShader);
glLinkProgram(pObject);
glUseProgram(pObject);
int w = 320;
int h = 240;
GLint positionLoc = glGetAttribLocation(pObject, "a_position");
GLint texCoordLoc = glGetAttribLocation(pObject, "a_texCoord");
GLint samplerLoc = glGetUniformLocation(pObject, "s_texture");
glUniform1f(glGetUniformLocation(pObject, "angle"), 90.0 * (3.1415 * 2.0) / 360.0);
glUniform1f(glGetUniformLocation(pObject, "aspect"), (float)w / h);
GLuint textureId = 0;
GLubyte pixels[320 * 240 * 3] = {0};
int x = 0, y = 0;
for (y = 0; y < 240; y++) {
for (x = 0; x < 320; x++) {
switch (y / 80) {
case 0:
pixels[(y * 320 * 3) + (x * 3) + 0] = 0xff;
pixels[(y * 320 * 3) + (x * 3) + 1] = 0x00;
pixels[(y * 320 * 3) + (x * 3) + 2] = 0x00;
break;
case 1:
pixels[(y * 320 * 3) + (x * 3) + 0] = 0x00;
pixels[(y * 320 * 3) + (x * 3) + 1] = 0xff;
pixels[(y * 320 * 3) + (x * 3) + 2] = 0x00;
break;
case 2:
pixels[(y * 320 * 3) + (x * 3) + 0] = 0x00;
pixels[(y * 320 * 3) + (x * 3) + 1] = 0x00;
pixels[(y * 320 * 3) + (x * 3) + 2] = 0xff;
break;
}
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLfloat vVertices[] = {
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 0.0f
};
GLushort indices[] = {0, 1, 2, 0, 2, 3};
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vVertices);
glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3]);
glEnableVertexAttribArray(positionLoc);
glEnableVertexAttribArray(texCoordLoc);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(samplerLoc, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
eglSwapBuffers(egl_display, egl_surface);
usleep(3000000);
eglDestroySurface(egl_display, egl_surface);
eglDestroyContext(egl_display, egl_context);
wl_egl_window_destroy(wl_egl_window);
eglTerminate(egl_display);
wl_region_destroy(wl_region);
wl_shell_surface_destroy(wl_shell_surface);
wl_shell_destroy(wl_shell);
wl_surface_destroy(wl_surface);
wl_compositor_destroy(wl_compositor);
wl_registry_destroy(wl_registry);
wl_display_disconnect(wl_display);
return 0;
}
編譯、執行
$ gcc main.c -o main -lwayland-client -lwayland-egl -lEGL -lGLESv2 $ ./main