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
11#include <QGLWidget>11#include <QGLWidget>
12#include <QNetworkReply>12#include <QNetworkReply>
13#include <QOpenGLFramebufferObject>13#include <QOpenGLFramebufferObject>
14#include <QRunnable>
15#include <QThreadPool>
14 16
15#include <glm/gtc/random.hpp>17#include <glm/gtc/random.hpp>
16 18
@@ -129,6 +131,7 @@ QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool no · 112 unmodified lines
129 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);131 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);
130 if (texture.isNull()) {132 if (texture.isNull()) {
131 texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));133 texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));
134 texture->setSelf(texture);
132 _dilatableNetworkTextures.insert(url, texture);135 _dilatableNetworkTextures.insert(url, texture);
133 }136 }
134 return texture;137 return texture;
@@ -267,8 +270,30 @@ NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) : · 132 unmodified lines
267 glBindTexture(GL_TEXTURE_2D, 0);270 glBindTexture(GL_TEXTURE_2D, 0);
268}271}
269 272
270void NetworkTexture::downloadFinished(QNetworkReply* reply) {273class ImageReader : public QRunnable {
271 QImage image = QImage::fromData(reply->readAll());274public:
275
276 ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data);
277
278 virtual void run();
279
280private:
281
282 QWeakPointer<Resource> _texture;
283 QByteArray _data;
284};
285
286ImageReader::ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data) :
287 _texture(texture),
288 _data(data) {
289}
290
291void ImageReader::run() {
292 QSharedPointer<Resource> texture = _texture.toStrongRef();
293 if (texture.isNull()) {
294 return;
295 }
296 QImage image = QImage::fromData(_data);
272 if (image.format() != QImage::Format_ARGB32) {297 if (image.format() != QImage::Format_ARGB32) {
273 image = image.convertToFormat(QImage::Format_ARGB32);298 image = image.convertToFormat(QImage::Format_ARGB32);
274 }299 }
@@ -292,8 +317,19 @@ void NetworkTexture::downloadFinished(QNetworkReply* reply) { · 17 unmodified lines
292 }317 }
293 }318 }
294 int imageArea = image.width() * image.height();319 int imageArea = image.width() * image.height();
295 _averageColor = accumulated / (imageArea * EIGHT_BIT_MAXIMUM);320 QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image),
296 _translucent = (translucentPixels >= imageArea / 2);321 Q_ARG(const glm::vec4&, accumulated / (imageArea * EIGHT_BIT_MAXIMUM)),
322 Q_ARG(bool, translucentPixels >= imageArea / 2));
323}
324
325void NetworkTexture::downloadFinished(QNetworkReply* reply) {
326 // send the reader off to the thread pool
327 QThreadPool::globalInstance()->start(new ImageReader(_self, reply->readAll()));
328}
329
330void NetworkTexture::setImage(const QImage& image, const glm::vec4& averageColor, bool translucent) {
331 _averageColor = averageColor;
332 _translucent = translucent;
297 333
298 finishedLoading(true);334 finishedLoading(true);
299 imageLoaded(image);335 imageLoaded(image);