From df4857947b154cadc1523f4aec5f298db352a00e Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 20 Apr 2016 17:16:59 -0700 Subject: [PATCH] Add prefetch overload for texture specificity --- .../src/model-networking/TextureCache.cpp | 12 ++++++++++++ .../src/model-networking/TextureCache.h | 7 +++++-- libraries/networking/src/ResourceCache.cpp | 4 ++-- libraries/networking/src/ResourceCache.h | 4 +++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 191f911120..af84ecd0f3 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -35,6 +35,12 @@ TextureCache::TextureCache() { const qint64 TEXTURE_DEFAULT_UNUSED_MAX_SIZE = DEFAULT_UNUSED_MAX_SIZE; setUnusedResourceCacheSize(TEXTURE_DEFAULT_UNUSED_MAX_SIZE); setObjectName("TextureCache"); + + // Expose enum Type to JS/QML via properties + auto metaEnum = QMetaEnum::fromType(); + for (int i = 0; i < metaEnum.keyCount(); ++i) { + setProperty(metaEnum.key(i), metaEnum.value(i)); + } } TextureCache::~TextureCache() { @@ -149,6 +155,12 @@ public: const QByteArray& content; }; +ScriptableResource* TextureCache::prefetch(const QUrl& url, int type) { + auto byteArray = QByteArray(); + TextureExtra extra = { (Type)type, byteArray }; + return ResourceCache::prefetch(url, &extra); +} + NetworkTexturePointer TextureCache::getTexture(const QUrl& url, Type type, const QByteArray& content) { TextureExtra extra = { type, content }; return ResourceCache::getResource(url, QUrl(), content.isEmpty(), &extra).staticCast(); diff --git a/libraries/model-networking/src/model-networking/TextureCache.h b/libraries/model-networking/src/model-networking/TextureCache.h index ebdc2002c5..fced2b3c5c 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.h +++ b/libraries/model-networking/src/model-networking/TextureCache.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,6 @@ public: }; /// A texture loaded from the network. - class NetworkTexture : public Resource, public Texture { Q_OBJECT @@ -98,8 +98,11 @@ class TextureCache : public ResourceCache, public Dependency { SINGLETON_DEPENDENCY using Type = NetworkTexture::Type; - + public: + // Overload ResourceCache::prefetch to allow specifying texture type for loads + Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url, int type); + /// Returns the ID of the permutation/normal texture used for Perlin noise shader programs. This texture /// has two lines: the first, a set of random numbers in [0, 255] to be used as permutation offsets, and /// the second, a set of random unit vectors to be used as noise gradients. diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 7feeff114e..f6b79e3acb 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -140,7 +140,7 @@ void ScriptableResource::finished(bool success) { emit loadedChanged(_isLoaded); } -ScriptableResource* ResourceCache::prefetch(const QUrl& url) { +ScriptableResource* ResourceCache::prefetch(const QUrl& url, void* extra) { auto result = new ScriptableResource(); if (QThread::currentThread() != thread()) { @@ -151,7 +151,7 @@ ScriptableResource* ResourceCache::prefetch(const QUrl& url) { } - auto resource = getResource(url); + auto resource = getResource(url, QUrl(), false, extra); result->_resource = resource; result->setObjectName(url.toString()); diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h index 3cd749ebc9..d678196ea3 100644 --- a/libraries/networking/src/ResourceCache.h +++ b/libraries/networking/src/ResourceCache.h @@ -131,7 +131,7 @@ public: Q_INVOKABLE QVariantList getResourceList(); // This must be exposed as a ptr so the ScriptEngine may take ownership - Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url); + Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url) { return prefetch(url, nullptr); } static void setRequestLimit(int limit); static int getRequestLimit() { return _requestLimit; } @@ -164,6 +164,8 @@ private slots: void clearATPAssets(); protected: + ScriptableResource* prefetch(const QUrl& url, void* extra); + /// Loads a resource from the specified URL. /// \param fallback a fallback URL to load if the desired one is unavailable /// \param delayLoad if true, don't load the resource immediately; wait until load is first requested