Report ScriptableResource memory cost to engine

This commit is contained in:
Zach Pomerantz 2016-04-20 18:08:16 -07:00
parent 5ec82fa43d
commit 1a0a623d5f
4 changed files with 37 additions and 11 deletions

View file

@ -128,6 +128,13 @@ void ScriptableResource::release() {
_resource.reset();
}
void ScriptableResource::updateMemoryCost(const QObject* engine) {
if (_resource && !_resource->isInScript()) {
_resource->setInScript(true);
connect(_resource.data(), SIGNAL(updateSize(qint64)), engine, SLOT(updateMemoryCost(qint64)));
}
}
void ScriptableResource::finished(bool success) {
disconnectHelper();
@ -329,6 +336,7 @@ QSharedPointer<Resource> ResourceCache::getResource(const QUrl& url, const QUrl&
getResource(fallback, QUrl(), true) : QSharedPointer<Resource>(), delayLoad, extra);
resource->setSelf(resource);
resource->setCache(this);
connect(resource.data(), &Resource::updateSize, this, &ResourceCache::updateTotalSize);
{
QWriteLocker locker(&_resourcesLock);
_resources.insert(url, resource);
@ -414,8 +422,8 @@ void ResourceCache::removeResource(const QUrl& url, qint64 size) {
_totalResourcesSize -= size;
}
void ResourceCache::updateTotalSize(const qint64& oldSize, const qint64& newSize) {
_totalResourcesSize += (newSize - oldSize);
void ResourceCache::updateTotalSize(const qint64& deltaSize) {
_totalResourcesSize += deltaSize;
emit dirty();
}
@ -592,7 +600,7 @@ void Resource::finishedLoading(bool success) {
}
void Resource::setSize(const qint64& bytes) {
QMetaObject::invokeMethod(_cache.data(), "updateTotalSize", Q_ARG(qint64, _bytes), Q_ARG(qint64, bytes));
emit updateSize(bytes - _bytes);
_bytes = bytes;
}

View file

@ -95,6 +95,9 @@ public:
bool isLoaded() const { return _isLoaded; }
bool isFailed() const { return _isFailed; }
// Connects to a SLOT(updateMemoryCost(qint64) on the given engine
void updateMemoryCost(const QObject* engine);
signals:
void progressChanged(uint64_t bytesReceived, uint64_t bytesTotal);
void loadedChanged(bool loaded); // analogous to &Resource::finished
@ -166,7 +169,7 @@ public slots:
void checkAsynchronousGets();
protected slots:
void updateTotalSize(const qint64& oldSize, const qint64& newSize);
void updateTotalSize(const qint64& deltaSize);
private slots:
void clearATPAssets();
@ -291,6 +294,9 @@ signals:
/// Fired when the resource is refreshed.
void onRefresh();
/// Fired when the size changes (through setSize).
void updateSize(qint64 deltaSize);
protected slots:
void attemptRequest();
@ -332,17 +338,21 @@ private:
void makeRequest();
void retry();
void reinsert();
bool isInScript() const { return _isInScript; }
void setInScript(bool isInScript) { _isInScript = isInScript; }
friend class ResourceCache;
friend class ScriptableResource;
ResourceRequest* _request = nullptr;
int _lruKey = 0;
QTimer* _replyTimer = nullptr;
qint64 _bytesReceived = 0;
qint64 _bytesTotal = 0;
qint64 _bytes = 0;
int _attempts = 0;
ResourceRequest* _request{ nullptr };
int _lruKey{ 0 };
QTimer* _replyTimer{ nullptr };
qint64 _bytesReceived{ 0 };
qint64 _bytesTotal{ 0 };
qint64 _bytes{ 0 };
int _attempts{ 0 };
bool _isInScript{ false };
};
uint qHash(const QPointer<QObject>& value, uint seed = 0);

View file

@ -274,6 +274,7 @@ static void resultHandlerFromScriptValue(const QScriptValue& value, AnimVariantR
using ScriptableResourceRawPtr = ScriptableResource*;
static QScriptValue scriptableResourceToScriptValue(QScriptEngine* engine, const ScriptableResourceRawPtr& resource) {
resource->updateMemoryCost(engine);
auto object = engine->newQObject(
const_cast<ScriptableResourceRawPtr>(resource),
QScriptEngine::ScriptOwnership);
@ -809,6 +810,12 @@ void ScriptEngine::callAnimationStateHandler(QScriptValue callback, AnimVariantM
}
}
void ScriptEngine::updateMemoryCost(const qint64& deltaSize) {
if (deltaSize > 0) {
reportAdditionalMemoryCost(deltaSize);
}
}
void ScriptEngine::timerFired() {
QTimer* callingTimer = reinterpret_cast<QTimer*>(sender());
CallbackData timerData = _timerFunctionMap.value(callingTimer);

View file

@ -158,6 +158,7 @@ public:
public slots:
void callAnimationStateHandler(QScriptValue callback, AnimVariantMap parameters, QStringList names, bool useNames, AnimVariantResultHandler resultHandler);
void updateMemoryCost(const qint64&);
signals:
void scriptLoaded(const QString& scriptFilename);