在控制台(帧缓冲区)中绘制字符的最快方法?

发布于 2024-11-19 21:20:24 字数 796 浏览 1 评论 0原文

在基于帧缓冲区的控制台中渲染字符的最快方法是什么?我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-26 21:20:25

我想到了一些事情(这让我回到了旧的 DOS 时代!):-

1)使用增量寻址来写入像素:

  p = calculate address of x,y
    for line = 0 to 15
      for column = 0 to 7
        write to p
        increment p
      end
      p += stride - 8 (stride = distance in memory between vertically adjacent pixels)
    end

2)消除内循环中的 if:

 draw pixel (fg + (bg - fg) & (((sym >> column) & 1) - 1)

3)使用操作系统的任何帮助。例如,这可能是硬件加速。

4)滚动时,不要重新绘制所有字符,只需移动屏幕上剩余的部分即可。例如,从第 1 行到第 0 行执行 memmove,行数为 - 1。然后清除暴露区域。

|-------|                             |-------|                      |-------|
|.......|                             |@@@@@@@|                      |@@@@@@@|
|@@@@@@@| scroll up a line => memmove |#######| then clear => memset |#######|
|#######|                             |#######|                      |       |
|-------|                             |-------|                      |-------|

A few things that come to mind (this takes me back to the old DOS days!):-

1) Use incremental addressing for writing pixels:

  p = calculate address of x,y
    for line = 0 to 15
      for column = 0 to 7
        write to p
        increment p
      end
      p += stride - 8 (stride = distance in memory between vertically adjacent pixels)
    end

2) Eliminate the if in the inner loop:

 draw pixel (fg + (bg - fg) & (((sym >> column) & 1) - 1)

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.

|-------|                             |-------|                      |-------|
|.......|                             |@@@@@@@|                      |@@@@@@@|
|@@@@@@@| scroll up a line => memmove |#######| then clear => memset |#######|
|#######|                             |#######|                      |       |
|-------|                             |-------|                      |-------|
仅此而已 2024-11-26 21:20:25

通常,大多数硬件帧缓冲区(例如 VGA 帧缓冲区)都具有硬件滚动功能。不仅如此,一些帧缓冲区(不幸的是不是 VGA 字符控制台)将“环绕”,这意味着当您写入帧缓冲区的最后一个字节(或位),然后再次写入第一个字节时帧缓冲区的字节(或位),这些字节将作为屏幕上的下一行出现在硬件中。因此,您可以执行以下操作:

  1. 完成后,使用帧缓冲区的硬件滚动功能滚动到下一行。这意味着您将首先清除帧缓冲区的下一行,在该行上写入所需的字符,然后将帧缓冲区起始行指针加一。然后,这将“显示”为好像帧缓冲区已向下滚动一行。当到达帧缓冲区内存的末尾时,保持相同的过程继续,但您要清除的下一行将是帧缓冲区内存中的第一行。然后,您将继续增加帧缓冲区的起始地址(这仍然指向帧缓冲区内存末尾附近的某个位置),直到它也指向帧缓冲区内存中的最后一行,此时然后你将回到内存中的第一行。
  2. 如果您的帧缓冲区仍然是多页的,但没有环绕功能(如 VGA 字符控制台),则保留指向帧缓冲区开头的指针,当您到达最后一个“页面”时“在内存中,使用memcpy()将内存中的最后一页复制到内存中帧缓冲区的第一页中......完成该步骤后,保持相同的页面-滚动例程,您可以清除下一行,然后增加帧缓冲区的起始行指针以产生向上滚动一行的错觉。页面复制将比迄今为止使用的所有其他硬件页面滚动慢一点,但是 memcpy() 非常高效,特别是如果您使用更大的内存一次复制多个字节。内存类型类似于 long 而不是 char

接下来,就您的访问函数 fb_set_px 而言,我会 1) 使其在头文件中内联,以便避免需要使用的实际函数调用的开销堆栈来设置激活帧,2)您可以使用帧缓冲区只是一个内存数组这一事实来实际将指针数组映射到表示每个字符的某种struct类型的数据布局frame_buffer(即,某些帧缓冲区有一个字节用于字符,另一个字节用于某些类型的属性,如应用于字符的颜色等)。例如,您可以执行以下操作:

typedef struct frame_buffer_t
{
    unsigned char character;
    unsigned char attributes;
    //add or omit any fields that describe your frame-buffer data-layout
} __attribute__((packed)) frame_buffer_t;

//define as global variable
//make volatile to avoid any compiler re-ordering
unsigned volatile frame_buffer_t* framebuffer[MAX_ROWS_IN_FRAMEBUFFER];

int main()
{
    unsigned volatile frame_buffer_t* temp = global_frame_buffer_start_ptr;

    for (int i=0; i < MAX_ROWS_IN_FRAMEBUFFER; i++)
    {
        framebuffer[i] = temp;
        temp += NUM_COLUMNS;
    }

    //... more code
}

现在您可以简单地将帧缓冲区中的 ax,y 位置寻址为

framebuffer[Y_POS][X_POS].character = SOME_VALUE;

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:

  1. Use the hardware scrolling capabilities of your frame-buffer to scroll to the next line when you get done. This means that you will first clear the next line of the frame-buffer, write the character that you need to on that line, and then increment the frame-buffer starting line pointer by one. This will then "appear" as though the frame-buffer has scrolled down a single line. When you get to the end of the frame buffer's memory, keep the same process going, but the next line you'll clear will be the first line in memory of the frame-buffer. You'll then keep incrementing the starting address of the frame-buffer (this is still pointing somewhere near the end of the frame-buffer's memory) until that too is pointing to the last line in memory of the frame-buffer, at which point you'll then wrap around to the first line in memory.
  2. If your frame-buffer is still multi-page, but does not have a wrap-around capability (like the VGA character console), then keep a pointer to the start of the frame-buffer, and when you get to the last "page" in memory, use 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, but memcpy() is very efficient, especially if you copy multiple bytes at a time using a larger memory-type like a long rather than a char.

Next, as far as your access function fb_set_px goes, I would 1) make it inline 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 some struct 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:

typedef struct frame_buffer_t
{
    unsigned char character;
    unsigned char attributes;
    //add or omit any fields that describe your frame-buffer data-layout
} __attribute__((packed)) frame_buffer_t;

//define as global variable
//make volatile to avoid any compiler re-ordering
unsigned volatile frame_buffer_t* framebuffer[MAX_ROWS_IN_FRAMEBUFFER];

int main()
{
    unsigned volatile frame_buffer_t* temp = global_frame_buffer_start_ptr;

    for (int i=0; i < MAX_ROWS_IN_FRAMEBUFFER; i++)
    {
        framebuffer[i] = temp;
        temp += NUM_COLUMNS;
    }

    //... more code
}

Now you can simply address a x,y position in your frame-buffer as

framebuffer[Y_POS][X_POS].character = SOME_VALUE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文