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
|
#include "threadedChainImageLoader.h"
#include <sstream>
threadedChainImageLoader::threadedChainImageLoader(){
nextID = 0;
ofAddListener(ofEvents().update, this, &threadedChainImageLoader::update);
ofAddListener(ofURLResponseEvent(),this,&threadedChainImageLoader::urlResponse);
startThread();
lastUpdate = 0;
}
threadedChainImageLoader::~threadedChainImageLoader(){
images_to_load_from_disk.close();
images_to_update.close();
waitForThread(true);
ofRemoveListener(ofEvents().update, this, &threadedChainImageLoader::update);
ofRemoveListener(ofURLResponseEvent(),this,&threadedChainImageLoader::urlResponse);
}
// Load an image from disk.
//--------------------------------------------------------------
void threadedChainImageLoader::loadFromDisk(chainImage& image, string filename) {
nextID++;
chainImageLoaderEntry entry(image);
entry.filename = filename;
entry.image->setUseTexture(false);
entry.name = filename;
images_to_load_from_disk.send(entry);
}
// Load an url asynchronously from an url.
//--------------------------------------------------------------
void threadedChainImageLoader::loadFromURL(chainImage& image, string url) {
nextID++;
chainImageLoaderEntry entry(image);
entry.url = url;
entry.image->setUseTexture(false);
entry.name = "image" + ofToString(nextID);
images_async_loading[entry.name] = entry;
ofLoadURLAsync(entry.url, entry.name);
}
// Reads from the queue and loads new images.
//--------------------------------------------------------------
void threadedChainImageLoader::threadedFunction() {
setThreadName("threadedChainImageLoader " + ofToString(thread.get_id()));
chainImageLoaderEntry entry;
while( images_to_load_from_disk.receive(entry) ) {
if(entry.image->load(entry.filename) ) {
images_to_update.send(entry);
}else{
ofLogError("threadedChainImageLoader") << "couldn't load file: \"" << entry.filename << "\"";
}
}
ofLogVerbose("threadedChainImageLoader") << "finishing thread on closed queue";
}
// When we receive an url response this method is called;
// The loaded image is removed from the async_queue and added to the
// update queue. The update queue is used to update the texture.
//--------------------------------------------------------------
void threadedChainImageLoader::urlResponse(ofHttpResponse & response) {
// this happens in the update thread so no need to lock to access
// images_async_loading
entry_iterator it = images_async_loading.find(response.request.name);
if(response.status == 200) {
if(it != images_async_loading.end()) {
it->second.image->load(response.data);
images_to_update.send(it->second);
}
}else{
// log error.
ofLogError("threadedChainImageLoader") << "couldn't load url, response status: " << response.status;
ofRemoveURLRequest(response.request.getID());
}
// remove the entry from the queue
if(it != images_async_loading.end()) {
images_async_loading.erase(it);
}
}
// Check the update queue and update the texture
//--------------------------------------------------------------
void threadedChainImageLoader::update(ofEventArgs & a){
// Load 1 image per update so we don't block the gl thread for too long
chainImageLoaderEntry entry;
if (images_to_update.tryReceive(entry)) {
entry.image->setUseTexture(true);
entry.image->update();
}
}
|