History

Process geometry/textures in worker threads (via QThreadPool). Closes #2137.

Andrzej Kapolka committed 12 years ago

Commit 2f27d8e1 parent 0bb375210 files · +131 −13Browse files

Showing the changes to interface/src/renderer/TextureCache.cpp (1 of 10 files). Show all

Unified · Split

interface/src/renderer/TextureCache.cpp+40−4
@@ -11,6 +11,8 @@ · 10 unmodified lines
1111 #include <QGLWidget>
1212 #include <QNetworkReply>
1313 #include <QOpenGLFramebufferObject>
14+#include <QRunnable>
15+#include <QThreadPool>
1416
1517 #include <glm/gtc/random.hpp>
1618
@@ -129,6 +131,7 @@ QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool no · 112 unmodified lines
129131 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);
130132 if (texture.isNull()) {
131133 texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));
134+ texture->setSelf(texture);
132135 _dilatableNetworkTextures.insert(url, texture);
133136 }
134137 return texture;
@@ -267,8 +270,30 @@ NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) : · 132 unmodified lines
267270 glBindTexture(GL_TEXTURE_2D, 0);
268271 }
269272
270−void NetworkTexture::downloadFinished(QNetworkReply* reply) {
271− QImage image = QImage::fromData(reply->readAll());
273+class ImageReader : public QRunnable {
274+public:
275+
276+ ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data);
277+
278+ virtual void run();
279+
280+private:
281+
282+ QWeakPointer<Resource> _texture;
283+ QByteArray _data;
284+};
285+
286+ImageReader::ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data) :
287+ _texture(texture),
288+ _data(data) {
289+}
290+
291+void ImageReader::run() {
292+ QSharedPointer<Resource> texture = _texture.toStrongRef();
293+ if (texture.isNull()) {
294+ return;
295+ }
296+ QImage image = QImage::fromData(_data);
272297 if (image.format() != QImage::Format_ARGB32) {
273298 image = image.convertToFormat(QImage::Format_ARGB32);
274299 }
@@ -292,8 +317,19 @@ void NetworkTexture::downloadFinished(QNetworkReply* reply) { · 17 unmodified lines
292317 }
293318 }
294319 int imageArea = image.width() * image.height();
295− _averageColor = accumulated / (imageArea * EIGHT_BIT_MAXIMUM);
296− _translucent = (translucentPixels >= imageArea / 2);
320+ QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image),
321+ Q_ARG(const glm::vec4&, accumulated / (imageArea * EIGHT_BIT_MAXIMUM)),
322+ Q_ARG(bool, translucentPixels >= imageArea / 2));
323+}
324+
325+void NetworkTexture::downloadFinished(QNetworkReply* reply) {
326+ // send the reader off to the thread pool
327+ QThreadPool::globalInstance()->start(new ImageReader(_self, reply->readAll()));
328+}
329+
330+void NetworkTexture::setImage(const QImage& image, const glm::vec4& averageColor, bool translucent) {
331+ _averageColor = averageColor;
332+ _translucent = translucent;
297333
298334 finishedLoading(true);
299335 imageLoaded(image);