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

本文主要是介绍鸿蒙(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

这篇关于鸿蒙(API 12 Beta6版)图形【NativeDisplaySoloist开发指导】方舟2D图形服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134517

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

如何搭建并配置HTTPD文件服务及访问权限控制

《如何搭建并配置HTTPD文件服务及访问权限控制》:本文主要介绍如何搭建并配置HTTPD文件服务及访问权限控制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、安装HTTPD服务二、HTTPD服务目录结构三、配置修改四、服务启动五、基于用户访问权限控制六、

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示