本示例中主要介绍开发者在利用Native XComponent来获取NativeWindow实例,通过数据填充改变矩形框颜色。并提供从NativeImage中获取NativeWindow进行buffer轮转的案例,通过NativeWindow生产buffer来触发NativeImage的可用帧回调。
说明 本示例XComponent接口使用方法已停止演进,推荐使用方法请参考ArkTSXComponent示例。
主页 | 改变矩形框颜色 | 生产buffer | 获取可用帧回调次数 |
---|---|---|---|
attachbuffer | detachbuffer | fit模式(手机) | 拉伸模式(手机) |
使用说明
├──entry/src/main
│ ├──cpp // C++代码区
│ │ ├──CMakeLists.txt // CMake配置文件
│ │ ├──logger_common.h // 日志封装定义文件
│ │ ├──NativeImageAdaptor.cpp // NativeImage功能适配模块
│ │ ├──NativeRender.cpp // NativeWindow和XComponentContext联合显示模块
│ ├──ets // ets代码区
│ │ ├──entryability
│ │ │ ├──EntryAbility.ts // 程序入口类
| | ├──interface
│ │ │ └──NativeWindowContext.ts // NativeWindowContext
│ │ └──pages // 页面文件
│ │ └──Index.ets // 主界面
| ├──resources // 资源文件目录
1.通过在IDE中创建Native c++ 工程,在c++代码中定义对外接口为DrawColor,在js侧调用该接口可改变窗口颜色; 2.从NativeImage中创建NativeWindow,在c++代码中定义对外接口为ProduceBuffer,js调用该接口后,NativeWindow会生产buffer并将该buffer flush给NativeImage,之后NativeImage将收到可用帧的回调; 3.js侧调用GetAvailableCount接口,可以获取回调触发次数,并展示在界面上,正常情况下FlushBuffer的次数和可用帧回调触发次数一致; 4.在c++代码中定义对外接口AttachBuffer,js侧调用该接口后,当缓存区cachebuffer数量大于0时,会添加1个cachebuffer到NativeWindow中,cachebuffer数量为0时不操作; 5.js侧调用DetachBuffer接口,当NativeWindow的attachedbuffer数量大于0时,会将NativeWindow中的buffer移除,放置到缓存区中,当attachedbuffer为0时不操作; 6.js侧调用GetBufferQueueSize、GetAttachBufferCount、GetCacheBufferCount接口时,将获取当前NativeWindow的bufferqueue容量、已经attach到nativewindow的buffer数量,缓存区buffer数量 7.js侧调用ChangeScalingMode接口,c++代码中会切换DrawColor的scalingmode,切换显示模式
源码参考:cpp目录下的文件。
涉及到的相关接口:
接口名 | 描述 |
---|---|
OH_NativeWindow_CreateNativeWindow (void *pSurface) | 创建OHNativeWindow实例,每次调用都会产生一个新的OHNativeWindow实例 |
OH_NativeWindow_DestroyNativeWindow (OHNativeWindow *window) | 将OHNativeWindow对象的引用计数减1,当引用计数为0的时候,该OHNativeWindow对象会被析构掉 |
OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer (OH_NativeBuffer *nativeBuffer) | 创建OHNativeWindowBuffer实例,每次调用都会产生一个新的OHNativeWindowBuffer实例 |
OH_NativeWindow_DestroyNativeWindowBuffer (OHNativeWindowBuffer *buffer) | 将OHNativeWindowBuffer对象的引用计数减1,当引用计数为0的时候,该OHNativeWindowBuffer对象会被析构掉 |
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_GetLastFlushedBuffer (OHNativeWindow *window, OHNativeWindowBuffer **buffer) | 从OHNativeWindow获取上次送回到buffer队列中的OHNativeWindowBuffer |
OH_NativeWindow_NativeWindowHandleOpt (OHNativeWindow *window, int code,...) | 设置/获取OHNativeWindow的属性,包括设置/获取宽高、内容格式等 |
OH_NativeWindow_GetBufferHandleFromNative (OHNativeWindowBuffer *buffer) | 通过OHNativeWindowBuffer获取该buffer的BufferHandle指针 |
OH_NativeBuffer_SetColorSpace (OH_NativeBuffer *buffer, OH_NativeBuffer_ColorSpace colorSpace) | 为OH_NativeBuffer设置颜色空间属性 |
OH_NativeImage_SetOnFrameAvailableListener (OH_NativeImage *image, OH_OnFrameAvailableListener listener) | 设置帧可用回调 |
OH_NativeImage_UnsetOnFrameAvailableListener (OH_NativeImage *image) | 取消设置帧可用回调 |
OH_NativeImage_AcquireNativeWindow (OH_NativeImage *image) | 获取与OH_NativeImage相关联的OHNativeWindow指针。 该OHNativeWindow后续不再需要时需要调用OH_NativeWindow_DestroyNativeWindow释放 |
OH_NativeWindow_GetSurfaceId (OHNativeWindow *window, uint64_t *surfaceId) | 获取surfaceId |
OH_NativeWindow_CreateNativeWindowFromSurfaceId (uint64_t surfaceId, OHNativeWindow **window) | 通过surfaceId获取对应的OHNativeWindow |
OH_NativeBuffer_MapPlanes (OH_NativeBuffer *buffer, void **virAddr, OH_NativeBuffer_Planes *outPlanes) | 将OH_NativeBuffer对应的多通道ION内存映射到进程空间 |
OH_NativeBuffer_FromNativeWindowBuffer (OHNativeWindowBuffer *nativeWindowBuffer, OH_NativeBuffer **buffer) | 将OHNativeWindowBuffer实例转换为OH_NativeBuffer实例 |
OH_NativeWindow_NativeWindowAttachBuffer(OHNativeWindow *window, OHNativeWindowBuffer *buffer) | 将缓存区OHNativeWindowBuffer添加到OHNativeWindow中 |
OH_NativeWindow_NativeWindowDetachBuffer(OHNativeWindow *window, OHNativeWindowBuffer *buffer) | 将OHNativeWindow中的buffer移除,放置到缓存区中 |
OH_NativeWindow_WriteToParcel(OHNativeWindow* window, OHIPCParcel* parcel) | 向OHIPCParcel对象中写入一个OHNativeWindow对象 |
OH_NativeWindow_ReadFromParcel(OHIPCParcel *parcel, OHNativeWindow **window) | 从OHIPCParcel对象中读取一个OHNativeWindow对象 |
OH_NativeWindow_NativeWindowSetScalingModeV2(OHNativeWindow *window, OHScalingModeV2 scalingMode) | 为OHNativeWindow设置显示模式 |
详细的接口说明请参考NativeWindow,NativeImage,NativeBuffer。
不涉及。
XComponent NativeWindow NativeImage EGL。
如需单独下载本工程,执行如下命令:
git init
git config core.sparsecheckout true
echo code/BasicFeature/Native/NdkNativeWindow/ > .git/info/sparse-checkout
git remote add origin https://gitee.com/openharmony/applications_app_samples.git
git pull origin master
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。