Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
97 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <mutex>
20
 
21
#include "timgcache.h"
22
#include "terror.h"
23
 
24
using std::string;
25
using std::vector;
26
using std::mutex;
27
 
28
vector<_IMGCACHE> TImgCache::mImgCache;
29
 
30
mutex _imgCache;
31
 
32
TImgCache::~TImgCache()
33
{
34
    DECL_TRACER("TImgCache::~TImgCache()");
35
 
36
    mImgCache.clear();
37
}
38
 
39
bool TImgCache::addImage(const string& name, SkBitmap& bm)
40
{
41
    DECL_TRACER("TImgCache::addImage(const string& name, SkBitmap& bm)");
42
 
43
    _imgCache.lock();
44
    _IMGCACHE ic;
45
 
46
    ic.name = name;
47
    ic.bitmap = bm;
48
 
49
    if (mImgCache.size() == 0)
50
    {
51
        mImgCache.push_back(ic);
52
        MSG_DEBUG("Bitmap \"" << name << "\" was freshly added.");
53
        _imgCache.unlock();
54
        return true;
55
    }
56
 
57
    vector<_IMGCACHE>::iterator iter;
58
 
59
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
60
    {
61
        if (iter->name == name)
62
        {
63
            MSG_DEBUG("Bitmap \"" << name << "\" already in cache.");
64
            _imgCache.unlock();
65
            return true;
66
        }
67
    }
68
 
69
    // Here we know that the image is not yet in the cache. So we add it now.
70
    mImgCache.push_back(ic);
71
    MSG_DEBUG("Bitmap \"" << name << "\" was added.");
72
    _imgCache.unlock();
73
    return true;
74
}
75
 
76
bool TImgCache::getBitmap(const string& name, SkBitmap *bm)
77
{
78
    DECL_TRACER("TImgCache::getBitmap(const string& name)");
79
 
80
    if (mImgCache.size() == 0 || !bm)
81
        return false;
82
 
83
    _imgCache.lock();
84
    vector<_IMGCACHE>::iterator iter;
85
 
86
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
87
    {
88
        if (iter->name == name)
89
        {
90
            *bm = iter->bitmap;
91
            MSG_DEBUG("Bitmap \"" << iter->name << "\" was found.");
92
            _imgCache.unlock();
93
            return true;
94
        }
95
    }
96
 
97
    _imgCache.unlock();
98
    return false;
99
}
100
 
101
bool TImgCache::delBitmap(const string& name)
102
{
103
    DECL_TRACER("TImgCache::delBitmap(const string& name)");
104
 
105
    if (name.empty() || mImgCache.size() == 0)
106
        return false;
107
 
108
    _imgCache.lock();
109
    vector<_IMGCACHE>::iterator iter;
110
 
111
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
112
    {
113
        if (iter->name == name)
114
        {
115
            MSG_DEBUG("Bitmap \"" << iter->name << "\" will be erased.");
116
            mImgCache.erase(iter);
117
            _imgCache.unlock();
118
            return true;
119
        }
120
    }
121
 
122
    _imgCache.unlock();
123
    return false;
124
}