我有一个使用 XCB 和 openGL 的应用程序。一开始,我选择了具有以下属性的帧缓冲区配置:
const int attributes[] = {GLX_BUFFER_SIZE, 32, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, True, GLX_RENDER_TYPE, GLX_RGBA_BIT, None};
fb_configs = glXChooseFBConfig(display, screen_index, attributes, &fb_configs_count);
我运行了一个简单的动画,它应该持续一个固定的持续时间(1 秒),但在屏幕上显示它需要更长的时间(大约 5 秒)。添加日志以显示进度值后,我发现实际循环仅持续 1s。
struct timeval start; // start time of the animation
gettimeofday(&start, 0);
while (1)
{
double progress = timer_progress(&start);
if (progress > 1.0)
break; // end the animation
draw(progress);
glXSwapBuffers(display, drawable);
xcb_generic_event_t *event = xcb_poll_for_event(connection);
if (!event)
{
usleep(1000);
continue;
}
switch (event->response_type & ~0x80)
{
case XCB_EXPOSE:
default:
free(event);
continue;
}
}
我不确定到底发生了什么。我想在每次迭代中都会glXSwapBuffers()
将用于绘图的opengl命令排入队列,并且在循环结束时它们中的大多数尚未执行。
调整 的参数usleep()
除了使动画不那么平滑或使动画更慢之外没有任何效果。当我切换到单缓冲时,问题就消失了(但我遇到了与单缓冲相关的问题)。
看来我做的不对,但我不知道是什么。