wayland(xdg_wm_base) + egl + opengles 最简实例

2024-01-21 07:36

本文主要是介绍wayland(xdg_wm_base) + egl + opengles 最简实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、ubuntu 下相关环境准备
    • 1. 获取 xdg_wm_base 依赖的相关文件
    • 2. 查看 ubuntu 上安装的opengles 版本
    • 3. 查看 weston 所支持的 窗口shell 接口种类
  • 二、xdg_wm_base 介绍
  • 三、egl_wayland_demo
    • 1.egl_wayland_demo2_0.c
    • 2.egl_wayland_demo3_0.c
    • 3. xdg-shell-protocol.c和 xdg-shell-client-protocol.h
    • 4. 编译和运行
      • 4.1 编译
      • 4.2 运行
  • 总结
  • 参考资料


前言

`本文主要介绍如何在linux 下,基于xdg_wm_base 接口的 wayland client 中 使用 egl + opengles 渲染一个最基本的三角形
软硬件环境:
硬件:PC
软件:
ubuntu22.04
EGL1.4
openGL ES3.1
weston9.0


一、ubuntu 下相关环境准备

1. 获取 xdg_wm_base 依赖的相关文件

之前的文章 weston 源码下载及编译 介绍了如何在ubuntu 22.04 下面编译 weston9.0 ,在编译结束后,在build 目录下会生成 xdg-shell-protocol.cxdg-shell-client-protocol.h 这两个文件,如下图所示
在这里插入图片描述
xdg-shell-protocol.cxdg-shell-client-protocol.h 这两个文件就是使用 xdg_wm_base 时所依赖的,这里就直接从weston9.0的目录下获取了,当然也可以使用wayland 相关的命令(wayland-scanner)去操作对应的.xml 文件来生成它们。

(wayland-scanner)是一个用于生成Wayland协议代码的工具。它是Wayland项目的一部分,用于根据XML描述文件生成C语言代码,以便在应用程序中使用Wayland协议。
Wayland-Scanner工具的用途是将Wayland协议的XML描述文件转换为可供应用程序使用的C语言代码。这些代码包括客户端和服务器端的接口定义、消息处理函数、数据结构等。通过使用Wayland-Scanner,开发人员可以根据自定义的Wayland协议描述文件生成所需的代码,从而实现与Wayland服务器的通信

2. 查看 ubuntu 上安装的opengles 版本

使用 glxinfo | grep “OpenGL ES” 可以查看 ubuntu 上所安装的opengles 的版本,如下图所示,代表当前 ubuntu (ubuntu22.04)上安装的是opengles版本是 opengles3.1
在这里插入图片描述

3. 查看 weston 所支持的 窗口shell 接口种类

直接执行 weston-info(后面会被wayland-info 命令取代) 命令,如下图所示,可以看到,当前ubuntu(ubuntu22.04)上所支持的窗口 shell 接口种类,其中就有 xdg_wm_base, zxdg_shell_v6 等,但是没有 wl_shell , 所以从这可以看出,wl_shell 确实已经在ubuntu22.04 上废弃使用了。
在这里插入图片描述

二、xdg_wm_base 介绍

  • xdg_wm_base是由XDG组织定义的Wayland协议接口之一,它提供了一种标准化的方法,使得窗口管理器和应用程序能够进行互动;
  • xdg_wm_base定义了与窗口管理器交互的基本功能,如创建新窗口、设置窗口标题、最大化和最小化窗口等;
  • 它鼓励窗口管理器使用更加灵活和自由的方式来处理窗口管理任务;
  • xdg_wm_base和 wl_shell 都是Wayland协议的一部分,用于定义窗口管理器(Window Manager)与应用程序之间的通信接口。它们之间的区别在于设计理念和功能,xdg_wm_base提供了更加灵活和标准化的窗口管理器接口,窗口管理器可以根据需要实现更多的功能;

三、egl_wayland_demo

1.egl_wayland_demo2_0.c

使用 opengles2.x 接口的 egl_wayland_demo2_0.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 "xdg-shell-client-protocol.h"#define WIDTH 640
#define HEIGHT 480struct 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);
}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};/*for shell surface listener*/static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial) 
{wl_shell_surface_pong (shell_surface, serial);
}
static void shell_surface_configure (void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {struct window *window = data;wl_egl_window_resize (window->egl_window, width, height, 0, 0);
}
static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface) 
{}static struct wl_shell_surface_listener shell_surface_listener = {&shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done};static void create_window (struct window *window, int32_t width, int32_t height)
{}
static void
handle_surface_configure(void *data, struct xdg_surface *surface,uint32_t serial)
{//struct window *window = data;xdg_surface_ack_configure(surface, serial);//window->wait_for_configure = false;
}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_demo");}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 opengles_init(GLuint *shaderProgram)
{GLuint fragmentShader = 0;GLuint vertexShader = 0;char msg[1000];GLsizei len;const char* const fragmentShaderSource = "precision mediump float;\n""void main()\n""{\n""   gl_FragColor = vec4 (1.0,0.0,0.0,1.0);\n""}\n";// Create a fragment shader objectfragmentShader = glCreateShader(GL_FRAGMENT_SHADER);// Load the source code into itglShaderSource(fragmentShader, 1, (const char**)&fragmentShaderSource, NULL);// Compile the source codeglCompileShader(fragmentShader);// Check that the shader compiledGLint isShaderCompiled;glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isShaderCompiled);if (!isShaderCompiled){// If an error happened, first retrieve the length of the log messageglGetShaderInfoLog(fragmentShader, sizeof msg, &len, msg);//glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &infoLogLength);// Allocate enough space for the message and retrieve it//std::vector<char> infoLog;//infoLog.resize(infoLogLength);//glGetShaderInfoLog(fragmentShader, infoLogLength, &charactersWritten, infoLog.data());fprintf(stderr, "Error: compiling %s: %.*s\n", "fragment", len, msg);return false;}// Vertex shader codeconst char* const vertexShaderSource =		"attribute vec4 vPosition; \n""void main()\n""{\n""   gl_Position = vPosition;\n""}\n";// Create a vertex shader objectvertexShader = glCreateShader(GL_VERTEX_SHADER);// Load the source code into the shaderglShaderSource(vertexShader, 1, (const char**)&vertexShaderSource, NULL);// Compile the shaderglCompileShader(vertexShader);// Check the shader has compiledglGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isShaderCompiled);if (!isShaderCompiled){// If an error happened, first retrieve the length of the log message//int infoLogLength, charactersWritten;//glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &infoLogLength);glGetShaderInfoLog(vertexShader, sizeof msg, &len, msg);// Allocate enough space for the message and retrieve it//std::vector<char> infoLog;//infoLog.resize(infoLogLength);//glGetShaderInfoLog(vertexShader, infoLogLength, &charactersWritten, infoLog.data());fprintf(stderr, "Error: compiling %s: %.*s\n", "vertex", len, msg);return false;}// Create the shader program*shaderProgram = glCreateProgram();// Attach the fragment and vertex shaders to itglAttachShader(*shaderProgram, fragmentShader);glAttachShader(*shaderProgram, vertexShader);// Bind the vertex attribute "myVertex" to location VertexArray (0)glBindAttribLocation(*shaderProgram, VertexArray, "vPosition");// Link the programglLinkProgram(*shaderProgram);// After linking the program, shaders are no longer necessaryglDeleteShader(vertexShader);glDeleteShader(fragmentShader);// Check if linking succeeded in the same way we checked for compilation successGLint isLinked;glGetProgramiv(*shaderProgram, GL_LINK_STATUS, &isLinked);if (!isLinked){// If an error happened, first retrieve the length of the log message//nt infoLogLength, charactersWritten;//glGetProgramiv(*shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);// Allocate enough space for the message and retrieve it//std::vector<char> infoLog;//infoLog.resize(infoLogLength);//glGetShaderInfoLog(shaderProgram, infoLogLength, &charactersWritten, infoLog.data());glGetProgramInfoLog(*shaderProgram, sizeof msg, &len, msg);fprintf(stderr, "Error: compiling %s: %.*s\n", "linkprogram", len, msg);return false;}//	Use the ProgramglUseProgram(*shaderProgram);return true;
}
bool renderScene(EGLDisplay eglDisplay, EGLSurface eglSurface)
{GLfloat vVertices[] = {0.0f,0.5f,0.0f,			//vertex pointer-0.5f,-0.5f,0.0f,0.5f,-0.5f,0.0f};glViewport(0, 0, WIDTH, HEIGHT);			//set the view portglClearColor(0.00f, 0.70f, 0.67f, 1.0f);		//set rgba value for backgroud glClear(GL_COLOR_BUFFER_BIT);glEnableVertexAttribArray(VertexArray);glVertexAttribPointer(VertexArray, 3, GL_FLOAT, GL_FALSE, 0, vVertices);glDrawArrays(GL_TRIANGLES, 0, 3);			//draw a triangleif (!eglSwapBuffers(eglDisplay, eglSurface)){return false;}return true;}bool render(GLuint shaderProgram, EGLDisplay eglDisplay, EGLSurface eglSurface)
{// Renders a triangle for 800 frames using the state setup in the previous functionfor (int i = 0; i < 800; ++i){wl_display_dispatch_pending(display);if (!renderScene(eglDisplay, eglSurface)) { return false; }}return true;
}void deInitializeGLState(GLuint shaderProgram)
{// Frees the OpenGL handles for the programglDeleteProgram(shaderProgram);
}void releaseEGLState(EGLDisplay eglDisplay)
{if (eglDisplay != NULL){// To release the resources in the context, first the context has to be released from its binding with the current thread.eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);// Terminate the display, and any resources associated with it (including the EGLContext)eglTerminate(eglDisplay);}
}int main()
{/* EGL variables*/EGLDisplay eglDisplay;EGLConfig eglConfig;EGLSurface eglSurface;EGLContext context;struct window window;//wayland client initif(!initializeWindow(&window)){printf("initializeWindow failed\n");return -1;}//egl init	eglDisplay = eglGetDisplay(display);if (eglDisplay == EGL_NO_DISPLAY){printf("Failed to get an EGLDisplay\n");return -1;}EGLint eglMajorVersion = 0;EGLint eglMinorVersion = 0;if (!eglInitialize(eglDisplay, &eglMajorVersion, &eglMinorVersion)){printf("Failed to initialize the EGLDisplay\n");return -1;} else {printf(" EGL%d.%d\n", eglMajorVersion, eglMinorVersion);printf(" EGL_CLIENT_APIS: %s\n", eglQueryString(eglDisplay, EGL_CLIENT_APIS));printf(" EGL_VENDOR: %s\n", eglQueryString(eglDisplay,  EGL_VENDOR));printf(" EGL_VERSION: %s\n", eglQueryString(eglDisplay,  EGL_VERSION));printf(" EGL_EXTENSIONS: %s\n", eglQueryString(eglDisplay,  EGL_EXTENSIONS));}int result = EGL_FALSE;result = eglBindAPI(EGL_OPENGL_ES_API);if (result != EGL_TRUE) { printf("eglBindAPI failed\n");return -1; }const EGLint configurationAttributes[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE };EGLint configsReturned;if (!eglChooseConfig(eglDisplay, configurationAttributes, &eglConfig, 1, &configsReturned) || (configsReturned != 1)){printf("Failed to choose a suitable config\n");return -1;}EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };context = eglCreateContext(eglDisplay, eglConfig, NULL, contextAttributes);if(!createEGLSurface(eglDisplay, eglConfig, &eglSurface, &window)){printf("Failed to create EGLSurface\n");return -1;}eglMakeCurrent(eglDisplay, eglSurface, eglSurface, context);const GLubyte* version = glGetString(GL_VERSION);if (version != NULL) {printf("OpenGL ES version: %s\n", version);} else {printf("Failed to get OpenGL ES version\n");}// opengles initGLuint shaderProgram = 0;opengles_init(&shaderProgram);//renderif(!render(shaderProgram, eglDisplay, eglSurface)) {printf("=========render failed\n");}//clean updeInitializeGLState(shaderProgram);// Release the EGL StatereleaseEGLState(eglDisplay);// Release the Wayland connectionreleaseWaylandConnection(&window);return 0;
}

2.egl_wayland_demo3_0.c

使用 opengles3.x 接口的 egl_wayland_demo3_0.c 代码如下

#include <wayland-client.h>
#include <wayland-server.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xdg-shell-client-protocol.h"#define WIDTH 640
#define HEIGHT 480struct 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

这篇关于wayland(xdg_wm_base) + egl + opengles 最简实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/628808

相关文章

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

MySQL的索引失效的原因实例及解决方案

《MySQL的索引失效的原因实例及解决方案》这篇文章主要讨论了MySQL索引失效的常见原因及其解决方案,它涵盖了数据类型不匹配、隐式转换、函数或表达式、范围查询、LIKE查询、OR条件、全表扫描、索引... 目录1. 数据类型不匹配2. 隐式转换3. 函数或表达式4. 范围查询之后的列5. like 查询6

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss