add shader plugin reg

This commit is contained in:
王劲鹏 2021-02-04 14:55:36 +08:00 committed by osborn
parent a64e8037d6
commit 99afe83b19
17 changed files with 178 additions and 11 deletions

View File

@ -60,3 +60,14 @@ InterfaceDriver* Context::getDriver()
}
return driver;
}
QObject* Context::obtainPlugin(QString name)
{
if (mPluginMap.keys().contains(name)) {
return mPluginMap.value(name);
} else {
QObject *plugin = getDriver()->getRegistry()->pluginInfoMap.createObject(name);
mPluginMap.insert(name, plugin);
return plugin;
}
}

View File

@ -10,6 +10,7 @@ class Context
{
private:
QString mContextId;
QMap<QString, QObject*> mPluginMap;
RootNode *mRootNode;
QString source;
QString script;
@ -29,6 +30,8 @@ public:
void callEntity(QString methodName, QVariantList args);
InterfaceDriver* getDriver();
QObject* obtainPlugin(QString name);
};
#endif // CONTEXT_H

View File

@ -8,3 +8,8 @@ Context *ContextManager::createContext(QString script, QString source, QString e
context->getDriver()->createContext(QString::number(contextId), script, source);
return context;
}
Context *ContextManager::getContext(QString contextId)
{
return contextMap->take(contextId);
}

View File

@ -11,12 +11,10 @@ private:
static ContextManager *local_instance;
ContextManager()
{
qDebug() << "ContextManager create";
}
~ContextManager()
{
qDebug() << "ContextManager destroy";
}
QAtomicInt *counter = new QAtomicInt();
@ -30,6 +28,8 @@ public:
}
Context *createContext(QString script, QString source, QString extra);
Context *getContext(QString contextId);
};
#endif // CONTEXTMANAGER_H

View File

@ -29,6 +29,8 @@ SOURCES += \
main.cpp \
native_driver.cpp \
panel.cpp \
plugin/shader_plugin.cpp \
registry.cpp \
shader/root_node.cpp \
utils/constant.cpp
@ -64,8 +66,11 @@ HEADERS += \
interface_driver.h \
native_driver.h \
panel.h \
plugin/shader_plugin.h \
registry.h \
shader/root_node.h \
template/singleton.h \
utils/constant.h \
utils/count_down_latch.h \
utils/object_factory.h \
utils/utils.h

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-02-01T17:32:31. -->
<!-- Written by QtCreator 4.11.1, 2021-02-04T14:31:29. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -1,5 +1,8 @@
#include <QDebug>
#include <QMetaObject>
#include "bridge_extension.h"
#include "../context_manager.h"
BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent)
{
@ -8,6 +11,17 @@ BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent)
void BridgeExtension::callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue)
{
Context *context = ContextManager::getInstance()->getContext(contextId);
bool classRegistered = context->getDriver()->getRegistry()->acquirePluginInfo(module);
if (classRegistered) {
QObject *plugin = context->obtainPlugin(module);
QMetaObject::invokeMethod(
plugin,
methodName.toStdString().c_str(),
Qt::DirectConnection, QGenericReturnArgument(),
Q_ARG(QJSValue, jsValue), Q_ARG(QString, callbackId));
qDebug() << plugin;
}
qDebug() << "contextId: " + contextId;
qDebug() << "module: " + module;
qDebug() << "methodName: " + methodName;

View File

@ -79,6 +79,11 @@ JSEngine::JSEngine(QObject *parent) : QObject(parent)
});
}
void JSEngine::prepareContext(QString contextId, QString script, QString source)
{
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
}
QJSValue JSEngine::invokeDoricMethod(QString method, QVariantList arguments)
{
return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments);
@ -90,11 +95,6 @@ void JSEngine::loadBuiltinJS(QString assetName)
QString result = mJSE->loadJS(script, "Assets://" + assetName);
}
void JSEngine::prepareContext(QString contextId, QString script, QString source)
{
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
}
QString JSEngine::packageContextScript(QString contextId, QString content)
{
return QString(Constant::TEMPLATE_CONTEXT_CREATE).replace("%s1", content).replace("%s2", contextId).replace("%s3", contextId);
@ -105,6 +105,11 @@ QString JSEngine::packageModuleScript(QString moduleName, QString content)
return QString(Constant::TEMPLATE_MODULE).replace("%s1", moduleName).replace("%s2", content);
}
Registry *JSEngine::getRegistry()
{
return this->mRegistry;
}
JSEngine::~JSEngine()
{

View File

@ -5,12 +5,14 @@
#include <QThreadPool>
#include "interface_jse.h"
#include "../registry.h"
class JSEngine : public QObject
{
Q_OBJECT
private:
InterfaceJSE *mJSE;
Registry *mRegistry = new Registry();
void loadBuiltinJS(QString assetName);
QString packageContextScript(QString contextId, QString content);
@ -22,8 +24,9 @@ public:
~JSEngine();
QJSValue invokeDoricMethod(QString method, QVariantList arguments);
void prepareContext(QString contextId, QString script, QString source);
QJSValue invokeDoricMethod(QString method, QVariantList arguments);
Registry *getRegistry();
};
#endif // JSENGINE_H

View File

@ -4,6 +4,8 @@
#include <QString>
#include <QVariant>
#include "registry.h"
class InterfaceDriver
{
public:
@ -14,6 +16,8 @@ public:
virtual void createContext(QString contextId, QString script, QString source) = 0;
virtual void destroyContext(QString contextId) = 0;
virtual Registry* getRegistry() = 0;
};
#endif // INTERFACEDRIVER_H

View File

@ -29,3 +29,8 @@ void NativeDriver::destroyContext(QString contextId)
{
}
Registry *NativeDriver::getRegistry()
{
return this->jsEngine.getRegistry();
}

View File

@ -12,12 +12,10 @@ private:
static NativeDriver *local_instance;
NativeDriver()
{
qDebug() << "constructor";
}
~NativeDriver()
{
qDebug() << "destructor";
}
JSEngine jsEngine;
@ -36,5 +34,7 @@ public:
void createContext(QString contextId, QString script, QString source) override;
void destroyContext(QString contextId) override;
Registry * getRegistry() override;
};
#endif // NATIVEDRIVER_H

View File

@ -0,0 +1,13 @@
#include "shader_plugin.h"
#include <QDebug>
ShaderPlugin::ShaderPlugin(QObject* parent) : QObject (parent)
{
}
void ShaderPlugin::render(QJSValue jsValue, QString callbackId)
{
qDebug() << "";
}

View File

@ -0,0 +1,16 @@
#ifndef SHADERPLUGIN_H
#define SHADERPLUGIN_H
#include <QJSValue>
#include <QObject>
class ShaderPlugin : public QObject
{
Q_OBJECT
public:
ShaderPlugin(QObject* parent);
Q_INVOKABLE void render(QJSValue jsValue, QString callbackId);
};
#endif // SHADERPLUGIN_H

View File

@ -0,0 +1,13 @@
#include "registry.h"
#include "plugin/shader_plugin.h"
Registry::Registry()
{
registerNativePlugin<ShaderPlugin>("shader");
}
bool Registry::acquirePluginInfo(QString name)
{
return pluginInfoMap.acquireClass(name);
}

24
doric-Qt/doric/registry.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef REGISTRY_H
#define REGISTRY_H
#include <QString>
#include "utils/object_factory.h"
class Registry
{
public:
ObjectFactory pluginInfoMap;
Registry();
template<typename T>
void registerNativePlugin(QString name)
{
pluginInfoMap.registerClass<T>(name);
}
bool acquirePluginInfo(QString name);
};
#endif // REGISTRY_H

View File

@ -0,0 +1,46 @@
#include <QByteArray>
#include <QMetaObject>
#include <QHash>
#ifndef OBJECTFACTORY_H
#define OBJECTFACTORY_H
class ObjectFactory
{
public:
template<typename T>
static void registerClass(QString name)
{
constructors().insert(name, &constructorHelper<T>);
}
static bool acquireClass(QString name)
{
return constructors().keys().contains(name);
}
static QObject* createObject(const QString& name, QObject* parent = NULL)
{
Constructor constructor = constructors().value(name);
if ( constructor == NULL )
return NULL;
return (*constructor)(parent);
}
private:
typedef QObject* (*Constructor)(QObject* parent);
template<typename T>
static QObject* constructorHelper(QObject* parent)
{
return new T(parent);
}
static QHash<QString, Constructor>& constructors()
{
static QHash<QString, Constructor> instance;
return instance;
}
};
#endif // OBJECTFACTORY_H