cached_container.h 5.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @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
 */

/**
 * @file cached_container.h
 * @brief Class to store instances of VERTEX with caching. It allows storing VERTEX objects and
 * associates them with VERTEX_ITEMs. This leads to a possibility of caching vertices data in the
 * GPU memory and a fast reuse of that data.
 */

#ifndef CACHED_CONTAINER_H_
#define CACHED_CONTAINER_H_

#include <gal/opengl/vertex_container.h>
#include <map>
#include <set>

// Debug messages verbosity level
40
//#define CACHED_CONTAINER_TEST 1
41 42 43 44 45 46 47 48 49 50 51 52 53 54

namespace KiGfx
{
class VERTEX_ITEM;
class SHADER;

class CACHED_CONTAINER : public VERTEX_CONTAINER
{
public:
    CACHED_CONTAINER( unsigned int aSize = defaultInitSize );

    ///< @copydoc VERTEX_CONTAINER::SetItem()
    virtual void SetItem( VERTEX_ITEM* aItem );

55 56 57
    ///< @copydoc VERTEX_CONTAINER::FinishItem()
    virtual void FinishItem();

58 59 60
    ///< @copydoc VERTEX_CONTAINER::Allocate()
    virtual VERTEX* Allocate( unsigned int aSize );

61 62
    ///< @copydoc VERTEX_CONTAINER::Delete()
    virtual void Delete( VERTEX_ITEM* aItem );
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    ///< @copydoc VERTEX_CONTAINER::Clear()
    virtual void Clear();

    /**
     * Function GetVertices()
     * returns the vertices stored by the specific item.
     *
     * @param aItem is the item.
     */
    virtual VERTEX* GetVertices( const VERTEX_ITEM* aItem ) const;

protected:
    ///< Maps size of free memory chunks to their offsets
    typedef std::pair<unsigned int, unsigned int> Chunk;
    typedef std::multimap<unsigned int, unsigned int> FreeChunkMap;

    /// List of all the stored items
    typedef std::set<VERTEX_ITEM*> Items;

    ///< Stores size & offset of free chunks.
    FreeChunkMap        m_freeChunks;

    ///< Stored VERTEX_ITEMs
    Items               m_items;

    ///< Currently modified item
    VERTEX_ITEM*        m_item;

    ///< Properties of currently modified chunk & item
    unsigned int        m_chunkSize;
    unsigned int        m_chunkOffset;
    unsigned int        m_itemSize;

    /**
Maciej Suminski's avatar
Maciej Suminski committed
98 99 100 101 102 103
     * Function reallocate()
     * resizes the chunk that stores the current item to the given size.
     *
     * @param aSize is the number of vertices to be stored.
     * @return offset of the new chunk.
     */
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    virtual unsigned int reallocate( unsigned int aSize );

    /**
     * Function defragment()
     * removes empty spaces between chunks, so after that there is a long continous space
     * for storing vertices at the and of the container.
     *
     * @param aTarget is the already allocated destination for defragmented data. It has to be
     * at least of the same size as the current container. If left NULL, it will be allocated
     * inside the defragment() function.
     * @return false in case of failure (eg. memory shortage)
     */
    virtual bool defragment( VERTEX* aTarget = NULL );

    /**
     * Function mergeFreeChunks()
     * looks for consecutive free memory chunks and merges them, decreasing fragmentation of
     * memory.
     */
    virtual void mergeFreeChunks();

    /**
     * Function resizeContainer()
     *
     * prepares a bigger container of a given size.
     * @param aNewSize is the new size of container, expressed in vertices
     * @return false in case of failure (eg. memory shortage)
     */
    virtual bool resizeContainer( unsigned int aNewSize );

    /**
     * Function getPowerOf2()
     * returns the nearest power of 2, bigger than aNumber.
     *
     * @param aNumber is the number for which we look for a bigger power of 2.
     */
    unsigned int getPowerOf2( unsigned int aNumber ) const;

private:
    /**
     * Function getChunkSize()
     * returns size of the given chunk.
     *
     * @param aChunk is the chunk.
     */
    inline int getChunkSize( const Chunk& aChunk ) const
    {
        return aChunk.first;
    }

    /**
     * Function getChunkOffset()
     * returns offset of the chunk.
     *
     * @param aChunk is the chunk.
     */
    inline unsigned int getChunkOffset( const Chunk& aChunk ) const
    {
        return aChunk.second;
    }

    /// Debug & test functions
166
#if CACHED_CONTAINER_TEST > 0
167 168 169 170 171 172 173 174 175 176 177 178
    void showFreeChunks();
    void showReservedChunks();
    void test();
#else
    inline void showFreeChunks() {}
    inline void showReservedChunks() {}
    inline void test() {}
#endif /* CACHED_CONTAINER_TEST */
};
} // namespace KiGfx

#endif /* CACHED_CONTAINER_H_ */