kicad/include/gal/opengl/cached_container.h

190 lines
5.5 KiB
C
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright 2013-2017 CERN
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
2020-12-21 23:42:21 +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://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
*/
#ifndef CACHED_CONTAINER_H_
#define CACHED_CONTAINER_H_
#include <gal/opengl/vertex_container.h>
#include <map>
#include <set>
namespace KIGFX
{
class VERTEX_ITEM;
class SHADER;
/**
2020-12-21 23:42:21 +00:00
* Class to store VERTEX instances with caching.
*
* It associates VERTEX objects and with VERTEX_ITEMs. Caching vertices data in the memory and a
* enables fast reuse of that data.
*/
class CACHED_CONTAINER : public VERTEX_CONTAINER
{
public:
CACHED_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
virtual ~CACHED_CONTAINER() {}
bool IsCached() const override
{
return true;
}
2016-09-24 18:53:15 +00:00
virtual void SetItem( VERTEX_ITEM* aItem ) override;
///< @copydoc VERTEX_CONTAINER::FinishItem()
2016-09-24 18:53:15 +00:00
virtual void FinishItem() override;
2013-09-12 07:44:57 +00:00
/**
* Return allocated space for the requested number of vertices associated with the
* current item (set with SetItem()).
*
* The allocated space is added at the end of the chunk used by the current item and
* may serve to store new vertices.
*
* @param aSize is the number of vertices to be allocated.
* @return Pointer to the allocated space.
* @throw bad_alloc exception if allocation fails.
*/
2016-09-24 18:53:15 +00:00
virtual VERTEX* Allocate( unsigned int aSize ) override;
///< @copydoc VERTEX_CONTAINER::Delete()
2016-09-24 18:53:15 +00:00
virtual void Delete( VERTEX_ITEM* aItem ) override;
///< @copydoc VERTEX_CONTAINER::Clear()
2016-09-24 18:53:15 +00:00
virtual void Clear() override;
/**
2020-12-21 23:42:21 +00:00
* Return handle to the vertex buffer. It might be negative if the buffer is not initialized.
*/
virtual unsigned int GetBufferHandle() const = 0;
/**
2020-12-21 23:42:21 +00:00
* Return true if vertex buffer is currently mapped.
*/
virtual bool IsMapped() const = 0;
///< @copydoc VERTEX_CONTAINER::Map()
virtual void Map() override = 0;
///< @copydoc VERTEX_CONTAINER::Unmap()
virtual void Unmap() override = 0;
protected:
///< Maps size of free memory chunks to their offsets
2013-10-14 18:40:36 +00:00
typedef std::pair<unsigned int, unsigned int> CHUNK;
typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
/// List of all the stored items
2013-10-14 18:40:36 +00:00
typedef std::set<VERTEX_ITEM*> ITEMS;
/**
2020-12-21 23:42:21 +00:00
* Resize the chunk that stores the current item to the given size. The current item has
* its offset adjusted after the call, and the new chunk parameters are stored
* in m_chunkOffset and m_chunkSize.
2013-07-30 16:29:54 +00:00
*
* @param aSize is the requested chunk size.
2020-12-21 23:42:21 +00:00
* @return true in case of success, false otherwise.
2013-07-30 16:29:54 +00:00
*/
bool reallocate( unsigned int aSize );
/**
2020-12-21 23:42:21 +00:00
* Remove empty spaces between chunks and optionally resizes the container.
*
2020-12-21 23:42:21 +00:00
* After the operation there is continuous space for storing vertices at the end of the
* container.
*
* @param aNewSize is the new size of container, expressed in number of vertices.
* @return false in case of failure (e.g. memory shortage).
*/
virtual bool defragmentResize( unsigned int aNewSize ) = 0;
/**
2020-12-21 23:42:21 +00:00
* Transfer all stored data to a new buffer, removing empty spaces between the data chunks
* in the container.
2020-12-21 23:42:21 +00:00
*
* @param aTarget is the destination for the defragmented data.
*/
void defragment( VERTEX* aTarget );
/**
2020-12-21 23:42:21 +00:00
* Look for consecutive free memory chunks and merges them, decreasing fragmentation of
* memory.
*/
void mergeFreeChunks();
/**
2020-12-21 23:42:21 +00:00
* Return the size of a chunk.
*
* @param aChunk is the chunk.
*/
2013-10-14 18:40:36 +00:00
inline int getChunkSize( const CHUNK& aChunk ) const
{
return aChunk.first;
}
/**
2020-12-21 23:42:21 +00:00
* Return the offset of a chunk.
*
* @param aChunk is the chunk.
*/
2013-10-14 18:40:36 +00:00
inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
{
return aChunk.second;
}
2016-05-02 14:12:16 +00:00
/**
2020-12-21 23:42:21 +00:00
* Add a chunk marked as a free space.
2016-05-02 14:12:16 +00:00
*/
void addFreeChunk( unsigned int aOffset, unsigned int aSize );
///< Store size & offset of free chunks.
2020-12-21 23:42:21 +00:00
FREE_CHUNK_MAP m_freeChunks;
///< Stored VERTEX_ITEMs
2020-12-21 23:42:21 +00:00
ITEMS m_items;
///< Currently modified item
2020-12-21 23:42:21 +00:00
VERTEX_ITEM* m_item;
///< Properties of currently modified chunk & item
2020-12-21 23:42:21 +00:00
unsigned int m_chunkSize;
unsigned int m_chunkOffset;
///< Maximal vertex index number stored in the container
2020-12-21 23:42:21 +00:00
unsigned int m_maxIndex;
private:
/// Debug & test functions
void showFreeChunks();
2016-05-02 14:12:16 +00:00
void showUsedChunks();
void test();
};
} // namespace KIGFX
#endif /* CACHED_CONTAINER_H_ */