Page 1 of 1

Double buffering causes system to freeze

Posted: Mon Jan 10, 2022 12:16 pm
by leonrobinsonn
Hey, I am currently working on implementing double buffering for my GOP driver. But after copying over the memory from the backbuffer to the framebuffer, it freezes.

Setting up the back buffer:

Code: Select all

BackBuffer = malloc(framebuffer->Width * framebuffer->Height * 4);
Drawing to the back buffer:

Code: Select all

void Graphics::draw_pixel(int x, int y, int color)
{
    *(unsigned int*)((unsigned int*)BackBuffer + x + (y * buffer->PixelsPerScanLine)) = color;
}

void Graphics::draw_rect(int x, int y, int w, int h, int color)
{
    for (int Y = y; Y < y + h; Y++)
    {
        for (int X = x; X < x + w; X++)
        {
            draw_pixel(X, Y, color);
        }
    }
}
Copying from the back buffer to frame buffer:

Code: Select all

memcpy((uint64_t*)buffer->BaseAddress, (uint64_t*)BackBuffer, buffer->Width * buffer->Height * 4);
memcpy function:

Code: Select all

void* memcpy(uint64_t* dest, uint64_t* source, int count)
{
    for (int i = 0; i < count; i++)
    {
        dest[i] = source[i];
    }
    return dest;
}
The drawing:

Code: Select all

g.draw_rect(0, 0, 50, 50, 0xFFFFFF);
g.update(); // Draws white square successfully but then freezes the system

// This doesn't draw!
g.draw_rect(0, 0, 50, 50, 0xFF0000);
g.update();
Not sure if I need to post any more information but I can if that's needed

Re: Double buffering causes system to freeze

Posted: Tue Jan 11, 2022 10:12 am
by Octocontrabass
Your memcpy function copies 8*count bytes, which means you're telling it to copy 8 times as much data as it should. Access beyond an object's boundaries is undefined behavior.

Also, it relies on accessing objects through incompatible pointer types, which is undefined behavior.

Re: Double buffering causes system to freeze

Posted: Tue Jan 11, 2022 12:30 pm
by nullplan
In addition to the above, the way your memcpy function is declared, it invokes undefined behavior with GCC as compiler, since GCC requires memcpy to exist and be declared a certain way. Due to how GCC's optimizers work, I would suggest implementing the memory functions (memcpy(), memmove(), memcmp(), memset()) in assembler instead. They are not the most complicated functions in the world.

Re: Double buffering causes system to freeze

Posted: Tue Jan 11, 2022 1:29 pm
by leonrobinsonn
Octocontrabass wrote:Your memcpy function copies 8*count bytes, which means you're telling it to copy 8 times as much data as it should. Access beyond an object's boundaries is undefined behavior.

Also, it relies on accessing objects through incompatible pointer types, which is undefined behavior.
Yeah, this makes sense since I just divided the memcpy count by 8 and it seems to work fine

Re: Double buffering causes system to freeze

Posted: Tue Jan 11, 2022 1:38 pm
by Octocontrabass
leonrobinsonn wrote:it seems to work fine
Undefined behavior may stop working at any point, for any reason (including no reason at all). You should fix the rest of the undefined behavior too.