什么是游戏场景感知上一篇我们介绍了用 ArkTS 实现游戏场景感知这篇来看看用 C 怎么做。C 版本功能更强大可以获取更详细的设备信息。简单回顾一下游戏场景感知让游戏可以和系统对话。游戏告诉系统我现在在做什么系统告诉游戏设备现在状态怎么样。这样双方就能更好地配合既保证游戏流畅又不让手机过热。环境搭建硬件要求设备类型华为手机、平板和 PC/2in1HarmonyOS 系统HarmonyOS 5.0.2 Beta1 及以上软件要求DevEco Studio 版本DevEco Studio 5.0.2 Beta1 及以上HarmonyOS SDK 版本HarmonyOS 5.0.2 Beta1 SDK 及以上搭建步骤安装 DevEco Studio去华为开发者官网下载安装配置开发环境确保网络环境正常设备调试使用真机进行调试项目结构├── entry/src/main │ ├── cpp │ │ ├── CMakeLists.txt // 编译配置 │ │ ├── init.cpp // 初始化 │ │ ├── thermallevel_callback.cpp // 温度回调 │ │ ├── updategameinfo.cpp // 更新场景信息 │ │ ├── query_gpuinfo.cpp // 查询GPU信息 │ │ └── query_thermalinfo.cpp // 查询温度信息 │ ├── ets │ │ ├── entryability │ │ │ └── EntryAbility.ets │ │ └── pages │ │ └── Index.ets │ └── resources整体流程概览下面是 C 版本游戏场景感知的整体开发流程是否配置 CMakeLists.txt导入头文件初始化场景感知注册温度变化回调上报游戏场景信息查询 GPU 和温度信息是否需要取消?取消注册回调下面是温度变化回调的处理流程是否设备状态变化触发回调函数获取 GPU 信息获取温度信息温度是否过高?降低游戏画质保持当前画质释放内存资源第一步配置 CMakeLists.txttarget_include_directories(entry PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/include) target_link_directories(entry PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/lib/aarch64-linux-ohos) target_link_libraries(entry PUBLIC libgame_performance.z.so)链接game_performance动态库并设置头文件和库文件的搜索路径。第二步导入头文件#includeGameServiceKit/game_performance.h导入 Game Service Kit 的 C 头文件。第三步初始化场景感知int32_tInit(){// 创建初始化参数。GamePerformance_InitParameters*initParametersNULL;HMS_GamePerformance_CreateInitParameters(initParameters);// 设置应用版本。GamePerformance_ErrorCode appVersionSetCodeHMS_GamePerformance_InitParameters_SetAppVersion(initParameters,1.0);if(appVersionSetCode!GAME_PERFORMANCE_SUCCESS){// 错误处理}// 设置包名。HMS_GamePerformance_InitParameters_SetBundleName(initParameters,com.example.gameperformancedemoforc);// 初始化。GamePerformance_ErrorCode retHMS_GamePerformance_Init(initParameters);// 释放内存。HMS_GamePerformance_DestroyInitParameters(initParameters);int32_tcodestatic_castint32_t(ret);returncode;}初始化步骤创建参数对象HMS_GamePerformance_CreateInitParameters创建一个参数对象设置参数设置应用版本和包名初始化HMS_GamePerformance_Init执行初始化释放内存HMS_GamePerformance_DestroyInitParameters释放参数对象和 ArkTS 版本相比C 版本需要手动管理内存创建和销毁都要显式调用。第四步注册温度变化回调// 定义回调函数staticvoidonThermalLevelChanged(GamePerformance_DeviceInfo*deviceInfo,void*userData){(void)userData;// 获取 GPU 信息。GamePerformance_GpuInfo*gpuInfoNULL;HMS_GamePerformance_DeviceInfo_GetGpuInfo(deviceInfo,gpuInfo);int32_tgpuloadLevel-1;int32_tvertexLevel-1;int32_tfragmentLoadLevel-1;int32_tbandwidthLoadLevel-1;int32_ttextureLoadLevel-1;int32_tcurrentFrequency-1;HMS_GamePerformance_GpuInfo_GetGpuLoadLevel(gpuInfo,gpuloadLevel);HMS_GamePerformance_GpuInfo_GetVertexLoadLevel(gpuInfo,vertexLevel);HMS_GamePerformance_GpuInfo_GetFragmentLoadLevel(gpuInfo,fragmentLoadLevel);HMS_GamePerformance_GpuInfo_GetBandwidthLoadLevel(gpuInfo,bandwidthLoadLevel);HMS_GamePerformance_GpuInfo_GetTextureLoadLevel(gpuInfo,textureLoadLevel);HMS_GamePerformance_GpuInfo_GetCurrentFrequency(gpuInfo,currentFrequency);回调函数会在设备状态变化时被调用。这里获取了 GPU 的详细信息gpuloadLevelGPU 整体负载等级vertexLevel顶点处理负载fragmentLoadLevel片元处理负载bandwidthLoadLevel带宽负载textureLoadLevel纹理负载currentFrequency当前频率// 获取温度信息。GamePerformance_ThermalInfo*thermalInfoNULL;HMS_GamePerformance_DeviceInfo_GetThermalInfo(deviceInfo,thermalInfo);int32_tmarginINT32_MIN;int32_ttrendINT32_MIN;int32_tlevel-1;HMS_GamePerformance_ThermalInfo_GetThermalMargin(thermalInfo,margin);HMS_GamePerformance_ThermalInfo_GetThermalTrend(thermalInfo,trend);HMS_GamePerformance_ThermalInfo_GetThermalLevel(thermalInfo,level);// 释放内存。HMS_GamePerformance_DestroyGpuInfo(gpuInfo);HMS_GamePerformance_DestroyThermalInfo(thermalInfo);}获取温度信息margin温度余量表示距离温度上限还有多少trend温度趋势表示温度在升高还是降低level温度等级int32_tRegisterThermalLevelChangedCallback(){// 订阅设备信息类型。size_t size2;GamePerformance_DeviceInfoType*types[]{newGamePerformance_DeviceInfoType(GAME_PERFORMANCE_DEVICEINFO_TYPE_GPU),newGamePerformance_DeviceInfoType(GAME_PERFORMANCE_DEVICEINFO_TYPE_THERMAL)};void*userData(void*)mydata;GamePerformance_ErrorCode retHMS_GamePerformance_RegisterThermalLevelChangedCallback(types,size,onThermalLevelChanged,userData);int32_tcodestatic_castint32_t(ret);returncode;}注册回调时指定要订阅的设备信息类型GAME_PERFORMANCE_DEVICEINFO_TYPE_GPUGPU 信息GAME_PERFORMANCE_DEVICEINFO_TYPE_THERMAL温度信息第五步取消注册温度变化回调int32_tUnregisterThermalLevelChangedCallback(){GamePerformance_ErrorCode retHMS_GamePerformance_UnregisterThermalLevelChangedCallback(onThermalLevelChanged);int32_tcodestatic_castint32_t(ret);returncode;}int32_tUnregisterAllThermalLevelChangedCallbacks(){GamePerformance_ErrorCode retHMS_GamePerformance_UnregisterAllThermalLevelChangedCallbacks();int32_tcodestatic_castint32_t(ret);returncode;}有两种取消方式UnregisterThermalLevelChangedCallback取消指定的回调UnregisterAllThermalLevelChangedCallbacks取消所有回调第六步上报游戏场景信息int32_tUpdateSceneInfo(){GamePerformance_SceneInfo*sceneInfoNULL;HMS_GamePerformance_CreateSceneInfo(sceneInfo);// 设置必填字段。HMS_GamePerformance_SceneInfo_SetSceneID(sceneInfo,1);HMS_GamePerformance_SceneInfo_SetImportanceLevel(sceneInfo,GAME_PERFORMANCE_SIL_LEVEL1);// 设置可选字段。HMS_GamePerformance_SceneInfo_SetDescription(sceneInfo,this is description of scene);HMS_GamePerformance_SceneInfo_SetSubSceneID(sceneInfo,20101020304);HMS_GamePerformance_SceneInfo_SetSubDescription(sceneInfo,this is description of subscene);HMS_GamePerformance_SceneInfo_SetSceneFrequency(sceneInfo,2);HMS_GamePerformance_SceneInfo_SetSceneTime(sceneInfo,15);创建场景信息对象设置各种参数SceneID场景 IDImportanceLevel重要性等级Description场景描述SubSceneID子场景 IDSceneFrequency场景刷新频率SceneTime场景持续时间// 设置推荐的硬件等级。HMS_GamePerformance_SceneInfo_SetRecommendedCpuLevel(sceneInfo,GAME_PERFORMANCE_CPU_LEVEL_HIGH);HMS_GamePerformance_SceneInfo_SetRecommendedGpuLevel(sceneInfo,GAME_PERFORMANCE_GPU_LEVEL_HIGH);HMS_GamePerformance_SceneInfo_SetRecommendedDdrLevel(sceneInfo,GAME_PERFORMANCE_DDR_LEVEL_HIGH);设置推荐的硬件等级告诉系统这个场景需要什么样的硬件资源。// 设置渲染信息。HMS_GamePerformance_SceneInfo_SetKeyThread(sceneInfo,render);HMS_GamePerformance_SceneInfo_SetDrawCallCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetVertexCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetTriangleCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetShaderCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetTextureCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetMeshCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetChannelCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetParticipantCount(sceneInfo,5);// 上报场景信息。GamePerformance_ErrorCode retHMS_GamePerformance_UpdateSceneInfo(sceneInfo);// 释放内存。HMS_GamePerformance_DestroySceneInfo(sceneInfo);int32_tcodestatic_castint32_t(ret);returncode;}设置渲染相关的详细信息这些信息可以帮助系统更好地理解游戏的负载情况。第七步查询 GPU 信息int32_tQueryGpuInfo(){GamePerformance_GpuInfo*gpuInfoNULL;GamePerformance_ErrorCode retHMS_GamePerformance_QueryGpuInfo(gpuInfo);int32_tgpuloadLevel-1;int32_tbandwidth-1;int32_tcurrentFrequency-1;int32_tfragmentLoadLevel-1;int32_ttextureLoadLevel-1;int32_tvertexLoadLevel-1;HMS_GamePerformance_GpuInfo_GetGpuLoadLevel(gpuInfo,gpuloadLevel);HMS_GamePerformance_GpuInfo_GetBandwidthLoadLevel(gpuInfo,bandwidth);HMS_GamePerformance_GpuInfo_GetCurrentFrequency(gpuInfo,currentFrequency);HMS_GamePerformance_GpuInfo_GetFragmentLoadLevel(gpuInfo,fragmentLoadLevel);HMS_GamePerformance_GpuInfo_GetTextureLoadLevel(gpuInfo,textureLoadLevel);HMS_GamePerformance_GpuInfo_GetVertexLoadLevel(gpuInfo,vertexLoadLevel);HMS_GamePerformance_DestroyGpuInfo(gpuInfo);int32_tcodestatic_castint32_t(ret);returncode;}主动查询 GPU 信息获取当前的负载和频率状态。第八步查询温度信息int32_tQueryThermalInfo(){GamePerformance_ThermalInfo*thermalInfoNULL;GamePerformance_ThermalInfoQueryParameters*parametersNULL;// 创建查询参数。HMS_GamePerformance_CreateThermalInfoQueryParameters(parameters);// 设置是否需要预测温度趋势。HMS_GamePerformance_ThermalInfoQueryParameters_SetNeedsPrediction(parameters,true);// 设置目标温度等级。HMS_GamePerformance_ThermalInfoQueryParameters_SetTargetThermalLevel(parameters,4);// 查询温度信息。GamePerformance_ErrorCode retHMS_GamePerformance_QueryThermalInfo(parameters,thermalInfo);int32_tmarginINT32_MIN;int32_ttrendINT32_MIN;int32_tlevel-1;HMS_GamePerformance_ThermalInfo_GetThermalLevel(thermalInfo,level);HMS_GamePerformance_ThermalInfo_GetThermalMargin(thermalInfo,margin);HMS_GamePerformance_ThermalInfo_GetThermalTrend(thermalInfo,trend);// 释放内存。HMS_GamePerformance_DestroyThermalInfo(thermalInfo);HMS_GamePerformance_DestroyThermalInfoQueryParameters(parameters);int32_tcodestatic_castint32_t(ret);returncode;}查询温度信息时可以设置预测参数SetNeedsPrediction是否需要预测温度趋势SetTargetThermalLevel目标温度等级用于预测C 版和 ArkTS 版的区别方面C 版ArkTS 版信息详细程度更详细有 GPU 负载、顶点/片元负载等较简单主要是温度等级内存管理手动管理需要创建和销毁对象自动管理开发难度较高较低性能更好一般使用场景复杂游戏、性能要求高简单游戏、快速开发适用场景游戏场景感知特别适合以下场景大型 3D 游戏需要详细的 GPU 信息来优化渲染竞技游戏对帧率要求高需要实时调整性能长时间游戏需要防止手机过热跨平台游戏需要适配不同设备的性能注意事项内存管理C 版本需要手动管理内存创建的对象都要销毁回调函数回调函数要轻量不要做耗时操作订阅类型注册回调时要指定订阅的设备信息类型错误处理每个接口调用都要检查返回值线程安全回调可能在非主线程调用要注意线程安全总结C 版本的游戏场景感知功能更强大核心流程配置 CMakeLists.txt链接 game_performance 库初始化场景感知服务注册温度变化回调监听设备状态上报游戏场景信息让系统优化资源分配主动查询 GPU 和温度信息不需要时取消注册回调如果你的游戏对性能要求很高需要详细的设备信息来优化C 版本是更好的选择。
HarmonyOS 游戏场景感知:用 C++ 实现游戏APP与系统交互
发布时间:2026/6/8 8:13:06
什么是游戏场景感知上一篇我们介绍了用 ArkTS 实现游戏场景感知这篇来看看用 C 怎么做。C 版本功能更强大可以获取更详细的设备信息。简单回顾一下游戏场景感知让游戏可以和系统对话。游戏告诉系统我现在在做什么系统告诉游戏设备现在状态怎么样。这样双方就能更好地配合既保证游戏流畅又不让手机过热。环境搭建硬件要求设备类型华为手机、平板和 PC/2in1HarmonyOS 系统HarmonyOS 5.0.2 Beta1 及以上软件要求DevEco Studio 版本DevEco Studio 5.0.2 Beta1 及以上HarmonyOS SDK 版本HarmonyOS 5.0.2 Beta1 SDK 及以上搭建步骤安装 DevEco Studio去华为开发者官网下载安装配置开发环境确保网络环境正常设备调试使用真机进行调试项目结构├── entry/src/main │ ├── cpp │ │ ├── CMakeLists.txt // 编译配置 │ │ ├── init.cpp // 初始化 │ │ ├── thermallevel_callback.cpp // 温度回调 │ │ ├── updategameinfo.cpp // 更新场景信息 │ │ ├── query_gpuinfo.cpp // 查询GPU信息 │ │ └── query_thermalinfo.cpp // 查询温度信息 │ ├── ets │ │ ├── entryability │ │ │ └── EntryAbility.ets │ │ └── pages │ │ └── Index.ets │ └── resources整体流程概览下面是 C 版本游戏场景感知的整体开发流程是否配置 CMakeLists.txt导入头文件初始化场景感知注册温度变化回调上报游戏场景信息查询 GPU 和温度信息是否需要取消?取消注册回调下面是温度变化回调的处理流程是否设备状态变化触发回调函数获取 GPU 信息获取温度信息温度是否过高?降低游戏画质保持当前画质释放内存资源第一步配置 CMakeLists.txttarget_include_directories(entry PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/include) target_link_directories(entry PUBLIC ${HMOS_SDK_NATIVE}/sysroot/usr/lib/aarch64-linux-ohos) target_link_libraries(entry PUBLIC libgame_performance.z.so)链接game_performance动态库并设置头文件和库文件的搜索路径。第二步导入头文件#includeGameServiceKit/game_performance.h导入 Game Service Kit 的 C 头文件。第三步初始化场景感知int32_tInit(){// 创建初始化参数。GamePerformance_InitParameters*initParametersNULL;HMS_GamePerformance_CreateInitParameters(initParameters);// 设置应用版本。GamePerformance_ErrorCode appVersionSetCodeHMS_GamePerformance_InitParameters_SetAppVersion(initParameters,1.0);if(appVersionSetCode!GAME_PERFORMANCE_SUCCESS){// 错误处理}// 设置包名。HMS_GamePerformance_InitParameters_SetBundleName(initParameters,com.example.gameperformancedemoforc);// 初始化。GamePerformance_ErrorCode retHMS_GamePerformance_Init(initParameters);// 释放内存。HMS_GamePerformance_DestroyInitParameters(initParameters);int32_tcodestatic_castint32_t(ret);returncode;}初始化步骤创建参数对象HMS_GamePerformance_CreateInitParameters创建一个参数对象设置参数设置应用版本和包名初始化HMS_GamePerformance_Init执行初始化释放内存HMS_GamePerformance_DestroyInitParameters释放参数对象和 ArkTS 版本相比C 版本需要手动管理内存创建和销毁都要显式调用。第四步注册温度变化回调// 定义回调函数staticvoidonThermalLevelChanged(GamePerformance_DeviceInfo*deviceInfo,void*userData){(void)userData;// 获取 GPU 信息。GamePerformance_GpuInfo*gpuInfoNULL;HMS_GamePerformance_DeviceInfo_GetGpuInfo(deviceInfo,gpuInfo);int32_tgpuloadLevel-1;int32_tvertexLevel-1;int32_tfragmentLoadLevel-1;int32_tbandwidthLoadLevel-1;int32_ttextureLoadLevel-1;int32_tcurrentFrequency-1;HMS_GamePerformance_GpuInfo_GetGpuLoadLevel(gpuInfo,gpuloadLevel);HMS_GamePerformance_GpuInfo_GetVertexLoadLevel(gpuInfo,vertexLevel);HMS_GamePerformance_GpuInfo_GetFragmentLoadLevel(gpuInfo,fragmentLoadLevel);HMS_GamePerformance_GpuInfo_GetBandwidthLoadLevel(gpuInfo,bandwidthLoadLevel);HMS_GamePerformance_GpuInfo_GetTextureLoadLevel(gpuInfo,textureLoadLevel);HMS_GamePerformance_GpuInfo_GetCurrentFrequency(gpuInfo,currentFrequency);回调函数会在设备状态变化时被调用。这里获取了 GPU 的详细信息gpuloadLevelGPU 整体负载等级vertexLevel顶点处理负载fragmentLoadLevel片元处理负载bandwidthLoadLevel带宽负载textureLoadLevel纹理负载currentFrequency当前频率// 获取温度信息。GamePerformance_ThermalInfo*thermalInfoNULL;HMS_GamePerformance_DeviceInfo_GetThermalInfo(deviceInfo,thermalInfo);int32_tmarginINT32_MIN;int32_ttrendINT32_MIN;int32_tlevel-1;HMS_GamePerformance_ThermalInfo_GetThermalMargin(thermalInfo,margin);HMS_GamePerformance_ThermalInfo_GetThermalTrend(thermalInfo,trend);HMS_GamePerformance_ThermalInfo_GetThermalLevel(thermalInfo,level);// 释放内存。HMS_GamePerformance_DestroyGpuInfo(gpuInfo);HMS_GamePerformance_DestroyThermalInfo(thermalInfo);}获取温度信息margin温度余量表示距离温度上限还有多少trend温度趋势表示温度在升高还是降低level温度等级int32_tRegisterThermalLevelChangedCallback(){// 订阅设备信息类型。size_t size2;GamePerformance_DeviceInfoType*types[]{newGamePerformance_DeviceInfoType(GAME_PERFORMANCE_DEVICEINFO_TYPE_GPU),newGamePerformance_DeviceInfoType(GAME_PERFORMANCE_DEVICEINFO_TYPE_THERMAL)};void*userData(void*)mydata;GamePerformance_ErrorCode retHMS_GamePerformance_RegisterThermalLevelChangedCallback(types,size,onThermalLevelChanged,userData);int32_tcodestatic_castint32_t(ret);returncode;}注册回调时指定要订阅的设备信息类型GAME_PERFORMANCE_DEVICEINFO_TYPE_GPUGPU 信息GAME_PERFORMANCE_DEVICEINFO_TYPE_THERMAL温度信息第五步取消注册温度变化回调int32_tUnregisterThermalLevelChangedCallback(){GamePerformance_ErrorCode retHMS_GamePerformance_UnregisterThermalLevelChangedCallback(onThermalLevelChanged);int32_tcodestatic_castint32_t(ret);returncode;}int32_tUnregisterAllThermalLevelChangedCallbacks(){GamePerformance_ErrorCode retHMS_GamePerformance_UnregisterAllThermalLevelChangedCallbacks();int32_tcodestatic_castint32_t(ret);returncode;}有两种取消方式UnregisterThermalLevelChangedCallback取消指定的回调UnregisterAllThermalLevelChangedCallbacks取消所有回调第六步上报游戏场景信息int32_tUpdateSceneInfo(){GamePerformance_SceneInfo*sceneInfoNULL;HMS_GamePerformance_CreateSceneInfo(sceneInfo);// 设置必填字段。HMS_GamePerformance_SceneInfo_SetSceneID(sceneInfo,1);HMS_GamePerformance_SceneInfo_SetImportanceLevel(sceneInfo,GAME_PERFORMANCE_SIL_LEVEL1);// 设置可选字段。HMS_GamePerformance_SceneInfo_SetDescription(sceneInfo,this is description of scene);HMS_GamePerformance_SceneInfo_SetSubSceneID(sceneInfo,20101020304);HMS_GamePerformance_SceneInfo_SetSubDescription(sceneInfo,this is description of subscene);HMS_GamePerformance_SceneInfo_SetSceneFrequency(sceneInfo,2);HMS_GamePerformance_SceneInfo_SetSceneTime(sceneInfo,15);创建场景信息对象设置各种参数SceneID场景 IDImportanceLevel重要性等级Description场景描述SubSceneID子场景 IDSceneFrequency场景刷新频率SceneTime场景持续时间// 设置推荐的硬件等级。HMS_GamePerformance_SceneInfo_SetRecommendedCpuLevel(sceneInfo,GAME_PERFORMANCE_CPU_LEVEL_HIGH);HMS_GamePerformance_SceneInfo_SetRecommendedGpuLevel(sceneInfo,GAME_PERFORMANCE_GPU_LEVEL_HIGH);HMS_GamePerformance_SceneInfo_SetRecommendedDdrLevel(sceneInfo,GAME_PERFORMANCE_DDR_LEVEL_HIGH);设置推荐的硬件等级告诉系统这个场景需要什么样的硬件资源。// 设置渲染信息。HMS_GamePerformance_SceneInfo_SetKeyThread(sceneInfo,render);HMS_GamePerformance_SceneInfo_SetDrawCallCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetVertexCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetTriangleCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetShaderCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetTextureCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetMeshCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetChannelCount(sceneInfo,100);HMS_GamePerformance_SceneInfo_SetParticipantCount(sceneInfo,5);// 上报场景信息。GamePerformance_ErrorCode retHMS_GamePerformance_UpdateSceneInfo(sceneInfo);// 释放内存。HMS_GamePerformance_DestroySceneInfo(sceneInfo);int32_tcodestatic_castint32_t(ret);returncode;}设置渲染相关的详细信息这些信息可以帮助系统更好地理解游戏的负载情况。第七步查询 GPU 信息int32_tQueryGpuInfo(){GamePerformance_GpuInfo*gpuInfoNULL;GamePerformance_ErrorCode retHMS_GamePerformance_QueryGpuInfo(gpuInfo);int32_tgpuloadLevel-1;int32_tbandwidth-1;int32_tcurrentFrequency-1;int32_tfragmentLoadLevel-1;int32_ttextureLoadLevel-1;int32_tvertexLoadLevel-1;HMS_GamePerformance_GpuInfo_GetGpuLoadLevel(gpuInfo,gpuloadLevel);HMS_GamePerformance_GpuInfo_GetBandwidthLoadLevel(gpuInfo,bandwidth);HMS_GamePerformance_GpuInfo_GetCurrentFrequency(gpuInfo,currentFrequency);HMS_GamePerformance_GpuInfo_GetFragmentLoadLevel(gpuInfo,fragmentLoadLevel);HMS_GamePerformance_GpuInfo_GetTextureLoadLevel(gpuInfo,textureLoadLevel);HMS_GamePerformance_GpuInfo_GetVertexLoadLevel(gpuInfo,vertexLoadLevel);HMS_GamePerformance_DestroyGpuInfo(gpuInfo);int32_tcodestatic_castint32_t(ret);returncode;}主动查询 GPU 信息获取当前的负载和频率状态。第八步查询温度信息int32_tQueryThermalInfo(){GamePerformance_ThermalInfo*thermalInfoNULL;GamePerformance_ThermalInfoQueryParameters*parametersNULL;// 创建查询参数。HMS_GamePerformance_CreateThermalInfoQueryParameters(parameters);// 设置是否需要预测温度趋势。HMS_GamePerformance_ThermalInfoQueryParameters_SetNeedsPrediction(parameters,true);// 设置目标温度等级。HMS_GamePerformance_ThermalInfoQueryParameters_SetTargetThermalLevel(parameters,4);// 查询温度信息。GamePerformance_ErrorCode retHMS_GamePerformance_QueryThermalInfo(parameters,thermalInfo);int32_tmarginINT32_MIN;int32_ttrendINT32_MIN;int32_tlevel-1;HMS_GamePerformance_ThermalInfo_GetThermalLevel(thermalInfo,level);HMS_GamePerformance_ThermalInfo_GetThermalMargin(thermalInfo,margin);HMS_GamePerformance_ThermalInfo_GetThermalTrend(thermalInfo,trend);// 释放内存。HMS_GamePerformance_DestroyThermalInfo(thermalInfo);HMS_GamePerformance_DestroyThermalInfoQueryParameters(parameters);int32_tcodestatic_castint32_t(ret);returncode;}查询温度信息时可以设置预测参数SetNeedsPrediction是否需要预测温度趋势SetTargetThermalLevel目标温度等级用于预测C 版和 ArkTS 版的区别方面C 版ArkTS 版信息详细程度更详细有 GPU 负载、顶点/片元负载等较简单主要是温度等级内存管理手动管理需要创建和销毁对象自动管理开发难度较高较低性能更好一般使用场景复杂游戏、性能要求高简单游戏、快速开发适用场景游戏场景感知特别适合以下场景大型 3D 游戏需要详细的 GPU 信息来优化渲染竞技游戏对帧率要求高需要实时调整性能长时间游戏需要防止手机过热跨平台游戏需要适配不同设备的性能注意事项内存管理C 版本需要手动管理内存创建的对象都要销毁回调函数回调函数要轻量不要做耗时操作订阅类型注册回调时要指定订阅的设备信息类型错误处理每个接口调用都要检查返回值线程安全回调可能在非主线程调用要注意线程安全总结C 版本的游戏场景感知功能更强大核心流程配置 CMakeLists.txt链接 game_performance 库初始化场景感知服务注册温度变化回调监听设备状态上报游戏场景信息让系统优化资源分配主动查询 GPU 和温度信息不需要时取消注册回调如果你的游戏对性能要求很高需要详细的设备信息来优化C 版本是更好的选择。