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
 
167 andreas 77
bool TImgCache::getBitmap(const string& name, SkBitmap *bm, _IMGCACHE_BMTYPE bmType, int *width, int *height)
97 andreas 78
{
167 andreas 79
    DECL_TRACER("TImgCache::getBitmap(const string& name, _IMGCACHE_BMTYPE bmType, int *width, int *height)");
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;
167 andreas 91
 
92
            if (width && !iter->bitmap.empty())
93
                *width = iter->bitmap.info().width();
94
 
95
            if (height && !iter->bitmap.empty())
96
                *height = iter->bitmap.info().height();
97
 
97 andreas 98
            MSG_DEBUG("Bitmap \"" << iter->name << "\" was found.");
99
            return true;
100
        }
101
    }
102
 
167 andreas 103
    if (width)
104
        *width = 0;
105
 
106
    if (height)
107
        *height = 0;
108
 
97 andreas 109
    return false;
110
}
111
 
165 andreas 112
bool TImgCache::delBitmap(const string& name, _IMGCACHE_BMTYPE bmType)
97 andreas 113
{
165 andreas 114
    DECL_TRACER("TImgCache::delBitmap(const string& name, _IMGCACHE_BMTYPE bmType)");
97 andreas 115
 
116
    if (name.empty() || mImgCache.size() == 0)
117
        return false;
118
 
119
    _imgCache.lock();
120
    vector<_IMGCACHE>::iterator iter;
121
 
122
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
123
    {
165 andreas 124
        if (iter->name == name && iter->bmType == bmType)
97 andreas 125
        {
126
            MSG_DEBUG("Bitmap \"" << iter->name << "\" will be erased.");
127
            mImgCache.erase(iter);
128
            _imgCache.unlock();
129
            return true;
130
        }
131
    }
132
 
133
    _imgCache.unlock();
134
    return false;
135
}
165 andreas 136
 
137
bool TImgCache::existBitmap(const string& name, _IMGCACHE_BMTYPE bmType)
138
{
139
    DECL_TRACER("TImgCache::existBitmap(const string& name, _IMGCACHE_BMTYPE bmType)");
140
 
141
    if (mImgCache.size() == 0)
142
        return false;
143
 
144
    vector<_IMGCACHE>::iterator iter;
145
 
146
    for (iter = mImgCache.begin(); iter != mImgCache.end(); ++iter)
147
    {
148
        if (iter->name == name && iter->bmType == bmType)
149
            return true;
150
    }
151
 
152
    return false;
153
}