Fastest way to draw characters in a console (framebuffer)?
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 &开发者_运维技巧lt; 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.
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 |#######|
|#######| |#######| | |
|-------| |-------| |-------|
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:
- 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.
- 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, 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 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;
精彩评论