OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:43 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Double buffering causes system to freeze
PostPosted: Mon Jan 10, 2022 12:16 pm 
Offline

Joined: Mon Jan 10, 2022 12:04 pm
Posts: 3
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:
BackBuffer = malloc(framebuffer->Width * framebuffer->Height * 4);


Drawing to the back buffer:
Code:
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:
memcpy((uint64_t*)buffer->BaseAddress, (uint64_t*)BackBuffer, buffer->Width * buffer->Height * 4);


memcpy function:
Code:
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:
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


Top
 Profile  
 
 Post subject: Re: Double buffering causes system to freeze
PostPosted: Tue Jan 11, 2022 10:12 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: Double buffering causes system to freeze
PostPosted: Tue Jan 11, 2022 12:30 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
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!


Top
 Profile  
 
 Post subject: Re: Double buffering causes system to freeze
PostPosted: Tue Jan 11, 2022 1:29 pm 
Offline

Joined: Mon Jan 10, 2022 12:04 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Double buffering causes system to freeze
PostPosted: Tue Jan 11, 2022 1:38 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 62 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group