2013-07-30 11:58:03 +00:00
|
|
|
/*
|
2013-07-25 12:40:04 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2019-08-29 02:28:40 +00:00
|
|
|
* Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-07-25 12:40:04 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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 the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http:O//www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file cairo_compositor.cpp
|
|
|
|
* @brief Class that handles multitarget rendering (ie. to different textures/surfaces) and
|
|
|
|
* later compositing into a single image (Cairo flavour).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/cairo/cairo_compositor.h>
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-07-25 12:40:04 +00:00
|
|
|
|
|
|
|
CAIRO_COMPOSITOR::CAIRO_COMPOSITOR( cairo_t** aMainContext ) :
|
2018-01-04 12:13:00 +00:00
|
|
|
m_current( 0 ), m_currentContext( aMainContext ), m_mainContext( *aMainContext ),
|
2019-01-21 18:42:06 +00:00
|
|
|
m_currentAntialiasingMode( CAIRO_ANTIALIAS_DEFAULT )
|
2013-07-25 12:40:04 +00:00
|
|
|
{
|
2015-03-21 09:06:04 +00:00
|
|
|
// Do not have uninitialized members:
|
|
|
|
cairo_matrix_init_identity( &m_matrix );
|
|
|
|
m_stride = 0;
|
|
|
|
m_bufferSize = 0;
|
2013-07-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CAIRO_COMPOSITOR::~CAIRO_COMPOSITOR()
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CAIRO_COMPOSITOR::Initialize()
|
|
|
|
{
|
|
|
|
// Nothing has to be done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-04 12:13:00 +00:00
|
|
|
void CAIRO_COMPOSITOR::SetAntialiasingMode( CAIRO_ANTIALIASING_MODE aMode )
|
|
|
|
{
|
|
|
|
|
|
|
|
switch( aMode )
|
|
|
|
{
|
|
|
|
case CAIRO_ANTIALIASING_MODE::FAST:
|
|
|
|
m_currentAntialiasingMode = CAIRO_ANTIALIAS_FAST;
|
|
|
|
break;
|
|
|
|
case CAIRO_ANTIALIASING_MODE::GOOD:
|
|
|
|
m_currentAntialiasingMode = CAIRO_ANTIALIAS_GOOD;
|
|
|
|
break;
|
|
|
|
case CAIRO_ANTIALIASING_MODE::BEST:
|
|
|
|
m_currentAntialiasingMode = CAIRO_ANTIALIAS_BEST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_currentAntialiasingMode = CAIRO_ANTIALIAS_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 12:40:04 +00:00
|
|
|
void CAIRO_COMPOSITOR::Resize( unsigned int aWidth, unsigned int aHeight )
|
|
|
|
{
|
|
|
|
clean();
|
|
|
|
|
|
|
|
m_width = aWidth;
|
|
|
|
m_height = aHeight;
|
|
|
|
|
|
|
|
m_stride = cairo_format_stride_for_width( CAIRO_FORMAT_ARGB32, m_width );
|
|
|
|
m_bufferSize = m_stride * m_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
unsigned int CAIRO_COMPOSITOR::CreateBuffer()
|
2013-07-25 12:40:04 +00:00
|
|
|
{
|
|
|
|
// Pixel storage
|
2019-08-29 02:28:40 +00:00
|
|
|
BitmapPtr bitmap = new uint32_t[m_bufferSize]();
|
2013-07-25 12:40:04 +00:00
|
|
|
|
|
|
|
// Create the Cairo surface
|
|
|
|
cairo_surface_t* surface = cairo_image_surface_create_for_data(
|
2019-08-29 02:28:40 +00:00
|
|
|
(unsigned char*) bitmap,
|
2013-07-25 12:40:04 +00:00
|
|
|
CAIRO_FORMAT_ARGB32, m_width,
|
|
|
|
m_height, m_stride );
|
|
|
|
cairo_t* context = cairo_create( surface );
|
2019-10-14 08:58:46 +00:00
|
|
|
#ifdef DEBUG
|
2013-08-19 09:02:38 +00:00
|
|
|
cairo_status_t status = cairo_status( context );
|
2013-11-03 18:15:46 +00:00
|
|
|
wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, wxT( "Cairo context creation error" ) );
|
2019-10-14 08:58:46 +00:00
|
|
|
#endif /* DEBUG */
|
2013-07-25 12:40:04 +00:00
|
|
|
|
|
|
|
// Set default settings for the buffer
|
2019-02-16 19:25:10 +00:00
|
|
|
cairo_set_antialias( context, m_currentAntialiasingMode );
|
2013-07-25 12:40:04 +00:00
|
|
|
|
|
|
|
// Use the same transformation matrix as the main context
|
2013-08-19 09:02:38 +00:00
|
|
|
cairo_get_matrix( m_mainContext, &m_matrix );
|
2013-07-25 12:40:04 +00:00
|
|
|
cairo_set_matrix( context, &m_matrix );
|
|
|
|
|
|
|
|
// Store the new buffer
|
|
|
|
CAIRO_BUFFER buffer = { context, surface, bitmap };
|
|
|
|
m_buffers.push_back( buffer );
|
|
|
|
|
|
|
|
return usedBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CAIRO_COMPOSITOR::SetBuffer( unsigned int aBufferHandle )
|
|
|
|
{
|
2013-08-19 09:02:38 +00:00
|
|
|
wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Apply the current transformation matrix
|
|
|
|
cairo_set_matrix( *m_currentContext, &m_matrix );
|
2013-07-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
void CAIRO_COMPOSITOR::Begin()
|
|
|
|
{
|
|
|
|
}
|
2013-07-25 12:40:04 +00:00
|
|
|
|
2017-08-04 12:43:02 +00:00
|
|
|
void CAIRO_COMPOSITOR::ClearBuffer( const COLOR4D& aColor )
|
2013-07-25 12:40:04 +00:00
|
|
|
{
|
2013-08-19 09:02:38 +00:00
|
|
|
// Clear the pixel storage
|
2019-08-29 02:28:40 +00:00
|
|
|
memset( m_buffers[m_current].bitmap, 0x00, m_bufferSize * sizeof(int) );
|
2013-07-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CAIRO_COMPOSITOR::DrawBuffer( unsigned int aBufferHandle )
|
|
|
|
{
|
2013-08-19 09:02:38 +00:00
|
|
|
wxASSERT_MSG( aBufferHandle <= usedBuffers(), wxT( "Tried to use a not existing buffer" ) );
|
2013-07-25 12:40:04 +00:00
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
// 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 );
|
2013-07-25 12:40:04 +00:00
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
// Draw the selected buffer contents
|
|
|
|
cairo_set_source_surface( m_mainContext, m_buffers[aBufferHandle - 1].surface, 0.0, 0.0 );
|
|
|
|
cairo_paint( m_mainContext );
|
2013-07-30 16:29:54 +00:00
|
|
|
|
2013-08-19 09:02:38 +00:00
|
|
|
// Restore the transformation matrix
|
|
|
|
cairo_set_matrix( m_mainContext, &m_matrix );
|
2013-07-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 17:58:29 +00:00
|
|
|
void CAIRO_COMPOSITOR::Present()
|
|
|
|
{
|
|
|
|
}
|
2013-07-25 12:40:04 +00:00
|
|
|
|
|
|
|
void CAIRO_COMPOSITOR::clean()
|
|
|
|
{
|
|
|
|
CAIRO_BUFFERS::const_iterator it;
|
|
|
|
|
|
|
|
for( it = m_buffers.begin(); it != m_buffers.end(); ++it )
|
|
|
|
{
|
|
|
|
cairo_destroy( it->context );
|
|
|
|
cairo_surface_destroy( it->surface );
|
2019-08-29 03:25:27 +00:00
|
|
|
delete[] it->bitmap;
|
2013-07-25 12:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_buffers.clear();
|
|
|
|
}
|