-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.cpp
151 lines (131 loc) · 4.36 KB
/
loader.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
149
150
151
/*
This file is part of Presenter,
copyright (c) 2013--2014 Jiří Župka.
Presenter 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.
Presenter 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 Presenter. If not, see <http://www.gnu.org/licenses/>.
*/
#include "loader.h"
#include <QDebug>
Loader::Loader(int max_texture_size, int cache_size, QSize img_size,QObject * parent):
QThread(parent)
{
supported_formats = CacheImage::supported_formats;
this->max_cache_size = cache_size;
this->cache_size = cache_size;
this->pos = 0;
this->max_texture_size = max_texture_size;
this->images_count = 0;
this->load_pos_start = 0;
this->load_pos_end = 0;
this->quit = false;
this->img_size = img_size;
}
Loader::~Loader(){
this->quit = true;
this->pos++;
this->wait_set.wakeAll();
this->wait(30);
for (int i = 0; i < images.size(); ++i){
delete this->images[i];
}
images.clear();
}
void Loader::start(){
QThread::start(Priority::LowestPriority);
}
void Loader::run(){
int _pos = this->pos;
while (!quit){
this->wait_set_mux.lock();
if (_pos == this->pos){
wait_set.wait(&this->wait_set_mux);
_pos = this->pos;
}else{
_pos = this->pos;
}
this->wait_set_mux.unlock();
if (quit){
return;
}
this->update_range(_pos);
this->clean_cache();
this->images[_pos]->load();
this->new_image_loaded++;
int qd, qu;
//Load Prevs
for (int i = _pos-1,j = _pos+1; i >= this->load_pos_start && j <= this->load_pos_end; ++j, --i){
qd = (i + this->images_count) % this->images_count;
qu = (j + this->images_count) % this->images_count;
if (_pos != this->pos) break;
this->images[qu]->load();
this->new_image_loaded++;
if (_pos != this->pos) break;
this->images[qd]->load();
this->new_image_loaded++;
}
}
}
void Loader::clean_cache(){
this->clean_image_mux.lock();
QSet<int> to_delete;
for (int i = 0; i < this->images_count; i++){
to_delete.insert(i);
}
int q = 0;
for (int i = this->load_pos_start; i <= this->load_pos_end; ++i){
q = (i + this->images_count) % this->images_count;
while (q < 0){
q = (q + images_count) % images_count;
}
to_delete.remove(q);
}
foreach (int to_del, to_delete) {
if (this->images[to_del]->is_loaded()){
delete this->images[to_del];
this->images[to_del] = 0;
CacheImage* image;
if (this->img_size.isValid()){
image = new Thumbnail(this->files[to_del], this->max_texture_size, this->img_size);
}else{
image = new BigImage(this->files[to_del], this->max_texture_size);
}
this->images[to_del] = image;
}
}
this->clean_image_mux.unlock();
}
void Loader::update_range(int pos){
this->load_pos_end = pos + this->cache_size/2;
this->load_pos_start = pos - this->cache_size/2;
//qDebug("new range %d %d", this->load_pos_start, this->load_pos_end);
}
void Loader::set(int pos){
//qDebug() << "update pos:" << pos;
this->pos = pos;
this->wait_set.wakeAll();
}
int* Loader::newImageLoaded(){
return &this->new_image_loaded;
}
void Loader::setFiles(QStringList files){
this->files = files;
this->images_count = files.size();
this->cache_size = this->images_count > this->max_cache_size ? this->max_cache_size : this->images_count;
for (int i = 0; i < this->images_count; ++i){
CacheImage* image;
if (this->img_size.isValid()){
image = new Thumbnail(files[i], this->max_texture_size, this->img_size);
}else{
image = new BigImage(files[i], this->max_texture_size);
}
images.append(image);
}
}