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
128128 }
129129 QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);
130130 if (texture.isNull()) {
131− texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));
131+ texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url), &Resource::allReferencesCleared);
132132 texture->setSelf(texture);
133+ texture->setCache(this);
133134 _dilatableNetworkTextures.insert(url, texture);
135+ } else {
136+ _unusedResources.removeOne(texture);
134137 }
135138 return texture;
136139 }
@@ -229,7 +232,7 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) { · 92 unmodified lines
229232
230233 QSharedPointer<Resource> TextureCache::createResource(const QUrl& url,
231234 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);
233236 }
234237
235238 QOpenGLFramebufferObject* TextureCache::createFramebufferObject() {
@@ -352,26 +355,6 @@ DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url) : · 116 unmodified lines
352355 {
353356 }
354357
355−void 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−
375358 QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilation) {
376359 QSharedPointer<Texture> texture = _dilatedTextures.value(dilation);
377360 if (texture.isNull()) {
@@ -400,3 +383,28 @@ QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilatio · 22 unmodified lines
400383 return texture;
401384 }
402385
386+void 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+
406+void DilatableNetworkTexture::reinsert() {
407+ static_cast<TextureCache*>(_cache.data())->_dilatableNetworkTextures.insert(_url,
408+ qWeakPointerCast<NetworkTexture, Resource>(_self));
409+}
410+