From 937cc87164468eaf0caed67ac5eaa82161c07eb6 Mon Sep 17 00:00:00 2001 From: shegangbin Date: Tue, 4 Jul 2023 18:12:59 +0800 Subject: [PATCH] fix Signed-off-by: shegangbin --- .../napi/native-buffer-guidelines.md | 24 +++++---- .../napi/native-image-guidelines.md | 50 ++++++++++++------- .../napi/native-vsync-guidelines.md | 7 +-- .../napi/native-window-guidelines.md | 29 +++++------ 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/zh-cn/application-dev/napi/native-buffer-guidelines.md b/zh-cn/application-dev/napi/native-buffer-guidelines.md index 08dc52b335..7feff7533d 100644 --- a/zh-cn/application-dev/napi/native-buffer-guidelines.md +++ b/zh-cn/application-dev/napi/native-buffer-guidelines.md @@ -23,11 +23,12 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通 ## 开发步骤 -以下步骤描述了在**OpenHarmony**中如何使用`NativeBuffer`提供的`NAPI`接口,创建`OH_NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。 +以下步骤描述了在**OpenHarmony**中如何使用`NativeBuffer`提供的Native API接口,创建`OH_NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。 + **头文件** - ```c++ - #include - ``` +```c++ +#include +``` 1. **创建OH_NativeBuffer实例**。 ```c++ @@ -37,7 +38,7 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通 }; OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config); if (buffer == nullptr) { - return; + std::cout << "OH_NativeBuffer_Alloc Failed" << std::endl; } ``` @@ -45,15 +46,16 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通 应用如需要访问这块buffer的内存空间,需要通过OH_NativeBuffer_Map接口将buffer对应的ION内存映射到进程空间 ```c++ // 将ION内存映射到进程空间 + void* virAddr = nullptr; int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr); // 映射后通过第二个参数virAddr返回内存的首地址 - if (ret != OHOS::GSERROR_OK) { - return; + if (ret != 0) { + std::cout << "OH_NativeBuffer_Map Failed" << std::endl; } // 使用后请及时将OH_NativeBuffer对应的ION内存从进程空间移除 ret = OH_NativeBuffer_Unmap(buffer); - if (ret != OHOS::GSERROR_OK) { - return; + if (ret != 0) { + std::cout << "OH_NativeBuffer_Unmap Failed" << std::endl; } ``` @@ -70,7 +72,7 @@ NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通 ```c++ // 调用OH_NativeBuffer_Unreference引用计数减1,之后buffer的引用计数为0,buffer会销毁 ret = OH_NativeBuffer_Unreference(buffer); - if (ret != OHOS::GSERROR_OK) { - return; + if (ret != 0) { + std::cout << "OH_NativeBuffer_Unreference Failed" << std::endl; } ``` \ No newline at end of file diff --git a/zh-cn/application-dev/napi/native-image-guidelines.md b/zh-cn/application-dev/napi/native-image-guidelines.md index 00481f562d..1f200277b2 100644 --- a/zh-cn/application-dev/napi/native-image-guidelines.md +++ b/zh-cn/application-dev/napi/native-image-guidelines.md @@ -25,17 +25,19 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, ## 开发步骤 以下步骤描述了在**OpenHarmony**中如何使用`NativeImage`提供的Native API接口,创建`OH_NativeImage`实例作为消费者端,将数据内容更新到OpenGL外部纹理上。 + **头文件** - ```c++ - #include - #include - #include - #include - #include - #include - ``` +```c++ +#include +#include +#include +#include +#include +#include +``` 1. **初始化EGL环境**。 + 这里提供一份初始化EGL环境的代码示例 ```c++ #include @@ -51,6 +53,7 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, EGLDisplay eglDisplay_ = EGL_NO_DISPLAY; static inline EGLConfig config_; + // 检查egl扩展 static bool CheckEglExtension(const char* extensions, const char* extension) { size_t extlen = strlen(extension); @@ -71,6 +74,7 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, return false; } + // 获取当前的显示设备 static EGLDisplay GetPlatformEglDisplay(EGLenum platform, void* native_display, const EGLint* attrib_list) { static GetPlatformDisplayExt eglGetPlatformDisplayExt = NULL; @@ -93,16 +97,19 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, 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; } EGLint major, minor; + // 初始化EGLDisplay if (eglInitialize(eglDisplay_, &major, &minor) == EGL_FALSE) { std::cout << "Failed to initialize EGLDisplay" << std::endl; } + // 绑定图形绘制的API为OpenGLES if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { std::cout << "Failed to bind OpenGL ES API" << std::endl; } @@ -112,6 +119,7 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, EGLint config_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, EGL_NONE }; + // 获取一个有效的系统配置信息 ret = eglChooseConfig(eglDisplay_, config_attribs, &config_, 1, &count); if (!(ret && static_cast(count) >= 1)) { std::cout << "Failed to eglChooseConfig" << std::endl; @@ -119,13 +127,16 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, static const EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_NUM, EGL_NONE }; + // 创建上下文 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; } + // 关联上下文 eglMakeCurrent(eglDisplay_, EGL_NO_SURFACE, EGL_NO_SURFACE, eglContext_); + // EGL环境初始化完成 std::cout << "Create EGL context successfully, version" << major << "." << minor << std::endl; } ``` @@ -177,26 +188,24 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, // 通过OH_NativeWindow_NativeWindowFlushBuffer 提交给消费者使用,例如:显示在屏幕上。 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, buffer, fenceFd, region); ``` + 5. 用完需要销毁NativeWindow + ```c++ + OH_NativeWindow_DestroyNativeWindow(nativeWindow); + ``` 5. **更新内容到OpenGL纹理**。 ```c++ // 更新内容到OpenGL纹理。 int32_t ret = OH_NativeImage_UpdateSurfaceImage(image); - if (ret != SURFACE_ERROR_OK) { + if (ret != 0) { std::cout << "OH_NativeImage_UpdateSurfaceImage failed" << std::endl; - return -1; } // 获取最近调用OH_NativeImage_UpdateSurfaceImage的纹理图像的时间戳和变化矩阵。 int64_t timeStamp = OH_NativeImage_GetTimestamp(image); - if (timeStamp == SURFACE_ERROR_ERROR) { - std::cout << "OH_NativeImage_GetTimestamp failed" << std::endl; - return -1; - } float matrix[16]; ret = OH_NativeImage_GetTransformMatrix(image, matrix); - if (ret != SURFACE_ERROR_OK) { + if (ret != 0) { std::cout << "OH_NativeImage_GetTransformMatrix failed" << std::endl; - return -1; } ``` @@ -204,12 +213,17 @@ NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块, ```c++ // 将OH_NativeImage实例从当前OpenGL ES上下文分离 ret = OH_NativeImage_DetachContext(image); - if (ret != SURFACE_ERROR_OK) { + if (ret != 0) { std::cout << "OH_NativeImage_DetachContext failed" << std::endl; - return -1; } // 将OH_NativeImage实例附加到当前OpenGL ES上下文, 且该OpenGL ES纹理会绑定到 GL_TEXTURE_EXTERNAL_OES, 并通过OH_NativeImage进行更新 GLuint textureId2; glGenTextures(1, &textureId2); ret = OH_NativeImage_AttachContext(image, textureId2); ``` + +7. **OH_NativeImage实例使用完需要销毁掉**。 + ```c++ + // 销毁OH_NativeImage实例 + OH_NativeImage_Destroy(&image); + ``` diff --git a/zh-cn/application-dev/napi/native-vsync-guidelines.md b/zh-cn/application-dev/napi/native-vsync-guidelines.md index 1fd6f5e4ec..63da1350b9 100644 --- a/zh-cn/application-dev/napi/native-vsync-guidelines.md +++ b/zh-cn/application-dev/napi/native-vsync-guidelines.md @@ -18,10 +18,11 @@ NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例 ## 开发步骤 以下步骤描述了在**OpenHarmony**中如何使用`NativeVsync`提供的Native API接口,创建和销毁`OH_NativeVsync`实例,以及如何设置VSync回调函数。 + **头文件** - ```c++ - #include - ``` +```c++ +#include +``` 1. **首先需要准备一个VSync回调函数** ```c++ diff --git a/zh-cn/application-dev/napi/native-window-guidelines.md b/zh-cn/application-dev/napi/native-window-guidelines.md index f97bcd80d5..1c2e232d8f 100644 --- a/zh-cn/application-dev/napi/native-window-guidelines.md +++ b/zh-cn/application-dev/napi/native-window-guidelines.md @@ -5,7 +5,7 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生产者端。开发者可以通过`NativeWindow`接口进行申请和提交`Buffer`,配置`Buffer`属性信息。 针对NativeWindow,常见的开发场景如下: -* 通过`NativeWindow`提供的`NAPI`接口申请图形`Buffer`,并将生产图形内容写入图形`Buffer`,最终提交`Buffer`到图形队列 +* 通过`NativeWindow`提供的Native API接口申请图形`Buffer`,并将生产图形内容写入图形`Buffer`,最终提交`Buffer`到图形队列 * 在适配EGL层的`eglswapbuffer`接口时,进行申请和提交`Buffer` ## 接口说明 @@ -20,11 +20,12 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生 ## 开发步骤 -以下步骤描述了在**OpenHarmony**中如何使用`NativeWindow`提供的`NAPI`接口,申请图形`Buffer`,并将生产图形内容写入图形`Buffer`后,最终提交`Buffer`到图形队列。 +以下步骤描述了在**OpenHarmony**中如何使用`NativeWindow`提供的Native API接口,申请图形`Buffer`,并将生产图形内容写入图形`Buffer`后,最终提交`Buffer`到图形队列。 + **头文件** - ```c++ - #include - ``` +```c++ +#include +``` 1. **获取OHNativeWindow实例**。可在[`OH_NativeXComponent_Callback`](../reference/native-apis/_o_h___native_x_component___callback.md)提供的接口中获取。 1. 在xxx.ets 中定义 XComponent。 @@ -89,30 +90,30 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生 OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback_); ``` -2. **设置NativeWindowBuffer的属性**。使用`OH_NativeWindow_NativeWindowHandleOpt`设置`NativeWindowBuffer`的属性。 +2. **设置OHNativeWindowBuffer的属性**。使用`OH_NativeWindow_NativeWindowHandleOpt`设置`OHNativeWindowBuffer`的属性。 ```c++ - // 设置 NativeWindowBuffer 的宽高 + // 设置 OHNativeWindowBuffer 的宽高 code = SET_BUFFER_GEOMETRY; int32_t width = 0x100; int32_t height = 0x100; ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height); - // 设置 NativeWindowBuffer 的步长 + // 设置 OHNativeWindowBuffer 的步长 code = SET_STRIDE; int32_t stride = 0x8; ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride); ``` -3. **从图形队列申请NativeWindowBuffer**。 +3. **从图形队列申请OHNativeWindowBuffer**。 ```c++ - struct NativeWindowBuffer* buffer = nullptr; + OHNativeWindowBuffer* buffer = nullptr; int fenceFd; - // 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 NativeWindowBuffer 实例 + // 通过 OH_NativeWindow_NativeWindowRequestBuffer 获取 OHNativeWindowBuffer 实例 OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow_, &buffer, &fenceFd); // 通过 OH_NativeWindow_GetNativeBufferHandleFromNative 获取 buffer 的 handle BufferHandle* bufferHandle = OH_NativeWindow_GetNativeBufferHandleFromNative(buffer); ``` -4. **将生产的内容写入NativeWindowBuffer**。 +4. **将生产的内容写入OHNativeWindowBuffer**。 ```c++ auto image = static_cast(buffer->sfbuffer->GetVirAddr()); static uint32_t value = 0x00; @@ -126,9 +127,9 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生 } ``` -5. **提交NativeWindowBuffer到图形队列**。 +5. **提交OHNativeWindowBuffer到图形队列**。 ```c++ - // 设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为NativeWindowBuffer全部有内容更改。 + // 设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为OHNativeWindowBuffer全部有内容更改。 Region region{nullptr, 0}; // 通过OH_NativeWindow_NativeWindowFlushBuffer 提交给消费者使用,例如:显示在屏幕上。 OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow_, buffer, fenceFd, region); -- GitLab