diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6ff8618918..1570b6fe95 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -101,7 +101,7 @@ #include #include #include -#include +#include #include #include #include @@ -884,7 +884,7 @@ bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) { DependencyManager::set(NodeType::Agent, listenPort); DependencyManager::set(); DependencyManager::set(); - DependencyManager::set(); + DependencyManager::set(); // ModelFormatRegistry must be defined before ModelCache. See the ModelCache constructor. DependencyManager::set(); DependencyManager::set(); DependencyManager::set(); @@ -2747,9 +2747,9 @@ Application::~Application() { DependencyManager::destroy(); DependencyManager::destroy(); DependencyManager::destroy(); - DependencyManager::destroy(); DependencyManager::destroy(); DependencyManager::destroy(); + DependencyManager::destroy(); DependencyManager::destroy(); DependencyManager::destroy(); DependencyManager::destroy(); diff --git a/libraries/fbx/src/FBXSerializer.cpp b/libraries/fbx/src/FBXSerializer.cpp index ccbda9af33..ac338407f4 100644 --- a/libraries/fbx/src/FBXSerializer.cpp +++ b/libraries/fbx/src/FBXSerializer.cpp @@ -33,7 +33,6 @@ #include #include -#include #include // TOOL: Uncomment the following line to enable the filtering of all the unkwnon fields of a node so we can break point easily while loading a model with problems... @@ -1834,14 +1833,16 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr return hfmModelPtr; } -MediaType getFBXMediaType() { +MediaType FBXSerializer::getMediaType() const { MediaType mediaType("fbx"); mediaType.extensions.push_back("fbx"); mediaType.fileSignatures.emplace_back("Kaydara FBX Binary \x00", 0); return mediaType; } -std::shared_ptr FBXSerializer::FORMAT = std::make_shared>(getFBXMediaType()); +std::unique_ptr FBXSerializer::getFactory() const { + return std::make_unique>(); +} HFMModel::Pointer FBXSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { QBuffer buffer(const_cast(&data)); diff --git a/libraries/fbx/src/FBXSerializer.h b/libraries/fbx/src/FBXSerializer.h index e66a6b911a..a76fb8f9bf 100644 --- a/libraries/fbx/src/FBXSerializer.h +++ b/libraries/fbx/src/FBXSerializer.h @@ -28,7 +28,6 @@ #include "FBX.h" #include -#include #include #include @@ -97,7 +96,8 @@ class ExtractedMesh; class FBXSerializer : public HFMSerializer { public: - static std::shared_ptr FORMAT; + MediaType getMediaType() const override; + std::unique_ptr getFactory() const override; HFMModel* _hfmModel; /// Reads HFMModel from the supplied model and mapping data. diff --git a/libraries/fbx/src/GLTFSerializer.cpp b/libraries/fbx/src/GLTFSerializer.cpp index cfa2124c5d..e254a91eb0 100644 --- a/libraries/fbx/src/GLTFSerializer.cpp +++ b/libraries/fbx/src/GLTFSerializer.cpp @@ -34,7 +34,6 @@ #include #include "FBXSerializer.h" -#include bool GLTFSerializer::getStringVal(const QJsonObject& object, const QString& fieldname, QString& value, QMap& defined) { @@ -906,14 +905,16 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const QUrl& url) { return true; } -MediaType getGLTFMediaType() { +MediaType GLTFSerializer::getMediaType() const { MediaType mediaType("gltf"); mediaType.extensions.push_back("gltf"); mediaType.webMediaTypes.push_back("model/gltf+json"); return mediaType; } -std::shared_ptr GLTFSerializer::FORMAT = std::make_shared>(getGLTFMediaType()); +std::unique_ptr GLTFSerializer::getFactory() const { + return std::make_unique>(); +} HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { diff --git a/libraries/fbx/src/GLTFSerializer.h b/libraries/fbx/src/GLTFSerializer.h index fffb192b5b..5fca77c4fd 100644 --- a/libraries/fbx/src/GLTFSerializer.h +++ b/libraries/fbx/src/GLTFSerializer.h @@ -16,8 +16,6 @@ #include #include #include -#include -#include "FBXSerializer.h" struct GLTFAsset { @@ -704,7 +702,8 @@ struct GLTFFile { class GLTFSerializer : public QObject, public HFMSerializer { Q_OBJECT public: - static std::shared_ptr FORMAT; + MediaType getMediaType() const override; + std::unique_ptr getFactory() const override; HFMModel::Pointer read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url = QUrl()) override; private: diff --git a/libraries/fbx/src/OBJSerializer.cpp b/libraries/fbx/src/OBJSerializer.cpp index b85ca7ebca..9c92466565 100644 --- a/libraries/fbx/src/OBJSerializer.cpp +++ b/libraries/fbx/src/OBJSerializer.cpp @@ -28,7 +28,6 @@ #include #include "FBXSerializer.h" -#include #include #include @@ -652,13 +651,15 @@ done: return result; } -MediaType getOBJMediaType() { +MediaType OBJSerializer::getMediaType() const { MediaType mediaType("obj"); mediaType.extensions.push_back("obj"); return mediaType; } -std::shared_ptr OBJSerializer::FORMAT = std::make_shared>(getOBJMediaType()); +std::unique_ptr OBJSerializer::getFactory() const { + return std::make_unique>(); +} HFMModel::Pointer OBJSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr); diff --git a/libraries/fbx/src/OBJSerializer.h b/libraries/fbx/src/OBJSerializer.h index cbf6ee3ce2..3723b0e569 100644 --- a/libraries/fbx/src/OBJSerializer.h +++ b/libraries/fbx/src/OBJSerializer.h @@ -14,8 +14,6 @@ #include #include -#include -#include "FBXSerializer.h" class OBJTokenizer { public: @@ -93,7 +91,8 @@ public: class OBJSerializer: public QObject, public HFMSerializer { // QObject so we can make network requests. Q_OBJECT public: - static std::shared_ptr FORMAT; + MediaType getMediaType() const override; + std::unique_ptr getFactory() const; typedef QVector FaceGroup; QVector vertices; diff --git a/libraries/hfm/src/hfm/FormatSerializerRegister.cpp b/libraries/hfm/src/hfm/FormatSerializerRegister.cpp new file mode 100644 index 0000000000..09c858b79d --- /dev/null +++ b/libraries/hfm/src/hfm/FormatSerializerRegister.cpp @@ -0,0 +1,29 @@ +// +// FormatSerializerRegister.cpp +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/12/07. +// Copyright 2018 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 "FormatSerializerRegister.h" + +#include "ModelFormatRegistry.h" + +namespace hfm { + +DoFormatSerializerRegister::DoFormatSerializerRegister(const MediaType& mediaType, std::unique_ptr factory) { + auto registry = DependencyManager::get(); + _mediaTypeID = registry->_hfmFormatRegistry.registerMediaType(mediaType, std::move(factory)); +} + +void DoFormatSerializerRegister::unregisterFormat() { + auto registry = DependencyManager::get(); + registry->_hfmFormatRegistry.unregisterMediaType(_mediaTypeID); + _mediaTypeID = hfm::FormatRegistry::INVALID_MEDIA_TYPE_ID; +} + +}; diff --git a/libraries/hfm/src/hfm/FormatSerializerRegister.h b/libraries/hfm/src/hfm/FormatSerializerRegister.h new file mode 100644 index 0000000000..5971d678c8 --- /dev/null +++ b/libraries/hfm/src/hfm/FormatSerializerRegister.h @@ -0,0 +1,35 @@ +// +// FormatSerializerRegister.h +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/11/30. +// Copyright 2018 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 +// + +#ifndef hifi_HFMFormat_h +#define hifi_HFMFormat_h + +#include "HFMFormatRegistry.h" +#include "HFMSerializer.h" + +namespace hfm { + // A helper class which allows early de-registration of a Serializer from ModelFormatRegistry + class FormatSerializerRegister { + public: + virtual void unregisterFormat() = 0; + }; + + class DoFormatSerializerRegister : public FormatSerializerRegister { + public: + DoFormatSerializerRegister(const MediaType& mediaType, std::unique_ptr factory); + + void unregisterFormat() override; + protected: + FormatRegistry::MediaTypeID _mediaTypeID { FormatRegistry::INVALID_MEDIA_TYPE_ID }; + }; +}; + +#endif // hifi_HFMFormat_h diff --git a/libraries/hfm/src/hfm/HFMFormat.h b/libraries/hfm/src/hfm/HFMFormat.h deleted file mode 100644 index 4430bca3f4..0000000000 --- a/libraries/hfm/src/hfm/HFMFormat.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// HFMFormat.h -// libraries/hfm/src/hfm -// -// Created by Sabrina Shanman on 2018/11/30. -// Copyright 2018 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 -// - -#ifndef hifi_HFMFormat_h -#define hifi_HFMFormat_h - -#include "HFMFormatRegistry.h" - -namespace hfm { - class Format { - public: - virtual void registerFormat(FormatRegistry& registry) = 0; - virtual void unregisterFormat(FormatRegistry& registry) = 0; - }; -}; - -#endif // hifi_HFMFormat_h diff --git a/libraries/hfm/src/hfm/HFMFormatRegistry.h b/libraries/hfm/src/hfm/HFMFormatRegistry.h index 203c5f5743..a437e9ac37 100644 --- a/libraries/hfm/src/hfm/HFMFormatRegistry.h +++ b/libraries/hfm/src/hfm/HFMFormatRegistry.h @@ -27,9 +27,10 @@ public: void unregisterMediaType(const MediaTypeID& id); std::shared_ptr getSerializerForMediaType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const; - std::shared_ptr getSerializerForMediaTypeID(MediaTypeID id) const; protected: + std::shared_ptr getSerializerForMediaTypeID(MediaTypeID id) const; + MediaTypeLibrary _mediaTypeLibrary; std::mutex _libraryLock; class SupportedFormat { diff --git a/libraries/hfm/src/hfm/HFMSerializer.h b/libraries/hfm/src/hfm/HFMSerializer.h index a8ff4a3fa0..868ec3dd45 100644 --- a/libraries/hfm/src/hfm/HFMSerializer.h +++ b/libraries/hfm/src/hfm/HFMSerializer.h @@ -15,6 +15,7 @@ #include #include "HFM.h" +#include namespace hfm { @@ -25,6 +26,16 @@ public: virtual std::shared_ptr get() = 0; }; + template + class SimpleFactory : public Factory { + std::shared_ptr get() override { + return std::make_shared(); + } + }; + + virtual MediaType getMediaType() const = 0; + virtual std::unique_ptr getFactory() const = 0; + virtual Model::Pointer read(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url = hifi::URL()) = 0; }; diff --git a/libraries/hfm/src/hfm/ModelFormatRegistry.cpp b/libraries/hfm/src/hfm/ModelFormatRegistry.cpp new file mode 100644 index 0000000000..5520307a9b --- /dev/null +++ b/libraries/hfm/src/hfm/ModelFormatRegistry.cpp @@ -0,0 +1,20 @@ +// +// ModelFormatRegistry.cpp +// libraries/model-networking/src/model-networking +// +// Created by Sabrina Shanman on 2018/11/30. +// Copyright 2018 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 "ModelFormatRegistry.h" + +std::unique_ptr ModelFormatRegistry::addFormat(const hfm::Serializer& serializer) { + return std::make_unique(serializer.getMediaType(), serializer.getFactory()); +} + +std::shared_ptr ModelFormatRegistry::getSerializerForMediaType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const { + return _hfmFormatRegistry.getSerializerForMediaType(data, url, webMediaType); +} diff --git a/libraries/hfm/src/hfm/ModelFormatRegistry.h b/libraries/hfm/src/hfm/ModelFormatRegistry.h new file mode 100644 index 0000000000..4116869390 --- /dev/null +++ b/libraries/hfm/src/hfm/ModelFormatRegistry.h @@ -0,0 +1,30 @@ +// +// ModelFormatRegistry.h +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/11/30. +// Copyright 2018 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 +// + +#ifndef hifi_ModelFormatRegistry_h +#define hifi_ModelFormatRegistry_h + +#include "FormatSerializerRegister.h" +#include "HFMFormatRegistry.h" + +#include + +class ModelFormatRegistry : public Dependency { +public: + std::unique_ptr addFormat(const hfm::Serializer& serializer); + std::shared_ptr getSerializerForMediaType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const; + +protected: + friend class hfm::DoFormatSerializerRegister; + hfm::FormatRegistry _hfmFormatRegistry; +}; + +#endif // hifi_ModelFormatRegistry_h diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp index 8dc483e43b..2c82401ad0 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.cpp +++ b/libraries/model-networking/src/model-networking/ModelCache.cpp @@ -23,6 +23,10 @@ #include "ModelNetworkingLogging.h" #include #include +#include +#include +#include +#include Q_LOGGING_CATEGORY(trace_resource_parse_geometry, "trace.resource.parse.geometry") @@ -308,6 +312,11 @@ ModelCache::ModelCache() { const qint64 GEOMETRY_DEFAULT_UNUSED_MAX_SIZE = DEFAULT_UNUSED_MAX_SIZE; setUnusedResourceCacheSize(GEOMETRY_DEFAULT_UNUSED_MAX_SIZE); setObjectName("ModelCache"); + + auto modelFormatRegistry = DependencyManager::get(); + modelFormatRegistry->addFormat(FBXSerializer()); + modelFormatRegistry->addFormat(OBJSerializer()); + modelFormatRegistry->addFormat(GLTFSerializer()); } QSharedPointer ModelCache::createResource(const QUrl& url, const QSharedPointer& fallback, diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h index 1018bdecd5..5f583d82d9 100644 --- a/libraries/model-networking/src/model-networking/ModelCache.h +++ b/libraries/model-networking/src/model-networking/ModelCache.h @@ -21,6 +21,7 @@ #include "FBXSerializer.h" #include "TextureCache.h" #include "ModelLoader.h" +#include "hfm/FormatSerializerRegister.h" // Alias instead of derive to avoid copying diff --git a/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp b/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp deleted file mode 100644 index 8adc4ea5d9..0000000000 --- a/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// -// ModelFormatRegistry.cpp -// libraries/model-networking/src/model-networking -// -// Created by Sabrina Shanman on 2018/11/30. -// Copyright 2018 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 "ModelFormatRegistry.h" - -#include "FBXSerializer.h" -#include "OBJSerializer.h" -#include "GLTFSerializer.h" - -ModelFormatRegistry::ModelFormatRegistry() : hfm::FormatRegistry() { - addDefaultFormats(); -} - -void ModelFormatRegistry::addDefaultFormats() { - addFormat(FBXSerializer::FORMAT); - addFormat(OBJSerializer::FORMAT); - addFormat(GLTFSerializer::FORMAT); -} - -void ModelFormatRegistry::addFormat(const std::shared_ptr& format) { - format->registerFormat(*this); - { - std::lock_guard lock(_formatsLock); - formats.push_back(format); - } -} - -ModelFormatRegistry::~ModelFormatRegistry() { - for (auto& format : formats) { - format->unregisterFormat(*this); - } - formats.clear(); -} diff --git a/libraries/model-networking/src/model-networking/ModelFormatRegistry.h b/libraries/model-networking/src/model-networking/ModelFormatRegistry.h deleted file mode 100644 index ec1ce79b43..0000000000 --- a/libraries/model-networking/src/model-networking/ModelFormatRegistry.h +++ /dev/null @@ -1,32 +0,0 @@ -// -// ModelFormatRegistry.h -// libraries/model-networking/src/model-networking -// -// Created by Sabrina Shanman on 2018/11/30. -// Copyright 2018 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 -// - -#ifndef hifi_ModelFormatRegistry_h -#define hifi_ModelFormatRegistry_h - -#include -#include - -#include - -class ModelFormatRegistry : public hfm::FormatRegistry, public Dependency { -public: - ModelFormatRegistry(); - ~ModelFormatRegistry(); - void addFormat(const std::shared_ptr& format); - -protected: - void addDefaultFormats(); - std::vector> formats; - std::mutex _formatsLock; -}; - -#endif // hifi_ModelFormatRegistry_h diff --git a/libraries/model-networking/src/model-networking/ModelLoader.cpp b/libraries/model-networking/src/model-networking/ModelLoader.cpp index 1ef8e8ae85..65314633c9 100644 --- a/libraries/model-networking/src/model-networking/ModelLoader.cpp +++ b/libraries/model-networking/src/model-networking/ModelLoader.cpp @@ -12,7 +12,7 @@ #include "ModelLoader.h" #include -#include "ModelFormatRegistry.h" +#include hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const {