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
namespace KIGFX
43 44 45 46 47 48 49 50 51
{
class VERTEX_ITEM;
class SHADER;

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

Maciej Suminski's avatar
Maciej Suminski committed
52
    ///> @copydoc VERTEX_CONTAINER::SetItem()
53 54
    virtual void SetItem( VERTEX_ITEM* aItem );

Maciej Suminski's avatar
Maciej Suminski committed
55
    ///> @copydoc VERTEX_CONTAINER::FinishItem()
56 57
    virtual void FinishItem();

Maciej Suminski's avatar
Maciej Suminski committed
58
    ///> @copydoc VERTEX_CONTAINER::Allocate()
59 60
    virtual VERTEX* Allocate( unsigned int aSize );

Maciej Suminski's avatar
Maciej Suminski committed
61
    ///> @copydoc VERTEX_CONTAINER::Delete()
62
    virtual void Delete( VERTEX_ITEM* aItem );
63

Maciej Suminski's avatar
Maciej Suminski committed
64
    ///> @copydoc VERTEX_CONTAINER::Clear()
65 66 67 68 69 70 71 72 73 74 75
    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:
Maciej Suminski's avatar
Maciej Suminski committed
76 77 78
    ///> Maps size of free memory chunks to their offsets
    typedef std::pair<unsigned int, unsigned int> CHUNK;
    typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
79 80

    /// List of all the stored items
Maciej Suminski's avatar
Maciej Suminski committed
81
    typedef std::set<VERTEX_ITEM*> ITEMS;
82

Maciej Suminski's avatar
Maciej Suminski committed
83 84
    ///> Stores size & offset of free chunks.
    FREE_CHUNK_MAP      m_freeChunks;
85

Maciej Suminski's avatar
Maciej Suminski committed
86 87
    ///> Stored VERTEX_ITEMs
    ITEMS               m_items;
88

Maciej Suminski's avatar
Maciej Suminski committed
89
    ///> Currently modified item
90 91
    VERTEX_ITEM*        m_item;

Maciej Suminski's avatar
Maciej Suminski committed
92
    ///> Properties of currently modified chunk & item
93 94 95 96 97
    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
    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.
     */
Maciej Suminski's avatar
Maciej Suminski committed
149
    inline int getChunkSize( const CHUNK& aChunk ) const
150 151 152 153 154 155 156 157 158 159
    {
        return aChunk.first;
    }

    /**
     * Function getChunkOffset()
     * returns offset of the chunk.
     *
     * @param aChunk is the chunk.
     */
Maciej Suminski's avatar
Maciej Suminski committed
160
    inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
161 162 163 164 165
    {
        return aChunk.second;
    }

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

#endif /* CACHED_CONTAINER_H_ */