Attempting to fix threading issues with web entities

This commit is contained in:
Brad Davis 2015-05-13 14:00:41 -07:00
parent a0097bcff8
commit 3b7e8a69b5
5 changed files with 35 additions and 28 deletions

View file

@ -341,6 +341,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
_domainConnectionRefusals(QList<QString>()),
_maxOctreePPS(maxOctreePacketsPerSecond.get())
{
setInstance(this);
#ifdef Q_OS_WIN
installNativeEventFilter(&MyNativeEventFilter::getInstance());
#endif
@ -4694,7 +4695,3 @@ qreal Application::getDevicePixelRatio() {
return _window ? _window->windowHandle()->devicePixelRatio() : 1.0;
}
AbstractViewStateInterface* AbstractViewStateInterface::instance() {
return qApp;
}

View file

@ -33,13 +33,15 @@ EntityItem* RenderableWebEntityItem::factory(const EntityItemID& entityID, const
RenderableWebEntityItem::RenderableWebEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) :
WebEntityItem(entityItemID, properties) {
qDebug() << "Created web entity " << getID();
}
RenderableWebEntityItem::~RenderableWebEntityItem() {
if (_webSurface) {
_webSurface->pause();
_webSurface->disconnect(_connection);
// After the disconnect, ensure that we have the latest
// After the disconnect, ensure that we have the latest texture by acquiring the
// lock used when updating the _texture value
_textureLock.lock();
_textureLock.unlock();
// The lifetime of the QML surface MUST be managed by the main thread
@ -55,6 +57,7 @@ RenderableWebEntityItem::~RenderableWebEntityItem() {
webSurface->deleteLater();
});
}
qDebug() << "Destroyed web entity " << getID();
}
void RenderableWebEntityItem::render(RenderArgs* args) {
@ -66,7 +69,7 @@ void RenderableWebEntityItem::render(RenderArgs* args) {
_webSurface->setBaseUrl(QUrl::fromLocalFile(PathUtils::resourcesPath() + "/qml/"));
_webSurface->load("WebEntity.qml");
_webSurface->resume();
updateQmlSourceUrl();
_webSurface->getRootItem()->setProperty("url", _sourceUrl);
_connection = QObject::connect(_webSurface, &OffscreenQmlSurface::textureUpdated, [&](GLuint textureId) {
_webSurface->lockTexture(textureId);
assert(!glGetError());
@ -135,24 +138,10 @@ void RenderableWebEntityItem::render(RenderArgs* args) {
void RenderableWebEntityItem::setSourceUrl(const QString& value) {
if (_sourceUrl != value) {
_sourceUrl = value;
AbstractViewStateInterface::instance()->postLambdaEvent([this] {
// Update the offscreen display
updateQmlSourceUrl();
});
if (_webSurface) {
AbstractViewStateInterface::instance()->postLambdaEvent([this] {
_webSurface->getRootItem()->setProperty("url", _sourceUrl);
});
}
}
}
void RenderableWebEntityItem::updateQmlSourceUrl() {
if (!_webSurface) {
return;
}
auto webView = _webSurface->getRootItem();
if (!webView) {
return;
}
if (!_sourceUrl.isEmpty()) {
webView->setProperty("url", _sourceUrl);
} else {
webView->setProperty("url", QVariant());
}
}

View file

@ -26,9 +26,7 @@ public:
virtual void setSourceUrl(const QString& value);
private:
void updateQmlSourceUrl();
OffscreenQmlSurface* _webSurface;
OffscreenQmlSurface* _webSurface{ nullptr };
QMetaObject::Connection _connection;
uint32_t _texture{ 0 };
QMutex _textureLock;

View file

@ -0,0 +1,20 @@
//
// Created by Bradley Austin Davis 2015/05/13
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AbstractViewStateInterface.h"
static AbstractViewStateInterface* INSTANCE{nullptr};
AbstractViewStateInterface* AbstractViewStateInterface::instance() {
return INSTANCE;
}
void AbstractViewStateInterface::setInstance(AbstractViewStateInterface* instance) {
INSTANCE = instance;
}

View file

@ -15,6 +15,8 @@
#include <glm/glm.hpp>
#include <functional>
#include <QtGlobal>
class Transform;
class QThread;
class ViewFrustum;
@ -59,6 +61,7 @@ public:
virtual qreal getDevicePixelRatio() = 0;
static AbstractViewStateInterface* instance();
static void setInstance(AbstractViewStateInterface* instance);
};