场景介绍
NativeWindow是HarmonyOS本地平台化窗口,表明图形行列的出产者端。开发者能够经过NativeWindow接口进行请求和提交Buffer,配置Buffer特点信息。
针对NativeWindow,常见的开发场景如下:
●经过NativeWindow供给的NativeAPI接口请求图形Buffer,并将出产图形内容写入图形Buffer,终究提交Buffer到图形行列
●在适配EGL层的eglswapbuffer接口时,进行请求和提交Buffer
接口阐明
接口名 | 描绘 |
---|---|
OH_NativeWindow_NativeWindowRequestBuffer(OHNativeWindow*window,OHNativeWindowBuffer**buffer,int*fenceFd) | 经过OHNativeWindow目标请求一块OHNativeWindowBuffer,用以内容出产。 |
OH_NativeWindow_NativeWindowFlushBuffer(OHNativeWindow*window,OHNativeWindowBuffer*buffer,intfenceFd,Regionregion) | 经过OHNativeWindow将出产好内容的OHNativeWindowBuffer放回到Buffer行列中,用以内容消费。 |
OH_NativeWindow_NativeWindowHandleOpt(OHNativeWindow*window,intcode,…) | 设置/获取OHNativeWindow的特点,包括设置/获取宽高、内容格局等。 |
具体的接口阐明请参考native_window。
开发过程
以下过程描绘了在HarmonyOS中怎么运用NativeWindow供给的NativeAPI接口,请求图形Buffer,并将出产图形内容写入图形Buffer后,终究提交Buffer到图形行列。
增加动态链接库
CMakeLists.txt中增加以下lib。
libace_ndk.z.so
libnative_window.so
头文件
#include<ace/xcomponent/native_interface_xcomponent.h>
#include<native_window/external_window.h>
1. 获取OHNativeWindow实例。
可在OH_NativeXComponent_Callback供给的接口中获取OHNativeWindow,下面供给一份代码示例。XComponent模块的具体运用方法请参考XComponent开发辅导。
a. 在xxx.ets中增加一个XComponent组件。
XComponent({id:'xcomponentId',type:'surface',libraryname:'entry'})
.width(360)
.height(360)
b. 在nativec 层获取NativeXComponent。
napi_valueexportInstance=nullptr;
//用来解分出被wrap了NativeXComponent指针的特点
napi_get_named_property(env,exports,OH_NATIVE_XCOMPONENT_OBJ,&exportInstance);
OH_NativeXComponent*nativeXComponent=nullptr;
//经过napi_unwrap接口,解分出NativeXComponent的实例指针
napi_unwrap(env,exportInstance,reinterpret_cast<void**>(&nativeXComponent));
//获取XComponentId
charidStr[OH_XCOMPONENT_ID_LEN_MAX 1]={};
uint64_tidSize=OH_XCOMPONENT_ID_LEN_MAX 1;
OH_NativeXComponent_GetXComponentId(nativeXComponent,idStr,&idSize);
c. 界说OH_NativeXComponent_Callback。
//界说回调函数
void OnSurfaceCreatedCB(OH_NativeXComponent*component, void*window)
{
//可获取OHNativeWindow实例
OHNativeWindow*nativeWindow= static_cast<OHNativeWindow*>(window);
//...
}
void OnSurfaceChangedCB(OH_NativeXComponent*component, void*window)
{
//可获取OHNativeWindow实例
OHNativeWindow*nativeWindow= static_cast<OHNativeWindow*>(window);
//...
}
void OnSurfaceDestroyedCB(OH_NativeXComponent*component, void*window)
{
//可获取OHNativeWindow实例
OHNativeWindow*nativeWindow= static_cast<OHNativeWindow*>(window);
//...
}
void DispatchTouchEventCB(OH_NativeXComponent*component, void*window)
{
//可获取OHNativeWindow实例
OHNativeWindow*nativeWindow= static_cast<OHNativeWindow*>(window);
//...
}
//初始化OH_NativeXComponent_Callback
OH_NativeXComponent_Callbackcallback;
callback.OnSurfaceCreated=OnSurfaceCreatedCB;
callback.OnSurfaceChanged=OnSurfaceChangedCB;
callback.OnSurfaceDestroyed=OnSurfaceDestroyedCB;
callback.DispatchTouchEvent=DispatchTouchEventCB;
d. 将OH_NativeXComponent_Callback注册给NativeXComponent。
//注册回调函数
OH_NativeXComponent_RegisterCallback(nativeXComponent,&callback);
2. 设置OHNativeWindowBuffer的特点。运用OH_NativeWindow_NativeWindowHandleOpt设置OHNativeWindowBuffer的特点。
//设置OHNativeWindowBuffer的宽高
int32_tcode=SET_BUFFER_GEOMETRY;
int32_twidth=0x100;
int32_theight=0x100;
//这里的nativeWindow是从上一过程中的回调函数中获得的
int32_tret=OH_NativeWindow_NativeWindowHandleOpt(nativeWindow,code,width,height);
3. 从图形行列请求OHNativeWindowBuffer。
OHNativeWindowBuffer*buffer=nullptr;
intfenceFd;
//经过OH_NativeWindow_NativeWindowRequestBuffer获取OHNativeWindowBuffer实例
OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow,&buffer,&fenceFd);
//经过OH_NativeWindow_GetBufferHandleFromNative获取buffer的handle
BufferHandle*bufferHandle=OH_NativeWindow_GetBufferHandleFromNative(buffer);
4. 内存映射mmap。
#include<sys/mman.h>
//运用体系接口mmap将bufferHandle对应的共享内存映射到用户空间,能够经过映射出来的虚拟地址向bufferHandle中写入图画数据
//bufferHandle->virAddr是bufferHandle在共享内存中的起始地址,bufferHandle->size是bufferHandle在共享内存中的内存占用大小
void*mappedAddr= mmap(bufferHandle->virAddr,bufferHandle->size,PROT_READ|PROT_WRITE,MAP_SHARED,bufferHandle->fd, 0);
if (mappedAddr==MAP_FAILED) {
//mmapfailed
}
5. 将出产的内容写入OHNativeWindowBuffer。
static uint32_tvalue= 0x00;
value ;
uint32_t *pixel= static_cast<uint32_t*>(mappedAddr); //运用mmap获取到的地址来访问内存
for (uint32_tx= 0;x<width;x ) {
for (uint32_ty= 0;y<height;y ) {
*pixel =value;
}
}
6. 提交OHNativeWindowBuffer到图形行列。
//设置刷新区域,如果Region中的Rect为nullptr,或者rectNumber为0,则认为OHNativeWindowBuffer全部有内容更改。
Regionregion{nullptr, 0};
//经过OH_NativeWindow_NativeWindowFlushBuffer提交给消费者运用,例如:显现在屏幕上。
OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow,buffer,fenceFd,region);
7. 撤销内存映射munmap。
//内存运用完记得去掉内存映射
intresult=munmap(mappedAddr,bufferHandle->size);
if(result==-1){
//munmapfailed
}