TextureCache.cc 2.42 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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL 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 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class TextureCache.
 *
28
 *   @author Lionel Heng <hengli@inf.ethz.ch>
29 30 31 32 33
 *
 */

#include "TextureCache.h"

34 35 36
TextureCache::TextureCache(int cacheSize)
    : mCacheSize(cacheSize)
    , mImageCache(new WebImageCache(0, cacheSize))
37
{
38 39
    for (int i = 0; i < mCacheSize; ++i)
    {
40
        TexturePtr t(new Texture(i));
41

42
        mTextures.push_back(t);
43 44 45 46
    }
}

TexturePtr
47
TextureCache::get(const QString& tileURL)
48
{
49 50 51
    QPair<TexturePtr, int> p1 = lookup(tileURL);
    if (!p1.first.isNull())
    {
52 53 54
        return p1.first;
    }

55 56 57 58
    QPair<WebImagePtr, int> p2 = mImageCache->lookup(tileURL);
    if (!p2.first.isNull())
    {
        mTextures[p2.second]->sync(p2.first);
59
        p1 = lookup(tileURL);
60 61 62 63 64 65 66 67 68 69

        return p1.first;
    }

    return TexturePtr();
}

void
TextureCache::sync(void)
{
70 71 72 73 74
    if (requireSync())
    {
        for (int i = 0; i < mTextures.size(); ++i)
        {
            mTextures[i]->sync(mImageCache->at(i));
75 76 77 78
        }
    }
}

79
QPair<TexturePtr, int>
80
TextureCache::lookup(const QString& tileURL)
81
{
82 83 84 85 86
    for (int i = 0; i < mTextures.size(); ++i)
    {
        if (mTextures[i]->getSourceURL() == tileURL)
        {
            return qMakePair(mTextures[i], i);
87 88 89 90 91 92 93 94 95
        }
    }

    return qMakePair(TexturePtr(), -1);
}

bool
TextureCache::requireSync(void) const
{
96 97 98 99
    for (int i = 0; i < mCacheSize; ++i)
    {
        if (mImageCache->at(i)->getSyncFlag())
        {
100 101 102 103 104
            return true;
        }
    }
    return false;
}