GLEX:鸿蒙原生 OpenGL ES 渲染框架,让 GPU 编程不再复杂
项目简介
GLEX 是一个专为 HarmonyOS/OpenHarmony 设计的轻量级 OpenGL ES 渲染框架。它封装了 EGL 上下文管理、Shader 编译、多 Pass 渲染管线等底层细节,让开发者可以专注于渲染效果本身,而不是被繁琐的 OpenGL 样板代码困扰。
GitHub 仓库: github.com/HOSRookie/o…
ohpm 安装:
ohpm install @yunshen1933/ohos_glex
核心特性
1. 开箱即用的渲染链路
传统 OpenGL ES 开发需要手动管理 EGL 上下文、Surface 绑定、渲染线程等,GLEX 将这些封装为一行代码:
import { createGlexRenderer } from '@yunshen1933/ohos_glex';
const native = createGlexRenderer();
native.bindXComponent('your_xcomponent_id');
native.startRender();
2. 多 Pass 渲染管线
基于 RenderPass 和 RenderPipeline 架构,支持动态切换和组合渲染效果:
// 切换到星空效果
native.setPasses(['DemoPass']);
// 切换到斩击特效
native.setPasses(['AttackPass']);
// 组合多个 Pass
native.setPasses(['DemoPass', 'AttackPass', 'CustomPass']);
3. 独立渲染线程 + 帧率控制
渲染循环运行在独立线程,不阻塞 UI 主线程,支持自定义目标帧率:
native.setTargetFPS(60); // 设置 60fps
native.startRender(); // 启动渲染循环
native.stopRender(); // 停止渲染
4. 触摸事件转发
将 ArkTS 触摸事件无缝转发到 Native 渲染管线:
XComponent({ id: xId, type: XComponentType.SURFACE, libraryname: 'glex' })
.onTouch((event: TouchEvent) => {
const touch = event.touches[0];
const action = event.type === TouchType.Down ? 0 :
event.type === TouchType.Move ? 1 : 2;
native.setTouchEvent(touch.x, touch.y, action, touch.id);
});
5. Shader 资源管理
支持从 Rawfile 加载 Shader,或直接传入源码:
// 从 Rawfile 加载
native.loadShaderFromRawfile(resourceManager, 'vertex.glsl', 'fragment.glsl');
// 直接设置源码
native.setShaderSources(vertexSource, fragmentSource);
// 设置 Uniform
native.setUniform('u_time', Date.now() / 1000.0);
6. GPU 信息与性能监控
实时获取渲染信息和 GPU 资源统计:
const info = native.getGLInfo();
console.log(`分辨率: ${info.width}x${info.height}`);
console.log(`GPU: ${info.renderer}`);
const fps = native.getCurrentFPS();
console.log(`当前帧率: ${fps}`);
const stats = native.getGpuStats();
console.log(`Shader 数量: ${stats.shaderCount}`);
内置效果演示
星空效果(DemoPass)
基于 OpenGL ES 3.0 实现的动态星空 + 流星效果,展示基础粒子系统和动画能力。
斩击特效(AttackPass)
触摸触发的斩击效果,展示触摸事件与渲染管线的联动。
快速上手
安装依赖
ohpm install @yunshen1933/ohos_glex
最简示例
import { createGlexRenderer, GlexNativeInstance } from '@yunshen1933/ohos_glex';
@Entry
@Component
struct Index {
private xId: string = 'glex_' + Date.now().toString();
private native: GlexNativeInstance = createGlexRenderer();
build() {
XComponent({
id: this.xId,
type: XComponentType.SURFACE,
libraryname: 'glex',
})
.width('100%')
.height('100%')
.onLoad(() => {
this.native.bindXComponent(this.xId);
this.native.setPasses(['DemoPass']);
this.native.startRender();
})
.onDestroy(() => {
this.native.stopRender();
this.native.destroySurface();
this.native.unbindXComponent();
});
}
}
完整示例(带触摸交互)
import { createGlexRenderer, GlexNativeInstance, GLInfo } from '@yunshen1933/ohos_glex';
@Entry
@Component
struct Index {
private xId: string = 'glex_' + Date.now().toString();
private native: GlexNativeInstance = createGlexRenderer();
private viewWidth: number = 0;
private viewHeight: number = 0;
private touchScaleX: number = 1.0;
private touchScaleY: number = 1.0;
private updateTouchScale(): void {
const info: GLInfo = this.native.getGLInfo();
if (info.width > 0 && info.height > 0) {
this.touchScaleX = info.width / this.viewWidth;
this.touchScaleY = info.height / this.viewHeight;
}
}
build() {
Stack() {
XComponent({
id: this.xId,
type: XComponentType.SURFACE,
libraryname: 'glex',
})
.width('100%')
.height('100%')
.onLoad(() => {
this.native.bindXComponent(this.xId);
this.native.setTargetFPS(60);
this.native.setPasses(['AttackPass']);
this.native.startRender();
this.updateTouchScale();
})
.onDestroy(() => {
this.native.stopRender();
this.native.destroySurface();
this.native.unbindXComponent();
});
}
.onAreaChange((_oldValue, newValue) => {
this.viewWidth = Number(newValue.width);
this.viewHeight = Number(newValue.height);
this.updateTouchScale();
})
.onTouch((event: TouchEvent) => {
const touch = event.touches[0];
const x = touch.x * this.touchScaleX;
const y = touch.y * this.touchScaleY;
const action = event.type === TouchType.Down ? 0 :
event.type === TouchType.Move ? 1 : 2;
this.native.setTouchEvent(x, y, action, touch.id);
});
}
}
架构设计
核心模块
GLEX
├── GLContext # EGL 上下文管理(ES 3.2/3.0/2.0 自动回退)
├── ShaderProgram # Shader 编译与链接
├── RenderPass # 渲染阶段抽象基类
├── RenderPipeline # 多 Pass 渲染管线
├── RenderThread # 独立渲染线程
└── NAPI Bridge # ArkTS ↔ Native 桥接
扩展自定义 Pass
继承 RenderPass 基类,实现自定义渲染效果:
class CustomPass : public RenderPass {
public:
void onInit(GLContext* ctx) override {
// 初始化 Shader、Buffer 等资源
}
void onRender(GLContext* ctx, float deltaTime) override {
// 执行渲染逻辑
}
void onDestroy(GLContext* ctx) override {
// 释放资源
}
};
通过 PassRegistry 注册后即可在 ArkTS 中使用:
native.setPasses(['CustomPass']);
API 参考
GlexNativeInstance 核心方法
| 方法 | 说明 |
|---|---|
bindXComponent(id) | 绑定 XComponent ID |
unbindXComponent() | 解绑组件 |
startRender() | 启动渲染循环 |
stopRender() | 停止渲染循环 |
destroySurface() | 销毁 Surface 资源 |
setTargetFPS(fps) | 设置目标帧率 |
setPasses(names) | 设置 Pass 列表 |
addPass(name) | 添加 Pass |
removePass(name) | 移除 Pass |
setTouchEvent(x, y, action, id) | 传递触摸事件 |
getCurrentFPS() | 获取当前帧率 |
getGLInfo() | 获取 GPU 信息 |
getGpuStats() | 获取资源统计 |
setBackgroundColor(r, g, b, a) | 设置清屏颜色 |
setShaderSources(vs, fs) | 设置 Shader 源码 |
loadShaderFromRawfile(resMgr, vsPath, fsPath) | 从 Rawfile 加载 Shader |
setUniform(name, value) | 设置 Shader Uniform |
内置 Pass 名称
DemoPass:星空演示AttackPass:斩击特效ShaderPass:自定义 Shader 通道
触摸事件 Action 约定
0: Down1: Move2: Up3: Cancel/Other
环境要求
- DevEco Studio 5.0+
- HarmonyOS SDK API 12+
- OpenGL ES 3.0+
- ABI:
arm64-v8a、x86_64
版本更新
v1.0.2(2026-02-27)
- 修复多实例场景下触摸序列共享状态问题
- 修复
DemoPassGPU 资源统计回收不完整问题 - 修复类型声明与实际导出不一致问题
- 修复部分声明注释乱码与文档口径不一致问题
v1.0.0(2026-02-06)
- 首个正式版本发布
- 完整的 EGL 上下文管理
- 多 Pass 渲染管线
- 独立渲染线程
- 内置星空与斩击效果
项目特点
稳定优先
当前阶段以稳定迭代为主,默认保持 API 与渲染主链路稳定,不做无必要的大规模重构。仅在以下场景引入较大调整:
- HarmonyOS / ArkUI / NAPI 官方接口出现破坏性变化
- 必须处理的安全、兼容性或稳定性问题
- 影响长期维护成本的底层结构缺陷
开源协议
Apache-2.0
适用场景
- 游戏渲染引擎
- 粒子特效系统
- 数据可视化
- 自定义 UI 动画
- 图像处理与滤镜
- AR/VR 应用
技术亮点
- 零配置启动:无需手动管理 EGL 上下文、Surface 绑定等底层细节
- 多实例支持:同一页面可创建多个独立渲染实例,互不干扰
- 线程安全:渲染循环运行在独立线程,不阻塞 UI
- 动态 Pass 切换:运行时无缝切换渲染效果,无需重启
- 自动资源管理:生命周期自动管理 GPU 资源,避免内存泄漏
- 完善的错误处理:提供
getLastError()获取详细错误信息
性能表现
- 星空效果(DemoPass):稳定 60fps
- 斩击特效(AttackPass):触摸响应延迟 < 16ms
- 内存占用:基础渲染 < 10MB
- 启动时间:首帧渲染 < 100ms
后续规划
- 支持更多内置效果(水波纹、火焰、闪电等)
- 提供可视化 Shader 编辑器
- 增强 GPU 性能分析工具
- 支持 Compute Shader(OpenGL ES 3.1+)
- 提供更多示例项目
开源地址
GitHub: github.com/HOSRookie/o…
ohpm: @yunshen1933/ohos_glex
交流群
如果你在使用过程中遇到问题,或者有任何建议,欢迎加入微信交流群:
总结
GLEX 是一个专为鸿蒙生态打造的 OpenGL ES 渲染框架,它将复杂的 GPU 编程封装为简洁的 API,让开发者可以快速实现高性能的渲染效果。无论你是游戏开发者、UI 动画设计师,还是数据可视化工程师,GLEX 都能帮你节省大量底层适配时间,专注于创意实现。
立即体验:
ohpm install @yunshen1933/ohos_glex
期待你的 Star 和反馈!