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.
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 alongrather than achar.Next, as far as your access function
fb_set_pxgoes, I would 1) make itinlineinside 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 somestructtype 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