Page 1 of 1

Issues with terminal scrolling in graphical mode

Posted: Mon Nov 06, 2023 1:50 pm
by zun1
Hi, I'm new to OSDev and I setup a minimal kernel with BOOTBOOT and all it does so far is printing. For a few days, I've been trying to figure out how to copy a row of glyphs up by one row, like I managed in VGA mode, but it seems that my code only clears the line above.

(For context) How the glyphs are drawn:

Code: Select all

void
kdrawc(char c, int x, int y, uint32_t color)
{
        uint8_t *glyph =        (uint8_t *) &BINARY_FONT_PSF_START
                                + font->header_size
                                + (c > 0 && c < font->num_glyph ? c : 0)
                                * font->bytes_per_glyph;

        int offset =    (y * font->height * bootboot.fb_scanline) +
                        (x * (font->width + 1) * sizeof(uint32_t));

        int bytes_per_line = (font->width + 7) / 8;

        for (int y = 0; y < font->height; ++y) {
                int line = offset;
                int mask = 1 << (font->width - 1);

                for (int x = 0; x < font->width; ++x) {
                        *((PIXEL *) ((uint64_t) &fb + line)) =
                        ((int) *glyph) & mask ? color : 0;

                        mask >>= 1;
                        line += sizeof(PIXEL);
                }

                glyph += bytes_per_line;
                offset += bootboot.fb_scanline;
        }
}
The function that's supposed to replace the destination line with the source line (possibly bad code quality):

Code: Select all

void
kmoveline(int dst_line, int src_line)
{
        int dst_offset =        (dst_line * pfont->height * bootboot.fb_scanline) +
                                (0 * (pfont->width + 1) * sizeof(uint32_t));
        
        int src_offset =        (src_line * pfont->height * bootboot.fb_scanline) +
                                (0 * (pfont->width + 1) * sizeof(uint32_t));

        for (int dst_y = 0; dst_y < pfont->height; ++dst_y) {
                int dst_line = dst_offset;

                for (int src_y = 0; src_y < pfont->height; ++src_y) {
                        int src_line = src_offset;

                        for (int dst_x = 0; dst_x < (pfont->width + 1); ++dst_x) {

                                for (int src_x = 0; src_x < (pfont->width + 1); ++src_x) {
                                        *((PIXEL *) ((uint64_t) &fb + dst_line)) =
                                        *((PIXEL *) ((uint64_t) &fb + src_line));

                                        src_line += sizeof(PIXEL);
                                }
                                dst_line += sizeof(PIXEL);
                        }
                        src_offset += bootboot.fb_scanline;
                }
                dst_offset += bootboot.fb_scanline;
        }
}
I hope this is complete, but if I missed anything, let me know and I will provide any details.
Thank you in advance.

Re: Issues with terminal scrolling in graphical mode

Posted: Tue Feb 13, 2024 11:14 am
by Octocontrabass
Adjusting the source and destination pointers should happen at the same time. You've nested the source pointer calculation one loop deeper, which means instead of copying each source pixel to each destination pixel, you're copying entire characters to single pixels.

Also, since you're copying entire scanlines, you don't really need a nested loop for each character, you can just copy the whole scanline.

Also, reading the framebuffer is usually very slow, so you might be able to speed it up by saving the text somewhere and redrawing it instead of copying the already-drawn scanlines.

Re: Issues with terminal scrolling in graphical mode

Posted: Tue Feb 13, 2024 2:55 pm
by zun1
Octocontrabass wrote:Adjusting the source and destination pointers should happen at the same time. You've nested the source pointer calculation one loop deeper, which means instead of copying each source pixel to each destination pixel, you're copying entire characters to single pixels.

Also, since you're copying entire scanlines, you don't really need a nested loop for each character, you can just copy the whole scanline.

Also, reading the framebuffer is usually very slow, so you might be able to speed it up by saving the text somewhere and redrawing it instead of copying the already-drawn scanlines.
Thank you very much, I've actually been able to make it work after writing this post (a few months ago) and I'll take your suggestions to heart!