66 lines
2.4 KiB
C
66 lines
2.4 KiB
C
// ge_fps.c
|
||
#include "ge_fps.h"
|
||
|
||
// 初始化FPS控制器
|
||
void ge_fps_init(ge_fps_controller_t* fps_ctrl, ge_u32_t target_fps,
|
||
ge_fps_get_us_func_t get_us, ge_fps_sleep_us_func_t sleep_us) {
|
||
fps_ctrl->target_fps = target_fps;
|
||
fps_ctrl->frame_duration = 1000 / target_fps;
|
||
fps_ctrl->last_frame_time = get_us();
|
||
fps_ctrl->frame_time = 0;
|
||
fps_ctrl->sleep_time = 0;
|
||
fps_ctrl->frame_count = 0;
|
||
fps_ctrl->fps = 0;
|
||
fps_ctrl->last_fps_time = fps_ctrl->last_frame_time;
|
||
fps_ctrl->call_get_us = get_us;
|
||
fps_ctrl->call_sleep_us = sleep_us;
|
||
}
|
||
|
||
// 帧开始
|
||
void ge_fps_begin_frame(ge_fps_controller_t* fps_ctrl) {
|
||
fps_ctrl->last_frame_time = fps_ctrl->call_get_us();
|
||
}
|
||
|
||
// 帧结束
|
||
void ge_fps_end_frame(ge_fps_controller_t* fps_ctrl) {
|
||
ge_u32_t current_time = fps_ctrl->call_get_us();
|
||
fps_ctrl->frame_time = current_time - fps_ctrl->last_frame_time;
|
||
|
||
// 帧率自适应算法
|
||
if (fps_ctrl->frame_time < fps_ctrl->frame_duration) {
|
||
ge_u32_t sleep_time = fps_ctrl->frame_duration - fps_ctrl->frame_time;
|
||
|
||
// 动态调整休眠精度
|
||
if (sleep_time > 2000) { // >2ms使用Sleep
|
||
fps_ctrl->call_sleep_us(sleep_time - 1000); // 提前1ms唤醒
|
||
current_time = fps_ctrl->call_get_us();
|
||
|
||
// 剩余时间忙等待
|
||
while ((current_time - fps_ctrl->last_frame_time) < fps_ctrl->frame_duration) {
|
||
current_time = fps_ctrl->call_get_us();
|
||
}
|
||
} else {
|
||
// 短时间直接忙等待
|
||
while ((fps_ctrl->call_get_us() - fps_ctrl->last_frame_time) < fps_ctrl->frame_duration);
|
||
}
|
||
}
|
||
|
||
// 更新FPS计数(同原逻辑)
|
||
fps_ctrl->frame_count++;
|
||
if (current_time - fps_ctrl->last_fps_time >= 1000000) {
|
||
fps_ctrl->fps = fps_ctrl->frame_count;
|
||
fps_ctrl->frame_count = 0;
|
||
fps_ctrl->last_fps_time = current_time;
|
||
|
||
// 动态调整目标帧时间(±5%容差)
|
||
// ge_u32_t avg_frame_time = 1000000 / fps_ctrl->fps;
|
||
// if (avg_frame_time > fps_ctrl->frame_duration * 1.05f) {
|
||
// fps_ctrl->frame_duration = avg_frame_time;
|
||
// } else if (avg_frame_time < fps_ctrl->frame_duration * 0.95f) {
|
||
// fps_ctrl->frame_duration = GE_MAX(
|
||
// fps_ctrl->target_fps * 1000,
|
||
// avg_frame_time
|
||
// );
|
||
// }
|
||
}
|
||
} |