前言

本文主要介绍如何在一个wayland client 里面使用 egl + opengles 读取一个 pattern 图片数据进行纹理贴图,在阅读本篇文章之前,建议先读一下之前的文章 《wayland(xdg_wm_base) + egl + opengles 最简实例(一)》
软硬件环境
硬件:PC
软件:ubuntu22.04 weston9.0 opengles2.0/3.0 egl1.4


一、使用gstreamer 获取 pattern 图片

在ubuntu 下执行如下命令可以获取一张格式为RGB888, 分辨率为640x480 的pattern 图片
gst-launch-1.0 videotestsrc num-buffers=1 foreground-color=0 ! video/x-raw,format=RGB, width=640,height=480 ! filesink location=./pattern_640x480.rgb
如下图所示:
wayland(xdg_wm_base) + egl + opengles 纹理贴图进阶实例(四)-LMLPHP
在PC 端使用YUV player 软件工具(格式和分辨率分别设置为RGB24和640x480)打开该图片的效果如下图所示
wayland(xdg_wm_base) + egl + opengles 纹理贴图进阶实例(四)-LMLPHP

二、代码实例

1. pattern 图片作为纹理数据源的代码实例

1.1 基于opengles2.0 接口的 egl_wayland_texture2_1.c

#include <wayland-client.h>
#include <wayland-server.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "xdg-shell-client-protocol.h"

#define WIDTH 800
#define HEIGHT 600

struct wl_display *display = NULL;
struct wl_compositor *compositor = NULL;
struct xdg_wm_base *wm_base = NULL;
struct wl_registry *registry = NULL;

struct window {
   
	struct wl_surface *surface;
	struct xdg_surface *xdg_surface;
	struct xdg_toplevel *xdg_toplevel;
	struct wl_egl_window *egl_window;
};

// Index to bind the attributes to vertex shaders
const unsigned int VertexArray = 0;

static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
{
   
		xdg_wm_base_pong(shell, serial);
}

/*for xdg_wm_base listener*/
static const struct xdg_wm_base_listener wm_base_listener = {
   
		xdg_wm_base_ping,
};

/*for registry listener*/
static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) 
{
   
	if (!strcmp(interface, "wl_compositor")) {
   
		compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
	} else if (strcmp(interface, "xdg_wm_base") == 0) {
   
		wm_base = wl_registry_bind(registry, name,
			&xdg_wm_base_interface, 1);
		xdg_wm_base_add_listener(wm_base, &wm_base_listener, NULL);
	}
}


void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name) 
{
   

}

static struct wl_registry_listener registry_listener = {
   registry_add_object, registry_remove_object};


static void
handle_surface_configure(void *data, struct xdg_surface *surface,
	uint32_t serial)
{
   
	xdg_surface_ack_configure(surface, serial);
}


static const struct xdg_surface_listener xdg_surface_listener = {
   
		handle_surface_configure
};

static void
handle_toplevel_configure(void *data, struct xdg_toplevel *toplevel,
				  int32_t width, int32_t height,
							  struct wl_array *states)
{
   
}

static void
handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
{
   
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
   
	handle_toplevel_configure,
	handle_toplevel_close,
};

bool initWaylandConnection()
{
   	
	if ((display = wl_display_connect(NULL)) == NULL)
	{
   
		printf("Failed to connect to Wayland display!\n");
		return false;
	}

	if ((registry = wl_display_get_registry(display)) == NULL)
	{
   
		printf("Faield to get Wayland registry!\n");
		return false;
	}

	wl_registry_add_listener(registry, &registry_listener, NULL);
	wl_display_dispatch(display);

	if (!compositor)
	{
   
		printf("Could not bind Wayland protocols!\n");
		return false;
	}

	return true;
}

bool initializeWindow(struct window *window)
{
   
	initWaylandConnection();
	window->surface = wl_compositor_create_surface (compositor);
	window->xdg_surface = xdg_wm_base_get_xdg_surface(wm_base, window->surface);
	if (window->xdg_surface == NULL)
	{
   
		printf("Failed to get Wayland xdg surface\n");
		return false;
	} else {
   
		xdg_surface_add_listener(window->xdg_surface, &xdg_surface_listener, window);
		window->xdg_toplevel =
			xdg_surface_get_toplevel(window->xdg_surface);
		xdg_toplevel_add_listener(window->xdg_toplevel,
			&xdg_toplevel_listener, window);
		xdg_toplevel_set_title(window->xdg_toplevel, "egl_wayland_texture");
	}

	return true;

}

void releaseWaylandConnection(struct window *window)
{
   
	if(window->xdg_toplevel)
		xdg_toplevel_destroy(window->xdg_toplevel);
	if(window->xdg_surface)
		xdg_surface_destroy(window->xdg_surface);
	wl_surface_destroy(window->surface);
	xdg_wm_base_destroy(wm_base);
	wl_compositor_destroy(compositor);
	wl_registry_destroy(registry);
	wl_display_disconnect(display);
}

bool createEGLSurface(EGLDisplay eglDisplay, EGLConfig eglConfig, EGLSurface *eglSurface, struct window *window)
{
   

	window->egl_window = wl_egl_window_create(window->surface, WIDTH, HEIGHT);
	if (window->egl_window == EGL_NO_SURFACE) {
    
		printf("Can't create egl window\n"); 
		return false;
	} else {
   
		printf("Created wl egl window\n");
	}

	*eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, window->egl_window, NULL);
	
	return true;
}

bool create_textures(GLuint *textureId)
{
   
	GLuint width, height;
	GLbyte *data;

	GLint fd;
	width = 640;
	height = 480;
	
	fd = open("./pattern_640x480.rgb", O_RDONLY);
	if(fd < 0) {
   
		printf("open failed\n");
		return false;
	}



	data = (GLbyte *)malloc(width * height * 3);
	if(<
02-14 11:17