未验证 提交 f0a9d469 编写于 作者: O openharmony_ci 提交者: Gitee

!20156 补充NDK资料

Merge pull request !20156 from shegangbin/ndk_doc
# NativeBuffer开发指导
## 场景介绍
NativeBuffer是`OpenHarmony`提供**共享内存**的模块。开发者可以通过`NativeBuffer`接口实现共享内存的申请、使用、属性查询、释放等操作。
针对NativeBuffer,常见的开发场景如下:
* 通过`NativeBuffer`提供的Native API接口申请`OH_NativeBuffer`实例,获取内存的属性信息,把对应的ION内存映射到进程空间。
## 接口说明
| 接口名 | 描述 |
| -------- | -------- |
| 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_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内存从进程空间移除。 |
| OH_NativeBuffer_GetSeqNum (OH_NativeBuffer \*buffer) | 获取OH_NativeBuffer的序列号。 |
详细的接口说明请参考[native_buffer](../reference/native-apis/_o_h___native_buffer.md)
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeBuffer`提供的Native API接口,创建`OH_NativeBuffer`实例获取内存的属性信息,并把对应的ION内存映射到进程空间。
**头文件**
```c++
#include <native_buffer/native_buffer.h>
```
1. **创建OH_NativeBuffer实例**
```c++
OH_NativeBuffer_Config config {
.width = 0x100,
.height = 0x100,
};
OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
if (buffer == nullptr) {
std::cout << "OH_NativeBuffer_Alloc Failed" << std::endl;
}
```
2. **将OH_NativeBuffer对应的ION内存映射到进程空间**
应用如需要访问这块buffer的内存空间,需要通过OH_NativeBuffer_Map接口将buffer对应的ION内存映射到进程空间
```c++
// 将ION内存映射到进程空间
void* virAddr = nullptr;
int32_t ret = OH_NativeBuffer_Map(buffer, &virAddr); // 映射后通过第二个参数virAddr返回内存的首地址
if (ret != 0) {
std::cout << "OH_NativeBuffer_Map Failed" << std::endl;
}
// 使用后请及时将OH_NativeBuffer对应的ION内存从进程空间移除
ret = OH_NativeBuffer_Unmap(buffer);
if (ret != 0) {
std::cout << "OH_NativeBuffer_Unmap Failed" << std::endl;
}
```
3. **获取内存的属性信息**
```c++
// 获取OH_NativeBuffer的属性
OH_NativeBuffer_Config config = {};
OH_NativeBuffer_GetConfig(buffer, &config);
// 获取OH_NativeBuffer的序列号
uint32_t hwBufferID = OH_NativeBuffer_GetSeqNum(buffer);
```
4. **销毁OH_NativeBuffer**
```c++
// 调用OH_NativeBuffer_Unreference引用计数减1,之后buffer的引用计数为0,buffer会销毁
ret = OH_NativeBuffer_Unreference(buffer);
if (ret != 0) {
std::cout << "OH_NativeBuffer_Unreference Failed" << std::endl;
}
```
\ No newline at end of file
# NativeImage开发指导
## 场景介绍
NativeImage是`OpenHarmony`提供**Surface关联OpenGL外部纹理**的模块,表示图形队列的消费者端。开发者可以通过`NativeImage`接口接收和使用`Buffer`,并将`Buffer`关联输出到OpenGL外部纹理。
针对NativeImage,常见的开发场景如下:
* 通过`NativeImage`提供的Native API接口创建`NativeImage`实例作为消费者端,获取与该实例对应的`NativeWindow`作为生产者端。`NativeWindow`相关接口可用于填充`Buffer`内容并提交,`NativeImage``Buffer`内容更新到OpenGL外部纹理上。本模块需要配合NativeWindow、NativeBuffer、EGL、GLES3模块一起使用。
## 接口说明
| 接口名 | 描述 |
| -------- | -------- |
| OH_NativeImage_Create (uint32_t textureId, uint32_t textureTarget) | 创建一个OH_NativeImage实例,该实例与OpenGL ES的纹理ID和纹理目标相关联。 |
| OH_NativeImage_AcquireNativeWindow (OH_NativeImage \*image) | 获取与OH_NativeImage相关联的OHNativeWindow指针,该OHNativeWindow后续不再需要时需要调用 OH_NativeWindow_DestroyNativeWindow释放。 |
| OH_NativeImage_AttachContext (OH_NativeImage \*image, uint32_t textureId) | 将OH_NativeImage实例附加到当前OpenGL ES上下文,且该OpenGL ES纹理会绑定到 GL_TEXTURE_EXTERNAL_OES,并通过OH_NativeImage进行更新。 |
| OH_NativeImage_DetachContext (OH_NativeImage \*image) | 将OH_NativeImage实例从当前OpenGL ES上下文分离。 |
| OH_NativeImage_UpdateSurfaceImage (OH_NativeImage \*image) | 通过OH_NativeImage获取最新帧更新相关联的OpenGL ES纹理。 |
| OH_NativeImage_GetTimestamp (OH_NativeImage \*image) | 获取最近调用OH_NativeImage_UpdateSurfaceImage的纹理图像的相关时间戳。 |
| OH_NativeImage_GetTransformMatrix (OH_NativeImage \*image, float matrix[16]) | 获取最近调用OH_NativeImage_UpdateSurfaceImage的纹理图像的变化矩阵。 |
| OH_NativeImage_Destroy (OH_NativeImage \*\*image) | 销毁通过OH_NativeImage_Create创建的OH_NativeImage实例,销毁后该OH_NativeImage指针会被赋值为空。 |
详细的接口说明请参考[native_image](../reference/native-apis/_o_h___native_image.md)
## 开发步骤
以下步骤描述了在**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++
#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_;
// 检查egl扩展
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;
}
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;
}
unsigned int ret;
EGLint count;
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<unsigned int>(count) >= 1)) {
std::cout << "Failed to eglChooseConfig" << std::endl;
}
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;
}
```
2. **创建OH_NativeImage实例**
```c++
// 创建 OpenGL 纹理
GLuint textureId;
glGenTextures(1, &textureId);
// 创建 NativeImage 实例,关联 OpenGL 纹理
OH_NativeImage* image = OH_NativeImage_Create(textureId, GL_TEXTURE_2D);
```
3. **获取对应的数据生产者端NativeWindow**
```c++
// 获取生产者NativeWindow
OHNativeWindow* nativeWindow = OH_NativeImage_AcquireNativeWindow(image);
```
4. **将生产的内容写入NativeWindowBuffer**
1. 从NativeWindow中获取NativeWindowBuffer
```c++
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);
```
3. 将生产的内容写入NativeWindowBuffer
```c++
static uint32_t value = 0x00;
value++;
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提交到NativeWindow
```c++
// 设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为NativeWindowBuffer全部有内容更改。
Region region{nullptr, 0};
// 通过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 != 0) {
std::cout << "OH_NativeImage_UpdateSurfaceImage failed" << std::endl;
}
// 获取最近调用OH_NativeImage_UpdateSurfaceImage的纹理图像的时间戳和变化矩阵。
int64_t timeStamp = OH_NativeImage_GetTimestamp(image);
float matrix[16];
ret = OH_NativeImage_GetTransformMatrix(image, matrix);
if (ret != 0) {
std::cout << "OH_NativeImage_GetTransformMatrix failed" << std::endl;
}
```
6. **解绑OpenGL纹理,绑定到新的外部纹理上**
```c++
// 将OH_NativeImage实例从当前OpenGL ES上下文分离
ret = OH_NativeImage_DetachContext(image);
if (ret != 0) {
std::cout << "OH_NativeImage_DetachContext failed" << std::endl;
}
// 将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);
```
# NativeVsync开发指导
## 场景介绍
NativeVsync模块用来获取系统VSync信号,提供了OH_NativeVSync实例的创建、销毁以及设置VSync回调函数的能力,VSync信号到来时会调用已设置的VSync回调函数。
## 接口说明
| 接口名 | 描述 |
| -------- | -------- |
| OH_NativeVSync_Create (const char \*name, unsigned int length) | 创建一个OH_NativeVSync实例,每次调用都会产生一个新的实例。 |
| OH_NativeVSync_Destroy (OH_NativeVSync \*nativeVsync) | 销毁OH_NativeVSync实例。 |
| OH_NativeVSync_FrameCallback (long long timestamp, void \*data) | 回调函数的形式,timestamp表示时间戳,data为回调函数入参。 |
| OH_NativeVSync_RequestFrame (OH_NativeVSync \*nativeVsync, OH_NativeVSync_FrameCallback callback, void \*data) | 请求下一次VSync信号,当信号到来时,调用回调函数callback。 |
详细的接口说明请参考[native_vsync](../reference/native-apis/_native_vsync.md)
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeVsync`提供的Native API接口,创建和销毁`OH_NativeVsync`实例,以及如何设置VSync回调函数。
**头文件**
```c++
#include <native_vsync/native_vsync.h>
```
1. **首先需要准备一个VSync回调函数**
```c++
static bool flag = false;
static void OnVSync(long long timestamp, void* data)
{
flag = true;
std::cout << "OnVSync: " << timestamp << std::endl;
}
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) { // 判断flag值,上面的VSync回调函数被执行后才会跳出while循环,表示已经接收到VSync信号
std::cout << "wait for vsync!\n";
sleep(1);
}
std::cout << "vsync come, end this thread\n";
```
4. **销毁OH_NativeVsync实例**。
```c++
OH_NativeVSync_Destroy(nativeVSync); // 如不需要接收VSync信号,请及时销毁OH_NativeVsync实例
```
\ No newline at end of file
......@@ -2,40 +2,32 @@
## 场景介绍
NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生产者端。接口能力包括从`Surface`构建`NativeWindow`的能力,从`SurfaceBuffer`构建出`NativeWindowBuffer`的能力,开发者可以通过`NativeWindow`接口进行申请和提交`Buffer`
NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生产者端。开发者可以通过`NativeWindow`接口进行申请和提交`Buffer`,配置`Buffer`属性信息
针对NativeWindow,常见的开发场景如下:
* 通过`NativeWindow`提供的`NAPI`接口申请图形`Buffer`,并将生产图形内容写入图形`Buffer`,最终提交`Buffer`到图形队列
* 通过`NativeWindow`提供的Native API接口申请图形`Buffer`,并将生产图形内容写入图形`Buffer`,最终提交`Buffer`到图形队列
* 在适配EGL层的`eglswapbuffer`接口时,进行申请和提交`Buffer`
## 接口说明
| 接口名 | 描述 |
| -------- | -------- |
| OH_NativeWindow_CreateNativeWindowFromSurface (void \*pSurface) | 创建NativeWindow实例,每次调用都会产生一个新的NativeWindow实例。 |
| OH_NativeWindow_DestroyNativeWindow (OHNativeWindow \*window) | 将NativeWindow对象的引用计数减1,当引用计数为0的时候,该NativeWindow对象会被析构掉。 |
| OH_NativeWindow_CreateNativeWindowBufferFromSurfaceBuffer (void \*pSurfaceBuffer) | 创建NativeWindowBuffer实例,每次调用都会产生一个新的NativeWindowBuffer实例。 |
| OH_NativeWindow_DestroyNativeWindowBuffer (OHNativeWindowBuffer \*buffer) | 将NativeWindowBuffer对象的引用计数减1,当引用计数为0的时候,该NativeWindowBuffer对象会被析构掉。 |
| OH_NativeWindow_NativeWindowRequestBuffer (OHNativeWindow \*window, OHNativeWindowBuffer \*\*buffer, int \*fenceFd) | 通过NativeWindow对象申请一块NativeWindowBuffer,用以内容生产。 |
| OH_NativeWindow_NativeWindowFlushBuffer (OHNativeWindow \*window, OHNativeWindowBuffer \*buffer, int fenceFd, Region region) | 通过NativeWindow将生产好内容的NativeWindowBuffer放回到Buffer队列中,用以内容消费。 |
| OH_NativeWindow_NativeWindowAbortBuffer (OHNativeWindow \*window, OHNativeWindowBuffer \*buffer) | 通过NativeWindow将之前申请出来的NativeWindowBuffer返还到Buffer队列中,供下次再申请。 |
| OH_NativeWindow_NativeWindowHandleOpt (OHNativeWindow \*window, int code,...) | 设置/获取NativeWindow的属性,包括设置/获取宽高、内容格式等。 |
| OH_NativeWindow_GetBufferHandleFromNative (OHNativeWindowBuffer \*buffer) | 通过NativeWindowBuffer获取该buffer的BufferHandle指针。 |
| 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) | 设置NativeWindow的缩放模式。 |
| OH_NativeWindow_NativeWindowSetMetaData(OHNativeWindow \*window, uint32_t sequence, int32_t size, const OHHDRMetaData \*metaData) | 设置NativeWindow的HDR静态元数据。 |
| OH_NativeWindow_NativeWindowSetMetaDataSet(OHNativeWindow \*window, uint32_t sequence, OHHDRMetadataKey key, int32_t size, const uint8_t \*metaData) | 设置NativeWindow的HDR静态元数据集。 |
| OH_NativeWindow_NativeWindowSetTunnelHandle(OHNativeWindow \*window, const OHExtDataHandle \*handle) | 设置NativeWindow的TunnelHandle。 |
| OH_NativeWindow_NativeWindowRequestBuffer (OHNativeWindow \*window, OHNativeWindowBuffer \*\*buffer, int \*fenceFd) | 通过OHNativeWindow对象申请一块OHNativeWindowBuffer,用以内容生产。 |
| OH_NativeWindow_NativeWindowFlushBuffer (OHNativeWindow \*window, OHNativeWindowBuffer \*buffer, int fenceFd, Region region) | 通过OHNativeWindow将生产好内容的OHNativeWindowBuffer放回到Buffer队列中,用以内容消费。 |
| OH_NativeWindow_NativeWindowHandleOpt (OHNativeWindow \*window, int code,...) | 设置/获取OHNativeWindow的属性,包括设置/获取宽高、内容格式等。 |
详细的接口说明请参考[native_window](../reference/native-apis/_native_window.md)
## 开发步骤
以下步骤描述了在**OpenHarmony**中如何使用`NativeWindow`提供的`NAPI`接口,申请图形`Buffer`,并将生产图形内容写入图形`Buffer`后,最终提交`Buffer`到图形队列。
以下步骤描述了在**OpenHarmony**中如何使用`NativeWindow`提供的Native API接口,申请图形`Buffer`,并将生产图形内容写入图形`Buffer`后,最终提交`Buffer`到图形队列。
1. **获取NativeWindow实例**。可在[`OH_NativeXComponent_Callback`](../reference/native-apis/_o_h___native_x_component___callback.md)提供的接口中获取。
**头文件**
```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。
```ts
XComponent({ id: 'xcomponentId', type: 'surface', libraryname: 'nativerender'})
......@@ -62,25 +54,25 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生
// 定义回调函数
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
{
// 可获取 NativeWindow 实例
// 可获取 OHNativeWindow 实例
OHNativeWindow* nativeWindow = window;
// ...
}
void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window)
{
// 可获取 NativeWindow 实例
// 可获取 OHNativeWindow 实例
OHNativeWindow* nativeWindow = window;
// ...
}
void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)
{
// 可获取 NativeWindow 实例
// 可获取 OHNativeWindow 实例
OHNativeWindow* nativeWindow = window;
// ...
}
void DispatchTouchEventCB(OH_NativeXComponent* component, void* window)
{
// 可获取 NativeWindow 实例
// 可获取 OHNativeWindow 实例
OHNativeWindow* nativeWindow = window;
// ...
}
......@@ -98,38 +90,30 @@ NativeWindow是`OpenHarmony`**本地平台化窗口**,表示图形队列的生
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback_);
```
2. **设置NativeWindowBuffer的属性**。使用`OH_NativeWindow_NativeWindowHandleOpt`设置`NativeWindowBuffer`的属性。
2. **设置OHNativeWindowBuffer的属性**。使用`OH_NativeWindow_NativeWindowHandleOpt`设置`OHNativeWindowBuffer`的属性。
```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 的宽高
// 设置 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);
// 设置 NativeWindowBuffer 的格式
code = SET_FORMAT;
int32_t format = PIXEL_FMT_RGBA_8888;
ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format);
```
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<uint8_t *>(buffer->sfbuffer->GetVirAddr());
static uint32_t value = 0x00;
......@@ -143,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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册