在控制台(帧缓冲区)中绘制字符的最快方法?
在基于帧缓冲区的控制台中渲染字符的最快方法是什么?我使用的是 XNU 发行版中的 iso_font.h
字体。
现在我正在使用这段代码来渲染一个字符,但它似乎不太有效:
px = px* ISO_CHAR_WIDTH;
py = py* (ISO_CHAR_HEIGHT-1);
for (int i = 0; i < 15; i += 1)
{
int sym = iso_font[c*16+i];
int x = px;
int y = py + i;
for (int ii =0; ii < 8; ii++)
{
x+=1;
if ((sym & (1 << ii)))
{
fb_set_px(x,y,fg);
}
else
{
fb_set_px(x,y,bg);
}
}
}
而且我也想知道这段代码是否可以简化:
void fb_set_px(x,y,hex){
void*ptr = ((_base + (_bpr*y) + (_bpe*x)));
unsigned int *p = (unsigned int *) ptr;
*p=hex;
}
它在行太多的情况下还不错我需要重新绘制整个控制台(滚动),此时会出现明显的延迟。
What would be the fastest way to render characters in a framebuffer based console? I'm using the iso_font.h
font from the XNU distribution.
Right now I'm using this code to render a character, but it doesn't seem to be too efficient:
px = px* ISO_CHAR_WIDTH;
py = py* (ISO_CHAR_HEIGHT-1);
for (int i = 0; i < 15; i += 1)
{
int sym = iso_font[c*16+i];
int x = px;
int y = py + i;
for (int ii =0; ii < 8; ii++)
{
x+=1;
if ((sym & (1 << ii)))
{
fb_set_px(x,y,fg);
}
else
{
fb_set_px(x,y,bg);
}
}
}
And I'm also wondering if this code could be simpliefied:
void fb_set_px(x,y,hex){
void*ptr = ((_base + (_bpr*y) + (_bpe*x)));
unsigned int *p = (unsigned int *) ptr;
*p=hex;
}
It is decent up to the point where there are too many lines and I need to redraw the whole console (to scroll) at which point there is a significant delay.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到了一些事情(这让我回到了旧的 DOS 时代!):-
1)使用增量寻址来写入像素:
2)消除内循环中的 if:
3)使用操作系统的任何帮助。例如,这可能是硬件加速。
4)滚动时,不要重新绘制所有字符,只需移动屏幕上剩余的部分即可。例如,从第 1 行到第 0 行执行 memmove,行数为 - 1。然后清除暴露区域。
A few things that come to mind (this takes me back to the old DOS days!):-
1) Use incremental addressing for writing pixels:
2) Eliminate the if in the inner loop:
3) Use any help from the OS. This might be hardware acceleration for example.
4) When scrolling, don't redraw all the characters, just memmove the portion of the screen that will remain. e.g. do a memmove from line 1 to line 0 for number of lines - 1. Then clear the exposed area.
通常,大多数硬件帧缓冲区(例如 VGA 帧缓冲区)都具有硬件滚动功能。不仅如此,一些帧缓冲区(不幸的是不是 VGA 字符控制台)将“环绕”,这意味着当您写入帧缓冲区的最后一个字节(或位),然后再次写入第一个字节时帧缓冲区的字节(或位),这些字节将作为屏幕上的下一行出现在硬件中。因此,您可以执行以下操作:
long
而不是char
。接下来,就您的访问函数
fb_set_px
而言,我会 1) 使其在头文件中内联,以便避免需要使用的实际函数调用的开销堆栈来设置激活帧,2)您可以使用帧缓冲区只是一个内存数组这一事实来实际将指针数组映射到表示每个字符的某种struct
类型的数据布局frame_buffer(即,某些帧缓冲区有一个字节用于字符,另一个字节用于某些类型的属性,如应用于字符的颜色等)。例如,您可以执行以下操作:现在您可以简单地将帧缓冲区中的 ax,y 位置寻址为
Typically most hardware frame-buffers, such as the VGA frame buffer, have a hardware scrolling capability. Not only that, but some frame-buffers (not the VGA-character console unfortunately) will "wrap-around", meaning when you write to the last byte (or bits) of the frame-buffer, and then write again to the first bytes (or bits) of the frame-buffer, those bytes will appear in the hardware as the next line on the screen. So there are a couple things you can do:
memcpy()
to copy the last page in memory into the first page of the frame-buffer in memory ... Once you've done that step, keep up the same page-scrolling routine where you clear the next line, and then increment the starting line pointer of the frame-buffer to give the illusion of scrolling up a single line. The page-copy will be a little slower than all the other hardware page-scrolls used up to that point, butmemcpy()
is very efficient, especially if you copy multiple bytes at a time using a larger memory-type like along
rather than achar
.Next, as far as your access function
fb_set_px
goes, I would 1) make itinline
inside a header file so that you avoid the overhead of an actual function call that requires using the stack to setup an activation frame, and 2) you could use the fact that your frame-buffer is just a memory array to actually map an array of pointers to somestruct
type that represents the per-character data layout of to your frame_buffer (i.e., some frame-buffers have a byte for a character and another byte for some type of attribute like color, etc. applied to the character). So for instance, you could do the following:Now you can simply address a x,y position in your frame-buffer as