Double buffering causes system to freeze

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
leonrobinsonn
Posts: 3
Joined: Mon Jan 10, 2022 12:04 pm
Libera.chat IRC: leon

Double buffering causes system to freeze

Post 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
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Double buffering causes system to freeze

Post 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.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Double buffering causes system to freeze

Post 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.
Carpe diem!
leonrobinsonn
Posts: 3
Joined: Mon Jan 10, 2022 12:04 pm
Libera.chat IRC: leon

Re: Double buffering causes system to freeze

Post 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
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Double buffering causes system to freeze

Post 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.
Post Reply