提交 18b91a10 编写于 作者: S shegangbin

fix review comment

Signed-off-by: Nshegangbin <shegangbin1@huawei.com>
上级 8a237d1e
# NativeBuffer 开发指导
# NativeBuffer开发指导
## 场景介绍
NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通过`NativeBuffer`接口实现共享内存的申请、使用、属性查询、释放等操作。
针对NativeBuffer,常见的开发场景如下:
* 通过`NativeBuffer`提供的`NAPI`接口申请`NativeBuffer`实例,获取内存的属性信息,把对应的ION内存映射到进程空间。
* 通过`NativeBuffer`提供的Native API接口申请`OH_NativeBuffer`实例,获取内存的属性信息,把对应的ION内存映射到进程空间。
## 接口说明
......@@ -13,7 +13,7 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通
| -------- | -------- |
| OH_NativeBuffer_Alloc (const OH_NativeBuffer_Config \*config) | 通过OH_NativeBuffer_Config创建OH_NativeBuffer实例,每次调用都会产生一个新的OH_NativeBuffer实例。 |
| OH_NativeBuffer_Reference (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对象的引用计数加1。 |
| OH_NativeBuffer_Unreference (OH_NativeImage \*image, uint32_t textureId) | 将OH_NativeBuffer对象的引用计数减1,当引用计数为0的时候,该NativeBuffer对象会被析构掉。 |
| OH_NativeBuffer_Unreference (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对象的引用计数减1,当引用计数为0的时候,该NativeBuffer对象会被析构掉。 |
| OH_NativeBuffer_GetConfig (OH_NativeBuffer \*buffer, OH_NativeBuffer_Config \*config) | 用于获取OH_NativeBuffer的属性。 |
| OH_NativeBuffer_Map (OH_NativeBuffer \*buffer, void \*\*virAddr) | 将OH_NativeBuffer对应的ION内存映射到进程空间。 |
| OH_NativeBuffer_Unmap (OH_NativeBuffer \*buffer) | 将OH_NativeBuffer对应的ION内存从进程空间移除。 |
......@@ -23,15 +23,18 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeBuffer`提供的`NAPI`接口,创建`NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。
以下步骤描述了在**OpenHarmony**中如何使用`NativeBuffer`提供的`NAPI`接口,创建`OH_NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。
**头文件**
```c++
#include <native_buffer/native_buffer.h>
```
1. **创建`NativeBuffer`实例**
1. **创建OH_NativeBuffer实例**
```c++
OH_NativeBuffer_Config config {
.width = 0x100,
.height = 0x100,
.format = GRAPHIC_PIXEL_FMT_RGBA_8888,
.usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA};
};
OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
if (buffer == nullptr) {
return;
......@@ -39,13 +42,15 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通
```
2. **将OH_NativeBuffer对应的ION内存映射到进程空间**
应用如需要访问这块buffer的内存空间,需要通过OH_NativeBuffer_Map接口将buffer对应的ION内存映射到进程空间
```c++
// 将ION内存映射到进程空间
int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr);
int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr); // 映射后通过第二个参数virAddr返回内存的首地址
if (ret != OHOS::GSERROR_OK) {
return;
}
// 将OH_NativeBuffer对应的ION内存从进程空间移除
// 使用后请及时将OH_NativeBuffer对应的ION内存从进程空间移除
ret = OH_NativeBuffer_Unmap(buffer);
if (ret != OHOS::GSERROR_OK) {
return;
......
# NativeImage 开发指导
# NativeImage开发指导
## 场景介绍
NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,表示图形队列的消费者端。开发者可以通过`NativeImage`接口接收和使用`Buffer`,并将`Buffer`关联输出到OpenGL外部纹理。
针对NativeImage,常见的开发场景如下:
* 通过`NativeImage`提供的`NAPI`接口创建`NativeImage`实例作为消费者端,获取与该实例对应的`NativeWindow`作为生产者端。`NativeWindow`相关接口可用于填充`Buffer`内容并提交,`NativeImage``Buffer`内容更新到OpenGL外部纹理上
* 通过`NativeImage`提供的Native API接口创建`NativeImage`实例作为消费者端,获取与该实例对应的`NativeWindow`作为生产者端。`NativeWindow`相关接口可用于填充`Buffer`内容并提交,`NativeImage``Buffer`内容更新到OpenGL外部纹理上。本模块需要配合NativeWindow、NativeBuffer、EGL、GLES3模块一起使用
## 接口说明
......@@ -24,25 +24,87 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeImage`提供的`NAPI`接口,创建`NativeImage`实例作为消费者端,将数据内容更新到OpenGL外部纹理上。
以下步骤描述了在**OpenHarmony**中如何使用`NativeImage`提供的Native API接口,创建`OH_NativeImage`实例作为消费者端,将数据内容更新到OpenGL外部纹理上。
**头文件**
```c++
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <native_image/native_image.h>
#include <native_window/external_window.h>
#include <native_buffer/native_buffer.h>
```
1. **初始化EGL环境**
这里提供一份初始化EGL环境的代码示例
```c++
EGLDisplay = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, NULL);
#include <EGL/egl.h>
#include <EGL/eglext.h>
using GetPlatformDisplayExt = PFNEGLGETPLATFORMDISPLAYEXTPROC;
constexpr const char* EGL_EXT_PLATFORM_WAYLAND = "EGL_EXT_platform_wayland";
constexpr const char* EGL_KHR_PLATFORM_WAYLAND = "EGL_KHR_platform_wayland";
constexpr int32_t EGL_CONTEXT_CLIENT_VERSION_NUM = 2;
constexpr char CHARACTER_WHITESPACE = ' ';
constexpr const char* CHARACTER_STRING_WHITESPACE = " ";
constexpr const char* EGL_GET_PLATFORM_DISPLAY_EXT = "eglGetPlatformDisplayEXT";
EGLContext eglContext_ = EGL_NO_CONTEXT;
EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
static inline EGLConfig config_;
static bool CheckEglExtension(const char* extensions, const char* extension)
{
size_t extlen = strlen(extension);
const char* end = extensions + strlen(extensions);
while (extensions < end) {
size_t n = 0;
if (*extensions == CHARACTER_WHITESPACE) {
extensions++;
continue;
}
n = strcspn(extensions, CHARACTER_STRING_WHITESPACE);
if (n == extlen && strncmp(extension, extensions, n) == 0) {
return true;
}
extensions += n;
}
return false;
}
static EGLDisplay GetPlatformEglDisplay(EGLenum platform, void* native_display, const EGLint* attrib_list)
{
static GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL;
if (!eglGetPlatformDisplayExt) {
const char* extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
if (extensions &&
(CheckEglExtension(extensions, EGL_EXT_PLATFORM_WAYLAND) ||
CheckEglExtension(extensions, EGL_KHR_PLATFORM_WAYLAND))) {
eglGetPlatformDisplayExt = (GetPlatformDisplayExt)eglGetProcAddress(EGL_GET_PLATFORM_DISPLAY_EXT);
}
}
if (eglGetPlatformDisplayExt) {
return eglGetPlatformDisplayExt(platform, native_display, attrib_list);
}
return eglGetDisplay((EGLNativeDisplayType)native_display);
}
static void InitEGLEnv()
{
eglDisplay_ = GetPlatformEglDisplay(EGL_PLATFORM_OHOS_KHR, EGL_DEFAULT_DISPLAY, NULL);
if (eglDisplay_ == EGL_NO_DISPLAY) {
std::cout << "Failed to create EGLDisplay gl errno : " << eglGetError() << std::endl;
return;
}
EGLint major, minor;
if (eglInitialize(eglDisplay_, &major, &minor) == EGL_FALSE) {
std::cout << "Failed to initialize EGLDisplay" << std::endl;
return;
}
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
std::cout << "Failed to bind OpenGL ES API" << std::endl;
return;
}
unsigned int ret;
......@@ -53,7 +115,6 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,
ret = eglChooseConfig(eglDisplay_, config_attribs, &config_, 1, &count);
if (!(ret && static_cast<unsigned int>(count) >= 1)) {
std::cout << "Failed to eglChooseConfig" << std::endl;
return;
}
static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE };
......@@ -61,15 +122,15 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,
eglContext_ = eglCreateContext(eglDisplay_, config_, EGL_NO_CONTEXT, context_attribs);
if (eglContext_ == EGL_NO_CONTEXT) {
std::cout << "Failed to create egl context %{public}x, error:" << eglGetError() << std::endl;
return;
}
eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_);
std::cout << "Create EGL context successfully, version" << major << "." << minor << std::endl;
}
```
2. **创建NativeImage实例**
2. **创建OH_NativeImage实例**
```c++
// 创建 OpenGL 纹理
GLuint textureId;
......@@ -82,56 +143,34 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,
```c++
// 获取生产者NativeWindow
OHNativeWindow* nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
// 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 NativeWindowBuffer 实例
OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer, &fenceFd);
// 通过 OH_NativeWindow_GetNativeBufferHandleFromNative 获取 buffer 的 handle
BufferHandle* bufferHandle = OH_NativeWindow_GetNativeBufferHandleFromNative(buffer);
```
4. **将生产的内容写入NativeWindowBuffer**
1. 设置NativeWindowBuffer的属性
1. 从NativeWindow中获取NativeWindowBuffer
```c++
// 设置 NativeWindowBuffer 的读写场景
int code = SET_USAGE;
int32_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, usage);
// 设置 NativeWindowBuffer 的宽高
code = SET_BUFFER_GEOMETRY;
OHNativeWindowBuffer* buffer = nullptr;
int fenceFd;
// 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 OHNativeWindowBuffer 实例
OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
int code = SET_BUFFER_GEOMETRY;
int32_t width = 0x100;
int32_t height = 0x100;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
// 设置 NativeWindowBuffer 的步长
code = SET_STRIDE;
int32_t stride = 0x8;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride);
// 设置 NativeWindowBuffer 的格式
code = SET_FORMAT;
int32_t format = PIXEL_FMT_RGBA_8888;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format);
```
2. 从图形队列申请NativeWindowBuffer
```c++
struct NativeWindowBuffer* buffer = nullptr;
int fenceFd;
// 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 NativeWindowBuffer 实例
OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &buffer, &fenceFd);
// 通过 OH_NativeWindow_GetNativeBufferHandleFromNative 获取 buffer 的 handle
BufferHandle* bufferHandle = OH_NativeWindow_GetNativeBufferHandleFromNative(buffer);
```
3. 将生产的内容写入NativeWindowBuffer
```c++
auto data = static_cast<uint8_t *>(buffer->sfbuffer->GetVirAddr());
static uint32_t value = 0x00;
value++;
uint32_t *pixel = static_cast<uint32_t *>(data);
uint32_t *pixel = static_cast<uint32_t *>(handle->virAddr);
for (uint32_t x = 0; x < width; x++) {
for (uint32_t y = 0; y < height; y++) {
*pixel++ = value;
}
}
```
4. 提交NativeWindowBuffer到图形队列
4. 将NativeWindowBuffer提交到NativeWindow
```c++
// 设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为NativeWindowBuffer全部有内容更改。
Region region{nullptr, 0};
......
# NativeVsync 开发指导
# NativeVsync开发指导
## 场景介绍
NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例的创建、销毁,以及VSync信号到来时调用设置的调用回调函数能力
NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例的创建、销毁以及设置VSync回调函数的能力,VSync信号到来时会调用已设置的VSync回调函数
## 接口说明
......@@ -17,15 +17,13 @@ NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeVsync`提供的`NAPI`接口,创建和销毁`NativeVsync`实例,以及如何设置VSync信号到来时想要调用的回调函数。
1. **获取NativeVsync实例**
以下步骤描述了在**OpenHarmony**中如何使用`NativeVsync`提供的Native API接口,创建和销毁`OH_NativeVsync`实例,以及如何设置VSync回调函数。
**头文件**
```c++
char name[] = "hello_vsync";
OH_NativeVSync* nativeVSync = OH_NativeVSync_Create(name, strlen(name));
#include <native_vsync/native_vsync.h>
```
2. **设置VSync信号到来时想要调用的回调函数**
1. **首先需要准备一个VSync回调函数**
```c++
static bool flag = false;
static void OnVSync(long long timestamp, void* data)
......@@ -33,16 +31,25 @@ NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例
flag = true;
std::cout << "OnVSync: " << timestamp << std::endl;
}
OH_NativeVSync_FrameCallback callback = OnVSync;
OH_NativeVSync_FrameCallback callback = OnVSync; // 回调函数必须是OH_NativeVSync_FrameCallback类型
```
2. **创建OH_NativeVsync实例**
```c++
char name[] = "hello_vsync";
OH_NativeVSync* nativeVSync = OH_NativeVSync_Create(name, strlen(name));
```
3. **通过OH_NativeVsync实例设置VSync回调函数**。
```c++
OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
while (!flag) {
while (!flag) { // 判断flag值,上面的VSync回调函数被执行后才会跳出while循环,表示已经接收到VSync信号
std::cout << "wait for vsync!\n";
sleep(1);
}
std::cout << "vsync come, end this thread\n";
```
3. **销毁NativeVsync实例**。
4. **销毁OH_NativeVsync实例**。
```c++
OH_NativeVSync_Destroy(nativeVSync);
OH_NativeVSync_Destroy(nativeVSync); // 如不需要接收VSync信号,请及时销毁OH_NativeVsync实例
```
\ No newline at end of file
......@@ -24,16 +24,20 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生
| OH_NativeWindow_NativeObjectReference (void \*obj) | 增加一个NativeObject的引用计数。 |
| OH_NativeWindow_NativeObjectUnreference (void \*obj) | 减少一个NativeObject的引用计数,当引用计数减少为0时,该NativeObject将被析构掉。 |
| OH_NativeWindow_GetNativeObjectMagic (void \*obj) | 获取NativeObject的MagicId。 |
| OH_NativeWindow_NativeWindowSetScalingMode (OHNativeWindow \*window, uint32_t sequence, OHScalingMode scalingMode) | 设置OHNativeWindow的缩放模式,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetMetaData(OHNativeWindow \*window, uint32_t sequence, int32_t size, const OHHDRMetaData \*metaData) | 设置OHNativeWindow的HDR静态元数据,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetMetaDataSet(OHNativeWindow \*window, uint32_t sequence, OHHDRMetadataKey key, int32_t size, const uint8_t \*metaData) | 设置OHNativeWindow的HDR静态元数据集,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetTunnelHandle(OHNativeWindow \*window, const OHExtDataHandle \*handle) | 设置OHNativeWindow的TunnelHandle,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetScalingMode (OHNativeWindow \*window, uint32_t sequence, OHScalingMode scalingMode) | 设置OHNativeWindow的缩放模式,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetMetaData(OHNativeWindow \*window, uint32_t sequence, int32_t size, const OHHDRMetaData \*metaData) | 设置OHNativeWindow的HDR静态元数据,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetMetaDataSet(OHNativeWindow \*window, uint32_t sequence, OHHDRMetadataKey key, int32_t size, const uint8_t \*metaData) | 设置OHNativeWindow的HDR静态元数据集,此接口从API version 10开始废弃,不提供替代接口。 |
| OH_NativeWindow_NativeWindowSetTunnelHandle(OHNativeWindow \*window, const OHExtDataHandle \*handle) | 设置OHNativeWindow的TunnelHandle,此接口从API version 10开始废弃,不提供替代接口。 |
详细的接口说明请参考[native_window](../reference/native-apis/_native_window.md)
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeWindow`提供的`NAPI`接口,申请图形`Buffer`,并将生产图形内容写入图形`Buffer`后,最终提交`Buffer`到图形队列。
**头文件**
```c++
#include <native_window/external_window.h>
```
1. **获取OHNativeWindow实例**。可在[`OH_NativeXComponent_Callback`](../reference/native-apis/_o_h___native_x_component___callback.md)提供的接口中获取。
1. 在xxx.ets 中定义 XComponent。
......@@ -100,10 +104,6 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生
2. **设置NativeWindowBuffer的属性**。使用`OH_NativeWindow_NativeWindowHandleOpt`设置`NativeWindowBuffer`的属性。
```c++
// 设置 NativeWindowBuffer 的读写场景
int code = SET_USAGE;
int32_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, usage);
// 设置 NativeWindowBuffer 的宽高
code = SET_BUFFER_GEOMETRY;
int32_t width = 0x100;
......@@ -113,10 +113,6 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生
code = SET_STRIDE;
int32_t stride = 0x8;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride);
// 设置 NativeWindowBuffer 的格式
code = SET_FORMAT;
int32_t format = PIXEL_FMT_RGBA_8888;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format);
```
3. **从图形队列申请NativeWindowBuffer**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册