鸿蒙(API 12 Beta6版)图形【NativeDisplaySoloist开发指导】方舟2D图形服务

如果开发者想在独立线程中进行帧率控制的Native侧业务,可以通过DisplaySoloist来实现,如游戏、自绘制UI框架对接等场景。

开发者可以选择多个DisplaySoloist实例共享一个线程,也可以选择每个DisplaySoloist实例独占一个线程。

接口说明

函数名称说明
OH_DisplaySoloist* OH_DisplaySoloist_Create (bool useExclusiveThread)创建一个OH_DisplaySoloist实例。
OH_DisplaySoloist_Destroy (OH_DisplaySoloist * displaySoloist)销毁一个OH_DisplaySoloist实例。
OH_DisplaySoloist_Start (OH_DisplaySoloist * displaySoloist, OH_DisplaySoloist_FrameCallback callback, void * data )设置每帧回调函数,每次VSync信号到来时启动每帧回调。
OH_DisplaySoloist_Stop (OH_DisplaySoloist * displaySoloist)停止请求下一次VSync信号,并停止调用回调函数callback。
OH_DisplaySoloist_SetExpectedFrameRateRange (OH_DisplaySoloist* displaySoloist, DisplaySoloist_ExpectedRateRange* range)设置期望帧率范围。

开发步骤

本范例是通过Drawing在Native侧实现图形的绘制,通过异步线程设置期望的帧率,再根据帧率进行图形的绘制并将其呈现在NativeWindow上。

添加开发依赖

添加动态链接库

CMakeLists.txt中添加以下lib。

libace_napi.z.so
libace_ndk.z.so
libnative_window.so
libnative_drawing.so
libnative_display_soloist.so

头文件

#include <ace/xcomponent/native_interface_xcomponent.h>
#include "napi/native_api.h"
#include <native_display_soloist/native_display_soloist.h>
#include <native_drawing/drawing_bitmap.h>
#include <native_drawing/drawing_color.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_path.h>
#include <native_window/external_window.h>
#include <cmath>
#include <algorithm>
#include <stdint.h>
#include <sys/mman.h>
  1. 定义ArkTS接口文件XComponentContext.ts,用来对接Native层。
export default interface XComponentContext {register(): void;unregister(): void;destroy(): void;
};
  1. 定义演示页面,包含两个XComponent组件。
import XComponentContext from "../interface/XComponentContext";@Entry
@Component
struct Index {private xComponentContext1: XComponentContext | undefined = undefined;private xComponentContext2: XComponentContext | undefined = undefined;build() {Column() {Row() {XComponent({ id: 'xcomponentId30', type: 'surface', libraryname: 'entry' }).onLoad((xComponentContext) => {this.xComponentContext1 = xComponentContext as XComponentContext;}).width('640px')}.height('40%')Row() {XComponent({ id: 'xcomponentId120', type: 'surface', libraryname: 'entry' }).onLoad((xComponentContext) => {this.xComponentContext2 = xComponentContext as XComponentContext;}).width('640px') // 64的倍数}.height('40%')}}
}
  1. 在 Native C++层获取NativeXComponent。建议使用单例模式保存XComponent。此步骤需要在napi_init的过程中处理。

    创建一个PluginManger单例类,用于管理NativeXComponent。

class PluginManager {
public:~PluginManager();static PluginManager *GetInstance();void SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent);SampleBitMap *GetRender(std::string &id);void Export(napi_env env, napi_value exports);
private:std::unordered_map<std::string, OH_NativeXComponent *> nativeXComponentMap_;std::unordered_map<std::string, SampleXComponent *> pluginRenderMap_;
};

SampleXComponent类会在后面的绘制图形中创建。

void PluginManager::Export(napi_env env, napi_value exports) {nativeXComponentMap_.clear();pluginRenderMap_.clear();if ((env == nullptr) || (exports == nullptr)) {DRAWING_LOGE("Export: env or exports is null");return;}napi_value exportInstance = nullptr;if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {DRAWING_LOGE("Export: napi_get_named_property fail");return;}OH_NativeXComponent *nativeXComponent = nullptr;if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {DRAWING_LOGE("Export: napi_unwrap fail");return;}char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {DRAWING_LOGE("Export: OH_NativeXComponent_GetXComponentId fail");return;}std::string id(idStr);auto context = PluginManager::GetInstance();if ((context != nullptr) && (nativeXComponent != nullptr)) {context->SetNativeXComponent(id, nativeXComponent);auto render = context->GetRender(id);if (render != nullptr) {render->RegisterCallback(nativeXComponent);render->Export(env, exports);} else {DRAWING_LOGE("render is nullptr");}}
}
  1. Native层配置帧率和注册回调函数。

    定义每帧回调函数内容。

static void TestCallback(long long timestamp, long long targetTimestamp, void *data) 
{// ...// 获取对应的XComponentOH_NativeXComponent *component = nullptr;component = static_cast<OH_NativeXComponent *>(data);if (component == nullptr) {SAMPLE_LOGE("TestCallback: component is null");return;}char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {SAMPLE_LOGE("TestCallback: Unable to get XComponent id");return;}std::string id(idStr);auto render = SampleXComponent::GetInstance(id);OHNativeWindow *nativeWindow = render->GetNativeWindow();uint64_t width;uint64_t height;// 获取XComponent的surface大小int32_t xSize = OH_NativeXComponent_GetXComponentSize(component, nativeWindow, &width, &height);if ((xSize == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) && (render != nullptr)) {render->Prepare();render->Create();if (id == "xcomponentId30") {// 30Hz绘制时,每帧移动的距离为16像素render->ConstructPath(16, 16, render->defaultOffsetY);}if (id == "xcomponentId120") {// 120Hz绘制时,每帧移动的距离为4像素render->ConstructPath(4, 4, render->defaultOffsetY);}// ...}
}

使用DisplaySoloist接口配置帧率和注册每帧回调函数。

说明

  • 实例在调用NapiRegister后,在不需要进行帧率控制时,应进行NapiUnregister操作,避免内存泄漏问题。
  • 在页面跳转时,应进行NapiUnregister和NapiDestroy操作,避免内存泄漏问题。
static std::unordered_map<std::string, OH_DisplaySoloist *> g_displaySync;napi_value SampleXComponent::NapiRegister(napi_env env, napi_callback_info info)
{// ...// 获取对应的XComponentnapi_value thisArg;if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) {SAMPLE_LOGE("NapiRegister: napi_get_cb_info fail");return nullptr;}napi_value exportInstance;if (napi_get_named_property(env, thisArg, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {SAMPLE_LOGE("NapiRegister: napi_get_named_property fail");return nullptr;}OH_NativeXComponent *nativeXComponent = nullptr;if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {SAMPLE_LOGE("NapiRegister: napi_unwrap fail");return nullptr;}char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {SAMPLE_LOGE("NapiRegister: Unable to get XComponent id");return nullptr;}SAMPLE_LOGI("RegisterID = %{public}s", idStr);std::string id(idStr);SampleXComponent *render = SampleXComponent().GetInstance(id);if (render != nullptr) {OH_DisplaySoloist *nativeDisplaySoloist = nullptr;if (g_displaySync.find(id) == g_displaySync.end()) {// 创建OH_DisplaySoloist实例// true表示OH_DisplaySoloist实例独占一个线程,false则表示共享一个线程g_displaySync[id] = OH_DisplaySoloist_Create(true);}nativeDisplaySoloist = g_displaySync[id];// 设置期望帧率范围// 此结构体成员变量分别为帧率范围的最小值、最大值以及期望帧率DisplaySoloist_ExpectedRateRange range;if (id == "xcomponentId30") {// 第一个XComponent期望帧率为30Hzrange = {30, 120, 30};}if (id == "xcomponentId120") {// 第二个XComponent期望帧率为120Hzrange = {30, 120, 120};}OH_DisplaySoloist_SetExpectedFrameRateRange(nativeDisplaySoloist, &range);// 注册回调与使能每帧回调OH_DisplaySoloist_Start(nativeDisplaySoloist, TestCallback, nativeXComponent);}// ...
}napi_value SampleXComponent::NapiUnregister(napi_env env, napi_callback_info info)
{// ...// 取消注册每帧回调OH_DisplaySoloist_Stop(g_displaySync[id]);; // ...
}napi_value SampleXComponent::NapiDestroy(napi_env env, napi_callback_info info)
{// ...// 销毁OH_DisplaySoloist实例OH_DisplaySoloist_Destroy(g_displaySync[id]);g_displaySync.erase(id);       // ...
}// 实现XComponentContext.ts中ArkTS接口与C++接口的绑定和映射。
void SampleXComponent::Export(napi_env env, napi_value exports) {if ((env == nullptr) || (exports == nullptr)) {SAMPLE_LOGE("Export: env or exports is null");return;}napi_property_descriptor desc[] = {{"register", nullptr, SampleXComponent::NapiRegister, nullptr, nullptr, nullptr, napi_default, nullptr},{"unregister", nullptr, SampleXComponent::NapiUnregister, nullptr, nullptr, nullptr, napi_default, nullptr},{"destroy", nullptr, SampleXComponent::NapiDestroy, nullptr, nullptr, nullptr, napi_default, nullptr}};napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);if (napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc) != napi_ok) {SAMPLE_LOGE("Export: napi_define_properties failed");}
}
  1. TS层注册和取消注册每帧回调,销毁OH_DisplaySoloist实例。
// 离开页面时,取消回调注册与销毁OH_DisplaySoloist实例
aboutToDisappear(): void {if (this.xComponentContext1) {this.xComponentContext1.unregister();this.xComponentContext1.destroy();}if (this.xComponentContext2) {this.xComponentContext2.unregister();this.xComponentContext2.destroy();}
}Row() {Button('Start').id('Start').fontSize(14).fontWeight(500).margin({ bottom: 20, right: 6, left: 6 }).onClick(() => {if (this.xComponentContext1) {this.xComponentContext1.register();}if (this.xComponentContext2) {this.xComponentContext2.register();}}).width('30%').height(40).shadow(ShadowStyle.OUTER_DEFAULT_LG)Button('Stop').id('Stop').fontSize(14).fontWeight(500).margin({ bottom: 20, left: 6 }).onClick(() => {if (this.xComponentContext1) {this.xComponentContext1.unregister();}if (this.xComponentContext2) {this.xComponentContext2.unregister();}}).width('30%').height(40).shadow(ShadowStyle.OUTER_DEFAULT_LG)
}

最后呢

很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。

而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。

在这里插入图片描述

针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。

  • 《鸿蒙 (OpenHarmony)开发学习视频》
  • 《鸿蒙生态应用开发V2.0白皮书》
  • 《鸿蒙 (OpenHarmony)开发基础到实战手册》
  • OpenHarmony北向、南向开发环境搭建
  • 《鸿蒙开发基础》
  • 《鸿蒙开发进阶》
  • 《鸿蒙开发实战》

在这里插入图片描述

总结

鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。

并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿
1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1522791.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

c++ 156函数

inline内联函数 #include<iostream> using namespace std;inline void printA() {int a 10;cout << "a:" << a << endl;}void main() {//printA();//c编译器会这样 把函数体机械地放到main函数里面{int a 10;cout << "a:"…

如何构建Java SpringBoot中药材管理系统,实现高效进存销,2025届必备技能!

✍✍计算机编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java实战 |…

Windows安装docker,启动ollama运行open-webui使用AIGC大模型写周杰伦歌词

Windows安装docker&#xff0c;启动ollama运行open-webui使用AIGC大模型写周杰伦歌词 1、下载docker的Windows版本。 docker下载地址&#xff1a; https://docs.docker.com/desktop/install/windows-install/https://docs.docker.com/desktop/install/windows-install/ 2、设…

ui 自动化测试过程是什么?

UI自动化测试是指通过模拟用户操作来测试应用程序的用户界面的一种测试方法。它可以模拟用户在应用程序上的操作&#xff0c;比如点击按钮、输入文本等&#xff0c;然后检查应用程序的响应是否符合预期。UI自动化测试可以提高测试效率并减少人工测试的工作量&#xff0c;同时可…

电脑永久删除的文件还能找回来吗?别再担心,误删文件也能救回!

在日常使用电脑的过程中&#xff0c;我们有时会因为各种原因而永久删除一些文件。这些文件可能是重要的工作文档、珍贵的照片&#xff0c;或者是其他对我们来说有价值的资料。一旦这些文件被永久删除&#xff0c;我们往往会感到焦虑和担忧&#xff0c;不知道是否还能够找回这些…

Linux核心技能:主流监控Prometheus详解,附官方可复制中文文档教程

Prometheus既是一个时序数据库&#xff0c;又是一个监控系统&#xff0c;更是一套完备的监控生态解决方案。作为时序数据库&#xff0c;目前Prometheus已超越了老牌的时序数据库OpenTSDB、Graphite、RRDtool、KairosDB等&#xff0c;如图所示。 &#xff08;来源网络&#xff0…

CAN-FD是怎么提高通信速率的?

经典CAN协议规定的最高速率是1Mb/s,汽车中实际应用的最高速率是500Kb/s&#xff0c;这个速度对于绝大部分ECU之间的数据通信已经足够了&#xff0c;而且CAN的技术成熟、稳定、成本低&#xff0c;因此CAN通信在汽车行业中得到了长期的应用。 随着汽车智能化的发展&#xff0c;汽…

redis之缓存淘汰策略

1.查看redis的最大占用内存 使用redis-cli命令连接redis服务端&#xff0c;输入命令&#xff1a;config get maxmemory 输出的值为0&#xff0c;0代表redis的最大占用内存等同于服务器的最大内存。 2.设置redis的最大占用内存 编辑redis的配置文件&#xff0c;并重启redis服务…

C++笔记12•面向对象之继承•

继承 1.继承的概念及定义 &#xff08;1&#xff09;概念&#xff1a; 继承 (inheritance) 机制是面向对象程序设计 使代码可以复用 的最重要的手段&#xff0c;它允许程序员在 保 持原有类特性的基础上进行扩展 &#xff0c;增加功能&#xff0c;这样产生新的类&#xff0c;称…

用Leangoo领歌敏捷工具进行迭代管理的实践分享Sprint Backlog

在敏捷开发中&#xff0c;迭代管理是确保项目持续推进、不断优化的重要环节。有效的迭代管理能够帮助团队快速响应变化&#xff0c;持续交付高质量产品。 Leangoo是一款免费的敏捷项目管理工具&#xff0c;为团队提供了直观、高效的看板管理方式来管理迭代过程。本文将探讨如何…

公园智能厕所引导大屏,清楚显示厕位有无人状态

在科技飞速发展的今天&#xff0c;公园的设施也在不断与时俱进。其中&#xff0c;公园智能厕所引导大屏的出现&#xff0c;为游客带来了全新的如厕体验。 走进公园的智能厕所区域&#xff0c;首先映入眼帘的便是那醒目的引导大屏。屏幕上清晰地显示着各个厕位的有无人状态&…

星闪NearLink短距无线连接技术

星闪NearLink短距无线连接技术&#xff0c;作为华为主导的新一代无线短距通信标准技术&#xff0c;自2020年起由中国工信部牵头制定标准&#xff0c;旨在为万物互联时代提供更高效、更稳定的连接方式。 类似技术介绍 AirDrop&#xff08;苹果&#xff09; AirDrop是苹果公司开发…

【STM32+HAL库】---- 通用定时器PWM输出实现呼吸灯

硬件开发板&#xff1a;STM32G0B1RET6 软件平台&#xff1a;cubemaxkeilVScode1 新建cubemax工程 1.1 配置系统时钟RCC 1.2 配置定时器 找到LED所对应的引脚PA5&#xff0c;选择TIM2_CH1模式 在TIM2中&#xff0c;时钟源选择内部时钟Internal Clock&#xff0c;通道1选择PWM…

NanoPC-T6安装redriod笔记

这里主要用于自己对安装过程的记录&#xff0c;中间可能记录比较粗糙。 重新编译内核 参考链接&#xff1a;【环境搭建】基于linux的NanoPC-T6_LTS系统固件编译环境搭建 基于docker构建编译环境 docker run -it \ --privilegedtrue --cap-addALL \ --name nanopc_t6_lts_en…

CRM系统为贷款中介行业插上科技的翅膀

CRM&#xff08;客户关系管理&#xff09;系统为贷款中介公司插上了科技的翅膀&#xff0c;极大提升了贷款中介企业的运营效率、客户管理能力和市场竞争力。鑫鹿贷款CRM系统基于互联网、大数据分析、人工智能、云计算等前沿技术&#xff0c;帮助贷款中介公司实现业务流程的自动…

对给定数组所对应的二叉树依次完成前序,中序,后序遍历,并输出遍历结果。

对给定数组所对应的二叉树依次完成前序&#xff0c;中序&#xff0c;后序遍历&#xff0c;并输出遍历结果。每行输入为一个二叉树&#xff0c;一维数组形式。其中-1表示Nil节点&#xff0c;例如&#xff1a;1,7,2,6,-1,4,8 构成的二叉树如下图所示&#xff1a; 结果以二维数组形…

pikachu文件包含漏洞靶场

File inclusion(local) 创建1.php 步骤一&#xff1a;选择一个球员提交 ../../../../1.php File Inclusion(remote)&#xff08;远程文件包含&#xff09; 步骤一&#xff1a;更改参数 php.ini ⾥有两个重要的参数 allow_url_fopen 、allow_url_include &#xff1b; 步骤二…

springboot集成guava布隆过滤器

1.创建springboot项目&#xff0c;引入maven依赖 <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>23.0</version></dependency>2.创建guava布隆过滤器 Component public class Gua…

浅析WebRTC技术在智慧园区视频管理场景中的应用

随着科技的飞速发展&#xff0c;智慧园区作为城市智慧化的重要组成部分&#xff0c;正逐步成为现代化管理的重要方向。智慧园区的建设不仅涉及硬件设施的智能化升级&#xff0c;还离不开高效的视频管理和实时通信技术。在这一背景下&#xff0c;WebRTC&#xff08;Web Real-Tim…

Ubuntu系统+宝塔面板部署Frp内网穿透服务

一、搭建目的 上次在局域网中搭建了自己的个人网盘之后&#xff0c;上传文件、照片都很方便&#xff0c;但是只能限制在内网中访问&#xff01;所以这次再搭建一个内网穿透服务器&#xff0c;这样不管在哪里都能访问到家里的云盘&#xff01; 二、内网穿透Frp是什么&#xff1…