History

Working on LRU cache for resources.

Andrzej Kapolka committed 12 years ago

Commit d2f947a1 parent 9d2758e7 files · +103 −25Browse files

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

Unified · Split

interface/src/renderer/TextureCache.cpp+30−22
@@ -128,9 +128,12 @@ QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool no · 127 unmodified lines
128 }128 }
129 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);129 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);
130 if (texture.isNull()) {130 if (texture.isNull()) {
131 texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));131 texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url), &Resource::allReferencesCleared);
132 texture->setSelf(texture);132 texture->setSelf(texture);
133 texture->setCache(this);
133 _dilatableNetworkTextures.insert(url, texture);134 _dilatableNetworkTextures.insert(url, texture);
135 } else {
136 _unusedResources.removeOne(texture);
134 }137 }
135 return texture;138 return texture;
136}139}
@@ -229,7 +232,7 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) { · 92 unmodified lines
229 232
230QSharedPointer<Resource> TextureCache::createResource(const QUrl& url,233QSharedPointer<Resource> TextureCache::createResource(const QUrl& url,
231 const QSharedPointer<Resource>& fallback, bool delayLoad, const void* extra) {234 const QSharedPointer<Resource>& fallback, bool delayLoad, const void* extra) {
232 return QSharedPointer<Resource>(new NetworkTexture(url, *(const bool*)extra));235 return QSharedPointer<Resource>(new NetworkTexture(url, *(const bool*)extra), &Resource::allReferencesCleared);
233}236}
234 237
235QOpenGLFramebufferObject* TextureCache::createFramebufferObject() {238QOpenGLFramebufferObject* TextureCache::createFramebufferObject() {
@@ -352,26 +355,6 @@ DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url) : · 116 unmodified lines
352{355{
353}356}
354 357
355void DilatableNetworkTexture::imageLoaded(const QImage& image) {
356 _image = image;
357
358 // scan out from the center to find inner and outer radii
359 int halfWidth = image.width() / 2;
360 int halfHeight = image.height() / 2;
361 const int BLACK_THRESHOLD = 32;
362 while (_innerRadius < halfWidth && qGray(image.pixel(halfWidth + _innerRadius, halfHeight)) < BLACK_THRESHOLD) {
363 _innerRadius++;
364 }
365 _outerRadius = _innerRadius;
366 const int TRANSPARENT_THRESHOLD = 32;
367 while (_outerRadius < halfWidth && qAlpha(image.pixel(halfWidth + _outerRadius, halfHeight)) > TRANSPARENT_THRESHOLD) {
368 _outerRadius++;
369 }
370
371 // clear out any textures we generated before loading
372 _dilatedTextures.clear();
373}
374
375QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilation) {358QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilation) {
376 QSharedPointer<Texture> texture = _dilatedTextures.value(dilation);359 QSharedPointer<Texture> texture = _dilatedTextures.value(dilation);
377 if (texture.isNull()) {360 if (texture.isNull()) {
@@ -400,3 +383,28 @@ QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilatio · 22 unmodified lines
400 return texture;383 return texture;
401}384}
402 385
386void DilatableNetworkTexture::imageLoaded(const QImage& image) {
387 _image = image;
388
389 // scan out from the center to find inner and outer radii
390 int halfWidth = image.width() / 2;
391 int halfHeight = image.height() / 2;
392 const int BLACK_THRESHOLD = 32;
393 while (_innerRadius < halfWidth && qGray(image.pixel(halfWidth + _innerRadius, halfHeight)) < BLACK_THRESHOLD) {
394 _innerRadius++;
395 }
396 _outerRadius = _innerRadius;
397 const int TRANSPARENT_THRESHOLD = 32;
398 while (_outerRadius < halfWidth && qAlpha(image.pixel(halfWidth + _outerRadius, halfHeight)) > TRANSPARENT_THRESHOLD) {
399 _outerRadius++;
400 }
401
402 // clear out any textures we generated before loading
403 _dilatedTextures.clear();
404}
405
406void DilatableNetworkTexture::reinsert() {
407 static_cast<TextureCache*>(_cache.data())->_dilatableNetworkTextures.insert(_url,
408 qWeakPointerCast<NetworkTexture, Resource>(_self));
409}
410