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
 
165 andreas 39
bool TImgCache::addImage(const string& name, SkBitmap& bm, _IMGCACHE_BMTYPE bmType)
97 andreas 40
{
165 andreas 41
    DECL_TRACER("TImgCache::addImage(const string& name, SkBitmap& bm, _IMGCACHE_BMTYPE bmType)");
97 andreas 42
 
43
    _imgCache.lock();
44
    _IMGCACHE ic;
45
 
46
    ic.name = name;
165 andreas 47
    ic.bmType = bmType;
97 andreas 48
    ic.bitmap = bm;
49
 
50
    if (mImgCache.size() == 0)
51
    {
52
        mImgCache.push_back(ic);
53
        MSG_DEBUG("Bitmap \"" << name << "\" was freshly added.");
54
        _imgCache.unlock();
55
        return true;
56
    }
57
 
58
    vector<_IMGCACHE>::iterator iter;
59
 
60
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
61
    {
62
        if (iter->name == name)
63
        {
64
            MSG_DEBUG("Bitmap \"" << name << "\" already in cache.");
65
            _imgCache.unlock();
66
            return true;
67
        }
68
    }
69
 
70
    // Here we know that the image is not yet in the cache. So we add it now.
71
    mImgCache.push_back(ic);
72
    MSG_DEBUG("Bitmap \"" << name << "\" was added.");
73
    _imgCache.unlock();
74
    return true;
75
}
76
 
165 andreas 77
bool TImgCache::getBitmap(const string& name, SkBitmap *bm, _IMGCACHE_BMTYPE bmType)
97 andreas 78
{
165 andreas 79
    DECL_TRACER("TImgCache::getBitmap(const string& name, _IMGCACHE_BMTYPE bmType)");
97 andreas 80
 
81
    if (mImgCache.size() == 0 || !bm)
82
        return false;
83
 
84
    vector<_IMGCACHE>::iterator iter;
85
 
86
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
87
    {
165 andreas 88
        if (iter->name == name && iter->bmType == bmType)
97 andreas 89
        {
90
            *bm = iter->bitmap;
91
            MSG_DEBUG("Bitmap \"" << iter->name << "\" was found.");
92
            return true;
93
        }
94
    }
95
 
96
    return false;
97
}
98
 
165 andreas 99
bool TImgCache::delBitmap(const string& name, _IMGCACHE_BMTYPE bmType)
97 andreas 100
{
165 andreas 101
    DECL_TRACER("TImgCache::delBitmap(const string& name, _IMGCACHE_BMTYPE bmType)");
97 andreas 102
 
103
    if (name.empty() || mImgCache.size() == 0)
104
        return false;
105
 
106
    _imgCache.lock();
107
    vector<_IMGCACHE>::iterator iter;
108
 
109
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
110
    {
165 andreas 111
        if (iter->name == name && iter->bmType == bmType)
97 andreas 112
        {
113
            MSG_DEBUG("Bitmap \"" << iter->name << "\" will be erased.");
114
            mImgCache.erase(iter);
115
            _imgCache.unlock();
116
            return true;
117
        }
118
    }
119
 
120
    _imgCache.unlock();
121
    return false;
122
}
165 andreas 123
 
124
bool TImgCache::existBitmap(const string& name, _IMGCACHE_BMTYPE bmType)
125
{
126
    DECL_TRACER("TImgCache::existBitmap(const string& name, _IMGCACHE_BMTYPE bmType)");
127
 
128
    if (mImgCache.size() == 0)
129
        return false;
130
 
131
    vector<_IMGCACHE>::iterator iter;
132
 
133
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
134
    {
135
        if (iter->name == name && iter->bmType == bmType)
136
            return true;
137
    }
138
 
139
    return false;
140
}