做嵌入式开发,Linux应用好还是驱动好?
一、为何需要 double buffer?
single buffer会导致:
文章下方附学习资源,自助领取。屏幕撕裂(tearing),即在屏幕上同时看到多帧数据拼接在一起。
点击查看大图
single buffer为何会造成撕裂:
refresh rate和 frame rate不一致。
refresh rate表示的是屏幕每秒能更新多少次显示,例如 30hz / 60hz。
点击查看大图
frame rate表示的是 lcd controller / gpu每秒能绘制多少帧数据,例如 30fps / 60fps。
点击查看大图
LCD controller / gpu和屏幕协作完成一帧图像的显示:
点击查看大图
在 single buffer的场景下,LCD user和 LCD controller / gpu总是在共用同一个 framebuffer,且没有同步机制。
LCD user是写者,LCD controller / gpu是读者。
由于存在竞争关系且读写没有同步机制,framebuffer里必须会发生同时存在frame N和 frame N-1的数据,此时 LCD将 framebuffer的数据显示出来时,就会看到撕裂的效果:
点击查看大图
可以通过 double buffer+vsync解决撕裂的问题。
double buffer,顾名思义,就是有 2个 framebuffer,其工作逻辑如下:
·LCD controller : draw fb0 to screen
·LCD user : write data to fb1
·LCD controller : draw fb1 to screen
·LCD user : write data to fb0
·循环...
vsync机制则用于确保一帧图像能不被打断地显示在屏幕。
如何支持 double buffer?
需要驱动和应用互相配合:
二、编写支持 double buffer的 fbdev驱动
fbdev框图:
先梳理一下思路:
让驱动支持 double buffer需要做 3件事。
1.申请2 x buffer:
size = (2 * width * height);
fbi->screen_base = dma_alloc_wc(sfb->dev, size, &map_dma, GFP_KERNEL);