Commit 43ae1cb9 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Smarter way of the overlay rendering (overlay is always refreshed, while...

Smarter way of the overlay rendering (overlay is always refreshed, while cached&noncached targets only if the viewport or items have changed).
parent e87eea7a
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -127,6 +127,8 @@ void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
{
    m_gal->ResizeScreen( aEvent.GetSize().x, aEvent.GetSize().y );
    m_view->SetTargetDirty( KiGfx::TARGET_CACHED );
    m_view->SetTargetDirty( KiGfx::TARGET_NONCACHED );
}


@@ -134,24 +136,19 @@ void EDA_DRAW_PANEL_GAL::Refresh( bool eraseBackground, const wxRect* rect )
{
#ifdef __WXDEBUG__
    prof_counter time;

    prof_start( &time, false );
#endif /* __WXDEBUG__ */

    printf("Refresh!\n");

    m_gal->BeginDrawing();
    m_gal->SetBackgroundColor( KiGfx::COLOR4D( 0.0, 0.0, 0.0, 1.0 ) );
    m_gal->ClearScreen();

    m_gal->DrawGrid();
    m_view->Redraw();

    m_gal->EndDrawing();

#ifdef __WXDEBUG__
    prof_end( &time );

    wxLogDebug( wxT( "EDA_DRAW_PANEL_GAL::Refresh: %.0f ms (%.0f fps)" ),
        static_cast<double>( time.value ) / 1000.0, 1000000.0 / static_cast<double>( time.value ) );
#endif /* __WXDEBUG__ */
@@ -184,6 +181,9 @@ void EDA_DRAW_PANEL_GAL::SwitchBackend( GalType aGalType )
    m_gal->SetScreenDPI( 106 );                                     // Display resolution setting
    m_gal->ComputeWorldScreenMatrix();

    wxSize size = GetClientSize();
    m_gal->ResizeScreen( size.GetX(), size.GetY() );

    if( m_painter )
        m_painter->SetGAL( m_gal );

@@ -193,9 +193,6 @@ void EDA_DRAW_PANEL_GAL::SwitchBackend( GalType aGalType )
        m_view->RecacheAllItems( true );
    }

    wxSize size = GetClientSize();
    m_gal->ResizeScreen( size.GetX(), size.GetY() );

    m_currentGal = aGalType;
}

+27 −41
Original line number Diff line number Diff line
@@ -36,8 +36,6 @@ using namespace KiGfx;
CAIRO_COMPOSITOR::CAIRO_COMPOSITOR( cairo_t** aMainContext ) :
    m_current( 0 ), m_currentContext( aMainContext ), m_mainContext( *aMainContext )
{
    // Obtain the transformation matrix used in the main context
    cairo_get_matrix( m_mainContext, &m_matrix );
}


@@ -65,11 +63,10 @@ void CAIRO_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
}


unsigned int CAIRO_COMPOSITOR::GetBuffer()
unsigned int CAIRO_COMPOSITOR::CreateBuffer()
{
    // Pixel storage
    BitmapPtr bitmap( new unsigned int[m_bufferSize] );

    memset( bitmap.get(), 0x00, m_bufferSize * sizeof(int) );

    // Create the Cairo surface
@@ -89,6 +86,7 @@ unsigned int CAIRO_COMPOSITOR::GetBuffer()
    cairo_set_line_cap( context, CAIRO_LINE_CAP_ROUND );

    // Use the same transformation matrix as the main context
    cairo_get_matrix( m_mainContext, &m_matrix );
    cairo_set_matrix( context, &m_matrix );

    // Store the new buffer
@@ -101,42 +99,36 @@ unsigned int CAIRO_COMPOSITOR::GetBuffer()

void CAIRO_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
{
    if( aBufferHandle <= usedBuffers() )
    {
    wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );

    // Get currently used transformation matrix, so it can be applied to the new buffer
    cairo_get_matrix( *m_currentContext, &m_matrix );

    m_current = aBufferHandle - 1;
    *m_currentContext = m_buffers[m_current].context;
    }

#ifdef __WXDEBUG__
    else
        wxLogDebug( wxT( "Tried to use a not existing buffer" ) );
#endif
    // Apply the current transformation matrix
    cairo_set_matrix( *m_currentContext, &m_matrix );
}


void CAIRO_COMPOSITOR::ClearBuffer()
{
    // Reset the transformation matrix, so it is possible to composite images using
    // screen coordinates instead of world coordinates
    cairo_identity_matrix( m_buffers[m_current].context );

    cairo_set_source_rgba( m_buffers[m_current].context, 0.0, 0.0, 0.0, 0.0 );
    cairo_rectangle( m_buffers[m_current].context, 0.0, 0.0, m_width, m_height );
    cairo_fill( m_buffers[m_current].context );

    // Restore the transformation matrix
    cairo_set_matrix( m_buffers[m_current].context, &m_matrix );
    // Clear the pixel storage
    memset( m_buffers[m_current].bitmap.get(), 0x00, m_bufferSize * sizeof(int) );
}


void CAIRO_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
{
    if( aBufferHandle <= usedBuffers() )
    {
    wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );

    // Reset the transformation matrix, so it is possible to composite images using
    // screen coordinates instead of world coordinates
    cairo_get_matrix( m_mainContext, &m_matrix );
    cairo_identity_matrix( m_mainContext );

    // Draw the selected buffer contents
    cairo_set_source_surface( m_mainContext, m_buffers[aBufferHandle - 1].surface, 0.0, 0.0 );
    cairo_paint( m_mainContext );

@@ -144,12 +136,6 @@ void CAIRO_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
    cairo_set_matrix( m_mainContext, &m_matrix );
}

#ifdef __WXDEBUG__
    else
        wxLogDebug( wxT( "Tried to use a not existing buffer" ) );
#endif
}


void CAIRO_COMPOSITOR::clean()
{
+43 −7
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ CAIRO_GAL::CAIRO_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener,
    isGrouping          = false;
    isInitialized       = false;
    isDeleteSavedPixels = false;
    validCompositor     = false;
    groupCounter        = 0;

    // Connecting the event handlers
@@ -90,8 +91,12 @@ CAIRO_GAL::~CAIRO_GAL()
void CAIRO_GAL::BeginDrawing()
{
    initSurface();
    if( !validCompositor )
        setCompositor();

    compositor->SetMainContext( context );
    compositor->SetBuffer( mainBuffer );

    // Cairo grouping prevents display of overlapping items on the same layer in the lighter color
    cairo_push_group( currentContext );
}
@@ -273,6 +278,10 @@ void CAIRO_GAL::ResizeScreen( int aWidth, int aHeight )
    deleteBitmaps();
    allocateBitmaps();

    if( validCompositor )
        compositor->Resize( aWidth, aHeight );
    validCompositor = false;

    SetSize( wxSize( aWidth, aHeight ) );
}

@@ -719,7 +728,7 @@ void CAIRO_GAL::SetTarget( RenderTarget aTarget )
{
    // If the compositor is not set, that means that there is a recaching process going on
    // and we do not need the compositor now
    if( !compositor )
    if( !validCompositor )
        return;

    // Cairo grouping prevents display of overlapping items on the same layer in the lighter color
@@ -751,6 +760,31 @@ RenderTarget CAIRO_GAL::GetTarget() const
}


void CAIRO_GAL::ClearTarget( RenderTarget aTarget )
{
    // Save the current state
    unsigned int currentBuffer = compositor->GetBuffer();

    switch( aTarget )
    {
    // Cached and noncached items are rendered to the same buffer
    default:
    case TARGET_CACHED:
    case TARGET_NONCACHED:
        compositor->SetBuffer( mainBuffer );
        break;

    case TARGET_OVERLAY:
        compositor->SetBuffer( overlayBuffer );
        break;
    }
    compositor->ClearBuffer();

    // Restore the previous state
    compositor->SetBuffer( currentBuffer );
}


VECTOR2D CAIRO_GAL::ComputeCursorToWorld( const VECTOR2D& aCursorPosition )
{
    MATRIX3x3D inverseMatrix = worldScreenMatrix.Inverse();
@@ -972,8 +1006,10 @@ void CAIRO_GAL::setCompositor()
    compositor->Resize( screenSize.x, screenSize.y );

    // Prepare buffers
    mainBuffer = compositor->GetBuffer();
    overlayBuffer = compositor->GetBuffer();
    mainBuffer = compositor->CreateBuffer();
    overlayBuffer = compositor->CreateBuffer();

    validCompositor = true;
}


+11 −11
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ void OPENGL_COMPOSITOR::Initialize()
                               GL_RENDERBUFFER, m_depthBuffer );

    // Unbind the framebuffer, so by default all the rendering goes directly to the display
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    glBindFramebuffer( GL_FRAMEBUFFER, DIRECT_RENDERING );
    m_currentFbo = 0;

    m_initialized = true;
@@ -91,7 +91,7 @@ void OPENGL_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
}


unsigned int OPENGL_COMPOSITOR::GetBuffer()
unsigned int OPENGL_COMPOSITOR::CreateBuffer()
{
    wxASSERT( m_initialized );

@@ -169,8 +169,8 @@ unsigned int OPENGL_COMPOSITOR::GetBuffer()


    ClearBuffer();
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    m_currentFbo = 0;
    glBindFramebuffer( GL_FRAMEBUFFER, DIRECT_RENDERING );
    m_currentFbo = DIRECT_RENDERING;

    // Store the new buffer
    OPENGL_BUFFER buffer = { textureTarget, attachmentPoint };
@@ -186,10 +186,10 @@ void OPENGL_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
        return;

    // Change the rendering destination to the selected attachment point
    if( aBufferHandle == 0 )
    if( aBufferHandle == DIRECT_RENDERING )
    {
        glBindFramebuffer( GL_FRAMEBUFFER, 0 );
        m_currentFbo = 0;
        glBindFramebuffer( GL_FRAMEBUFFER, DIRECT_RENDERING );
        m_currentFbo = DIRECT_RENDERING;
    }
    else if( m_currentFbo != m_framebuffer )
    {
@@ -197,7 +197,7 @@ void OPENGL_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
        m_currentFbo = m_framebuffer;
    }

    if( m_currentFbo != 0 && m_current != aBufferHandle - 1 )
    if( m_currentFbo != DIRECT_RENDERING )
    {
        m_current = aBufferHandle - 1;
        glDrawBuffer( m_buffers[m_current].attachmentPoint );
@@ -219,8 +219,8 @@ void OPENGL_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
    wxASSERT( m_initialized );

    // Switch to the main framebuffer and blit the scene
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    m_currentFbo = 0;
    glBindFramebuffer( GL_FRAMEBUFFER, DIRECT_RENDERING );
    m_currentFbo = DIRECT_RENDERING;

    // Depth test has to be disabled to make transparency working
    glDisable( GL_DEPTH_TEST );
@@ -279,4 +279,4 @@ void OPENGL_COMPOSITOR::clean()
}


GLuint OPENGL_COMPOSITOR::m_currentFbo = 0;
GLuint OPENGL_COMPOSITOR::m_currentFbo = DIRECT_RENDERING;
+30 −8
Original line number Diff line number Diff line
@@ -130,8 +130,8 @@ void OPENGL_GAL::BeginDrawing()

        // Prepare rendering target buffers
        compositor.Initialize();
        mainBuffer = compositor.GetBuffer();
        overlayBuffer = compositor.GetBuffer();
        mainBuffer = compositor.CreateBuffer();
        overlayBuffer = compositor.CreateBuffer();

        isFramebufferInitialized = true;
    }
@@ -187,13 +187,10 @@ void OPENGL_GAL::BeginDrawing()
    SetFillColor( fillColor );
    SetStrokeColor( strokeColor );

    // Prepare buffers for drawing
    compositor.SetBuffer( mainBuffer );
    compositor.ClearBuffer();
    compositor.SetBuffer( overlayBuffer );
    compositor.ClearBuffer();
    compositor.SetBuffer( 0 );    // Unbind buffers
    // Unbind buffers - set compositor for direct drawing
    compositor.SetBuffer( OPENGL_COMPOSITOR::DIRECT_RENDERING );

    // Remove all previously stored items
    nonCachedManager.Clear();
    overlayManager.Clear();

@@ -711,6 +708,31 @@ RenderTarget OPENGL_GAL::GetTarget() const
}


void OPENGL_GAL::ClearTarget( RenderTarget aTarget )
{
    // Save the current state
    unsigned int oldTarget = compositor.GetBuffer();

    switch( aTarget )
    {
    // Cached and noncached items are rendered to the same buffer
    default:
    case TARGET_CACHED:
    case TARGET_NONCACHED:
        compositor.SetBuffer( mainBuffer );
        break;

    case TARGET_OVERLAY:
        compositor.SetBuffer( overlayBuffer );
        break;
    }
    compositor.ClearBuffer();

    // Restore the previous state
    compositor.SetBuffer( oldTarget );
}


VECTOR2D OPENGL_GAL::ComputeCursorToWorld( const VECTOR2D& aCursorPosition )
{
    VECTOR2D cursorPosition = aCursorPosition;
Loading