From 3b7e8a69b53f16bed41a0452316caea963389f97 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 13 May 2015 14:00:41 -0700 Subject: [PATCH] Attempting to fix threading issues with web entities --- interface/src/Application.cpp | 5 +-- .../src/RenderableWebEntityItem.cpp | 31 ++++++------------- .../src/RenderableWebEntityItem.h | 4 +-- .../src/AbstractViewStateInterface.cpp | 20 ++++++++++++ .../src/AbstractViewStateInterface.h | 3 ++ 5 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 libraries/render-utils/src/AbstractViewStateInterface.cpp diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 71ca66e97a..d9e2780672 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -341,6 +341,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : _domainConnectionRefusals(QList()), _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; -} diff --git a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp index 4d2c3ee543..fe85a37762 100644 --- a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp @@ -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()); - } -} \ No newline at end of file diff --git a/libraries/entities-renderer/src/RenderableWebEntityItem.h b/libraries/entities-renderer/src/RenderableWebEntityItem.h index cc5e74b4ab..cf63d7915e 100644 --- a/libraries/entities-renderer/src/RenderableWebEntityItem.h +++ b/libraries/entities-renderer/src/RenderableWebEntityItem.h @@ -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; diff --git a/libraries/render-utils/src/AbstractViewStateInterface.cpp b/libraries/render-utils/src/AbstractViewStateInterface.cpp new file mode 100644 index 0000000000..bd4ba8b1b6 --- /dev/null +++ b/libraries/render-utils/src/AbstractViewStateInterface.cpp @@ -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; +} + diff --git a/libraries/render-utils/src/AbstractViewStateInterface.h b/libraries/render-utils/src/AbstractViewStateInterface.h index 0a3cb55848..a1447293b7 100644 --- a/libraries/render-utils/src/AbstractViewStateInterface.h +++ b/libraries/render-utils/src/AbstractViewStateInterface.h @@ -15,6 +15,8 @@ #include #include +#include + class Transform; class QThread; class ViewFrustum; @@ -59,6 +61,7 @@ public: virtual qreal getDevicePixelRatio() = 0; static AbstractViewStateInterface* instance(); + static void setInstance(AbstractViewStateInterface* instance); };