Skip to content

Commit

Permalink
2dgraphics: Speed up operation
Browse files Browse the repository at this point in the history
Before a new graphics frame has been prepared in the uncached display
buffer. Now a separate cached buffer is used, which speeds up operation.
The separate buffer is copied to the display buffer in the vertical sync
phase to suppress flickering. Now this class can be used on the
Raspberry Pi 5 too, but without vertical sync support.

Issue #489
  • Loading branch information
rsta2 committed Oct 28, 2024
1 parent ab4fc91 commit 14a1c22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Circle supports the following features:
| | (not on Raspberry Pi 4) | |
| | uGUI (by Achim Doebler) | |
| | LVGL (by LVGL Kft) | x |
| | 2D graphics class in base library | |
| | 2D graphics class in base library | x |
| | | |
| Not supported | Bluetooth | |

Expand Down
26 changes: 20 additions & 6 deletions lib/2dgraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright (C) 2021 Stephane Damo
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2014-2023 R. Stange <[email protected]>
// Copyright (C) 2014-2024 R. Stange <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -39,6 +39,8 @@ C2DGraphics::C2DGraphics (unsigned nWidth, unsigned nHeight, boolean bVSync, uns

C2DGraphics::~C2DGraphics (void)
{
delete [] m_Buffer;

if(m_pFrameBuffer)
{
delete m_pFrameBuffer;
Expand All @@ -47,8 +49,12 @@ C2DGraphics::~C2DGraphics (void)

boolean C2DGraphics::Initialize (void)
{
#if RASPPI <= 4
m_pFrameBuffer = new CBcmFrameBuffer (m_nWidth, m_nHeight, DEPTH, m_nWidth, 2*m_nHeight,
m_nDisplay, TRUE);
#else
m_pFrameBuffer = new CBcmFrameBuffer (m_nWidth, m_nHeight, DEPTH, 0, 0, m_nDisplay, FALSE);
#endif

#if DEPTH == 8
m_pFrameBuffer->SetPalette (RED_COLOR, RED_COLOR16);
Expand Down Expand Up @@ -76,8 +82,13 @@ boolean C2DGraphics::Initialize (void)
m_baseBuffer = (TScreenColor *) (uintptr) m_pFrameBuffer->GetBuffer();
m_nWidth = m_pFrameBuffer->GetWidth();
m_nHeight = m_pFrameBuffer->GetHeight();
m_Buffer = m_baseBuffer + m_nWidth * m_nHeight;


m_Buffer = new TScreenColor[m_nWidth * m_nHeight];
if (!m_Buffer)
{
return FALSE;
}

return TRUE;
}

Expand All @@ -89,6 +100,7 @@ boolean C2DGraphics::Resize (unsigned nWidth, unsigned nHeight)
m_nWidth = nWidth;
m_nHeight = nHeight;

delete [] m_Buffer;
m_Buffer = 0;
m_bBufferSwapped = TRUE;

Expand Down Expand Up @@ -361,15 +373,17 @@ TScreenColor* C2DGraphics::GetBuffer ()

void C2DGraphics::UpdateDisplay()
{
#if RASPPI <= 4
if(m_bVSync)
{
m_pFrameBuffer->SetVirtualOffset(0, m_bBufferSwapped ? m_nHeight : 0);
m_pFrameBuffer->WaitForVerticalSync();
memcpy (m_baseBuffer + m_bBufferSwapped * m_nWidth * m_nHeight, m_Buffer,
m_nWidth * m_nHeight * sizeof(TScreenColor));
m_pFrameBuffer->SetVirtualOffset(0, m_bBufferSwapped ? m_nHeight : 0);
m_bBufferSwapped = !m_bBufferSwapped;
m_Buffer = m_baseBuffer + m_bBufferSwapped * m_nWidth * m_nHeight;
}
else
#endif
{
memcpy(m_baseBuffer, m_Buffer, m_nWidth * m_nHeight * sizeof(TScreenColor));
}
Expand Down
2 changes: 1 addition & 1 deletion sample/README
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SAMPLES
38-bootloader HTTP- and TFTP-based bootloader with Web front-end
39-umsdplugging [PnP,5] Plug in and remove USB flash drives, list directory
40-irqlatency [PnP] Displays the maximum measured IRQ latency
41-screenanimations 2D graphical shapes demo on screen without flickering or screen tearing
41-screenanimations [5] 2D graphical shapes demo on screen without flickering or screen tearing
42-soundinput [5] I2S or USB to PWM sound data converter and digital sound recorder

Samples marked with [PnP] are enabled for USB plug-and-play.
Expand Down

0 comments on commit 14a1c22

Please sign in to comment.