Glsl: Avoid use of global object pool

Also, use the opportunity to re-organize and clean up a bit.

Change-Id: I09d5635f48d98b450ffd1eb1a0e003a288cf6804
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2018-02-02 15:34:20 +01:00
parent 4e494debff
commit acd63756dc
3 changed files with 76 additions and 121 deletions

View File

@@ -208,14 +208,14 @@ void GlslEditorWidget::updateDocumentNow()
Scope *globalScope = new Namespace(); Scope *globalScope = new Namespace();
doc->_globalScope = globalScope; doc->_globalScope = globalScope;
const GlslEditorPlugin::InitFile *file = GlslEditorPlugin::shaderInit(variant); const GlslEditorPlugin::InitFile *file = GlslEditorPlugin::shaderInit(variant);
sem.translationUnit(file->ast, globalScope, file->engine); sem.translationUnit(file->ast(), globalScope, file->engine());
if (variant & Lexer::Variant_VertexShader) { if (variant & Lexer::Variant_VertexShader) {
file = GlslEditorPlugin::vertexShaderInit(variant); file = GlslEditorPlugin::vertexShaderInit(variant);
sem.translationUnit(file->ast, globalScope, file->engine); sem.translationUnit(file->ast(), globalScope, file->engine());
} }
if (variant & Lexer::Variant_FragmentShader) { if (variant & Lexer::Variant_FragmentShader) {
file = GlslEditorPlugin::fragmentShaderInit(variant); file = GlslEditorPlugin::fragmentShaderInit(variant);
sem.translationUnit(file->ast, globalScope, file->engine); sem.translationUnit(file->ast(), globalScope, file->engine());
} }
sem.translationUnit(ast, globalScope, doc->_engine); sem.translationUnit(ast, globalScope, doc->_engine);

View File

@@ -40,24 +40,10 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/fileiconprovider.h> #include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/id.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>
#include <QAction>
#include <QCoreApplication>
#include <QDebug>
#include <QMenu> #include <QMenu>
#include <QSettings>
#include <QTimer>
#include <QtPlugin>
using namespace Core; using namespace Core;
using namespace TextEditor;
namespace GlslEditor { namespace GlslEditor {
namespace Internal { namespace Internal {
@@ -65,59 +51,67 @@ namespace Internal {
class GlslEditorPluginPrivate class GlslEditorPluginPrivate
{ {
public: public:
GlslEditorPluginPrivate() : GlslEditorPlugin::InitFile m_glsl_120_frag{"glsl_120.frag"};
m_glsl_120_frag(0), GlslEditorPlugin::InitFile m_glsl_120_vert{"glsl_120.vert"};
m_glsl_120_vert(0), GlslEditorPlugin::InitFile m_glsl_120_common{"glsl_120_common.glsl"};
m_glsl_120_common(0), GlslEditorPlugin::InitFile m_glsl_es_100_frag{"glsl_es_100.frag"};
m_glsl_es_100_frag(0), GlslEditorPlugin::InitFile m_glsl_es_100_vert{"glsl_es_100.vert"};
m_glsl_es_100_vert(0), GlslEditorPlugin::InitFile m_glsl_es_100_common{"glsl_es_100_common.glsl"};
m_glsl_es_100_common(0)
{}
~GlslEditorPluginPrivate() GlslEditorFactory editorFactory;
{ GlslCompletionAssistProvider completionAssistProvider;
delete m_glsl_120_frag;
delete m_glsl_120_vert;
delete m_glsl_120_common;
delete m_glsl_es_100_frag;
delete m_glsl_es_100_vert;
delete m_glsl_es_100_common;
}
QPointer<BaseTextEditor> m_currentTextEditable;
GlslEditorPlugin::InitFile *m_glsl_120_frag;
GlslEditorPlugin::InitFile *m_glsl_120_vert;
GlslEditorPlugin::InitFile *m_glsl_120_common;
GlslEditorPlugin::InitFile *m_glsl_es_100_frag;
GlslEditorPlugin::InitFile *m_glsl_es_100_vert;
GlslEditorPlugin::InitFile *m_glsl_es_100_common;
}; };
static GlslEditorPluginPrivate *dd = 0; static GlslEditorPluginPrivate *dd = nullptr;
static GlslEditorPlugin *m_instance = 0;
GlslEditorPlugin::InitFile::InitFile(const QString &fileName)
: m_fileName(fileName)
{}
GlslEditorPlugin::InitFile::~InitFile() GlslEditorPlugin::InitFile::~InitFile()
{ {
delete engine; delete m_engine;
} }
GlslEditorPlugin::GlslEditorPlugin() void GlslEditorPlugin::InitFile::initialize() const
{ {
m_instance = this; // Parse the builtins for any language variant so we can use all keywords.
dd = new GlslEditorPluginPrivate; const int variant = GLSL::Lexer::Variant_All;
QByteArray code;
QFile file(ICore::resourcePath() + "/glsl/" + m_fileName);
if (file.open(QFile::ReadOnly))
code = file.readAll();
m_engine = new GLSL::Engine();
GLSL::Parser parser(m_engine, code.constData(), code.size(), variant);
m_ast = parser.parse();
}
GLSL::TranslationUnitAST *GlslEditorPlugin::InitFile::ast() const
{
if (!m_ast)
initialize();
return m_ast;
}
GLSL::Engine *GlslEditorPlugin::InitFile::engine() const
{
if (!m_engine)
initialize();
return m_engine;
} }
GlslEditorPlugin::~GlslEditorPlugin() GlslEditorPlugin::~GlslEditorPlugin()
{ {
delete dd; delete dd;
m_instance = 0; dd = nullptr;
} }
bool GlslEditorPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage) bool GlslEditorPlugin::initialize(const QStringList &, QString *)
{ {
addAutoReleasedObject(new GlslEditorFactory); dd = new GlslEditorPluginPrivate;
addAutoReleasedObject(new GlslCompletionAssistProvider);
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT); ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
ActionContainer *glslToolsMenu = ActionManager::createMenu(Id(Constants::M_TOOLS_GLSL)); ActionContainer *glslToolsMenu = ActionManager::createMenu(Id(Constants::M_TOOLS_GLSL));
@@ -127,18 +121,14 @@ bool GlslEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
menu->setTitle(tr("GLSL")); menu->setTitle(tr("GLSL"));
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(glslToolsMenu); ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(glslToolsMenu);
Command *cmd = 0;
// Insert marker for "Refactoring" menu: // Insert marker for "Refactoring" menu:
Command *sep = contextMenu->addSeparator(); Command *sep = contextMenu->addSeparator();
sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT)); sep->action()->setObjectName(Constants::M_REFACTORING_MENU_INSERTION_POINT);
contextMenu->addSeparator(); contextMenu->addSeparator();
cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION); Command *cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(cmd); contextMenu->addAction(cmd);
errorMessage->clear();
return true; return true;
} }
@@ -151,62 +141,25 @@ void GlslEditorPlugin::extensionsInitialized()
FileIconProvider::registerIconOverlayForMimeType(":/glsleditor/images/glslfile.png", Constants::GLSL_MIMETYPE_FRAG_ES); FileIconProvider::registerIconOverlayForMimeType(":/glsleditor/images/glslfile.png", Constants::GLSL_MIMETYPE_FRAG_ES);
} }
ExtensionSystem::IPlugin::ShutdownFlag GlslEditorPlugin::aboutToShutdown()
{
// delete GLSL::Icons::instance(); // delete object held by singleton
return IPlugin::aboutToShutdown();
}
static QByteArray glslFile(const QString &fileName)
{
QFile file(ICore::resourcePath() + QLatin1String("/glsl/") + fileName);
if (file.open(QFile::ReadOnly))
return file.readAll();
return QByteArray();
}
static void parseGlslFile(const QString &fileName, GlslEditorPlugin::InitFile *initFile)
{
// Parse the builtins for any langugage variant so we can use all keywords.
const int variant = GLSL::Lexer::Variant_All;
const QByteArray code = glslFile(fileName);
initFile->engine = new GLSL::Engine();
GLSL::Parser parser(initFile->engine, code.constData(), code.size(), variant);
initFile->ast = parser.parse();
}
static GlslEditorPlugin::InitFile *getInitFile(const char *fileName, GlslEditorPlugin::InitFile **initFile)
{
if (*initFile)
return *initFile;
*initFile = new GlslEditorPlugin::InitFile;
parseGlslFile(QLatin1String(fileName), *initFile);
return *initFile;
}
const GlslEditorPlugin::InitFile *GlslEditorPlugin::fragmentShaderInit(int variant) const GlslEditorPlugin::InitFile *GlslEditorPlugin::fragmentShaderInit(int variant)
{ {
if (variant & GLSL::Lexer::Variant_GLSL_120) return (variant & GLSL::Lexer::Variant_GLSL_120)
return getInitFile("glsl_120.frag", &dd->m_glsl_120_frag); ? &dd->m_glsl_120_frag
else : &dd->m_glsl_es_100_frag;
return getInitFile("glsl_es_100.frag", &dd->m_glsl_es_100_frag);
} }
const GlslEditorPlugin::InitFile *GlslEditorPlugin::vertexShaderInit(int variant) const GlslEditorPlugin::InitFile *GlslEditorPlugin::vertexShaderInit(int variant)
{ {
if (variant & GLSL::Lexer::Variant_GLSL_120) return (variant & GLSL::Lexer::Variant_GLSL_120)
return getInitFile("glsl_120.vert", &dd->m_glsl_120_vert); ? &dd->m_glsl_120_vert
else : &dd->m_glsl_es_100_vert;
return getInitFile("glsl_es_100.vert", &dd->m_glsl_es_100_vert);
} }
const GlslEditorPlugin::InitFile *GlslEditorPlugin::shaderInit(int variant) const GlslEditorPlugin::InitFile *GlslEditorPlugin::shaderInit(int variant)
{ {
if (variant & GLSL::Lexer::Variant_GLSL_120) return (variant & GLSL::Lexer::Variant_GLSL_120)
return getInitFile("glsl_120_common.glsl", &dd->m_glsl_120_common); ? &dd->m_glsl_120_common
else : &dd->m_glsl_es_100_common;
return getInitFile("glsl_es_100_common.glsl", &dd->m_glsl_es_100_common);
} }
} // namespace Internal } // namespace Internal

View File

@@ -31,37 +31,39 @@
namespace GlslEditor { namespace GlslEditor {
namespace Internal { namespace Internal {
class GlslEditorWidget;
class GlslEditorPlugin : public ExtensionSystem::IPlugin class GlslEditorPlugin : public ExtensionSystem::IPlugin
{ {
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "GLSLEditor.json") Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "GLSLEditor.json")
public: public:
GlslEditorPlugin(); GlslEditorPlugin() = default;
~GlslEditorPlugin(); ~GlslEditorPlugin() final;
// IPlugin class InitFile
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
void extensionsInitialized();
ShutdownFlag aboutToShutdown();
struct InitFile
{ {
InitFile(GLSL::Engine *engine = 0, GLSL::TranslationUnitAST *ast = 0) public:
: engine(engine), ast(ast) explicit InitFile(const QString &m_fileName);
{}
~InitFile(); ~InitFile();
GLSL::Engine *engine; GLSL::Engine *engine() const;
GLSL::TranslationUnitAST *ast; GLSL::TranslationUnitAST *ast() const;
private:
void initialize() const;
QString m_fileName;
mutable GLSL::Engine *m_engine = nullptr;
mutable GLSL::TranslationUnitAST *m_ast = nullptr;
}; };
static const InitFile *fragmentShaderInit(int variant); static const InitFile *fragmentShaderInit(int variant);
static const InitFile *vertexShaderInit(int variant); static const InitFile *vertexShaderInit(int variant);
static const InitFile *shaderInit(int variant); static const InitFile *shaderInit(int variant);
private:
bool initialize(const QStringList &arguments, QString *errorMessage) final;
void extensionsInitialized() final;
}; };
} // namespace Internal } // namespace Internal