Commit ce9b7bf1 authored by Marco Bubke's avatar Marco Bubke

QmlDesigner.NodeInstances: Shared memory for value changes

Change-Id: I3c3455f411f18322a062f144e98461691d12ada8
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@nokia.com>
parent 505c90db
......@@ -22,7 +22,7 @@ HEADERS += $$PWD/removepropertiescommand.h
HEADERS += $$PWD/reparentinstancescommand.h
HEADERS += $$PWD/valueschangedcommand.h
HEADERS += $$PWD/changeauxiliarycommand.h
HEADERS += $$PWD/removesharedmemorycommand.h
SOURCES += $$PWD/synchronizecommand.cpp
SOURCES += $$PWD/tokencommand.cpp
......@@ -46,3 +46,4 @@ SOURCES += $$PWD/createinstancescommand.cpp
SOURCES += $$PWD/createscenecommand.cpp
SOURCES += $$PWD/pixmapchangedcommand.cpp
SOURCES += $$PWD/changeauxiliarycommand.cpp
SOURCES += $$PWD/removesharedmemorycommand.cpp
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "removesharedmemorycommand.h"
namespace QmlDesigner {
RemoveSharedMemoryCommand::RemoveSharedMemoryCommand()
{
}
RemoveSharedMemoryCommand::RemoveSharedMemoryCommand(const QString &typeName, const QVector<qint32> &keyNumberVector)
: m_typeName(typeName),
m_keyNumberVector(keyNumberVector)
{
}
QString RemoveSharedMemoryCommand::typeName() const
{
return m_typeName;
}
QVector<qint32> RemoveSharedMemoryCommand::keyNumbers() const
{
return m_keyNumberVector;
}
QDataStream &operator<<(QDataStream &out, const RemoveSharedMemoryCommand &command)
{
out << command.typeName();
out << command.keyNumbers();
return out;
}
QDataStream &operator>>(QDataStream &in, RemoveSharedMemoryCommand &command)
{
in >> command.m_typeName;
in >> command.m_keyNumberVector;
return in;
}
} // namespace QmlDesigner
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef QMLDESIGNER_REMOVESHAREDMEMORYCOMMAND_H
#define QMLDESIGNER_REMOVESHAREDMEMORYCOMMAND_H
#include <QMetaType>
#include <QString>
#include <QVector>
namespace QmlDesigner {
class RemoveSharedMemoryCommand
{
friend QDataStream &operator>>(QDataStream &in, RemoveSharedMemoryCommand &command);
public:
RemoveSharedMemoryCommand();
RemoveSharedMemoryCommand(const QString &typeName, const QVector<qint32> &keyNumberVector);
QString typeName() const;
QVector<qint32> keyNumbers() const;
private:
QString m_typeName;
QVector<qint32> m_keyNumberVector;
};
QDataStream &operator<<(QDataStream &out, const RemoveSharedMemoryCommand &command);
QDataStream &operator>>(QDataStream &in, RemoveSharedMemoryCommand &command);
} // namespace QmlDesigner
Q_DECLARE_METATYPE(QmlDesigner::RemoveSharedMemoryCommand)
#endif // QMLDESIGNER_REMOVESHAREDMEMORYCOMMAND_H
......@@ -30,14 +30,21 @@
#include "valueschangedcommand.h"
#include <QSharedMemory>
#include <QCache>
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
ValuesChangedCommand::ValuesChangedCommand()
: m_keyNumber(0)
{
}
ValuesChangedCommand::ValuesChangedCommand(const QVector<PropertyValueContainer> &valueChangeVector)
: m_valueChangeVector (valueChangeVector)
: m_valueChangeVector (valueChangeVector),
m_keyNumber(0)
{
}
......@@ -46,17 +53,87 @@ QVector<PropertyValueContainer> ValuesChangedCommand::valueChanges() const
return m_valueChangeVector;
}
quint32 ValuesChangedCommand::keyNumber() const
{
return m_keyNumber;
}
void ValuesChangedCommand::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
static const QLatin1String valueKeyTemplateString("Values-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (!sharedMemoryIsCreated) {
if (sharedMemory->isAttached())
sharedMemory->attach();
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
return sharedMemory;
}
return 0;
}
QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
{
if (command.valueChanges().count() > 5) {
static quint32 keyCounter = 0;
++keyCounter;
command.m_keyNumber = keyCounter;
QByteArray outDataStreamByteArray;
QDataStream temporaryOutDataStream(&outDataStreamByteArray, QIODevice::WriteOnly);
temporaryOutDataStream << command.valueChanges();;
QSharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
if (sharedMemory) {
qMemCopy(sharedMemory->data(), outDataStreamByteArray.constData(), sharedMemory->size());
out << command.keyNumber();
return out;
}
}
out << qint32(0);
out << command.valueChanges();
return out;
}
void readSharedMemory(qint32 key, QVector<PropertyValueContainer> *valueChangeVector)
{
QSharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
if (canAttach) {
QDataStream in(QByteArray::fromRawData(static_cast<const char*>(sharedMemory.constData()), sharedMemory.size()));
in >> *valueChangeVector;
}
}
QDataStream &operator>>(QDataStream &in, ValuesChangedCommand &command)
{
in >> command.m_valueChangeVector;
in >> command.m_keyNumber;
if (command.keyNumber() > 0) {
readSharedMemory(command.keyNumber(), &command.m_valueChangeVector);
} else {
in >> command.m_valueChangeVector;
}
return in;
}
......
......@@ -40,6 +40,7 @@ namespace QmlDesigner {
class ValuesChangedCommand
{
friend QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command);
friend QDataStream &operator>>(QDataStream &in, ValuesChangedCommand &command);
public:
......@@ -47,9 +48,13 @@ public:
ValuesChangedCommand(const QVector<PropertyValueContainer> &valueChangeVector);
QVector<PropertyValueContainer> valueChanges() const;
quint32 keyNumber() const;
static void removeSharedMemorys(const QVector<qint32> &keyNumberVector);
private:
QVector<PropertyValueContainer> m_valueChangeVector;
mutable quint32 m_keyNumber;
};
QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command);
......
......@@ -33,18 +33,25 @@
#include <QSharedMemory>
#include <QCache>
#define QTC_ASSERT_STRINGIFY_HELPER(x) #x
#define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x)
#define QTC_ASSERT_STRING(cond) qDebug("SOFT ASSERT: \"" cond"\" in file " __FILE__ ", line " QTC_ASSERT_STRINGIFY(__LINE__))
#define QTC_ASSERT(cond, action) if (cond) {} else { QTC_ASSERT_STRING(#cond); action; } do {} while (0)
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
ImageContainer::ImageContainer()
: m_instanceId(-1)
: m_instanceId(-1),
m_keyNumber(-2)
{
}
ImageContainer::ImageContainer(qint32 instanceId, const QImage &image)
ImageContainer::ImageContainer(qint32 instanceId, const QImage &image, qint32 keyNumber)
: m_image(image),
m_instanceId(instanceId)
m_instanceId(instanceId),
m_keyNumber(keyNumber)
{
}
......@@ -58,16 +65,46 @@ QImage ImageContainer::image() const
return m_image;
}
qint32 ImageContainer::keyNumber() const
{
return m_keyNumber;
}
void ImageContainer::setImage(const QImage &image)
{
QTC_ASSERT(m_image.isNull(), /**/);
m_image = image;
}
void ImageContainer::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
static const QLatin1String imageKeyTemplateString("Image-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
if (sharedMemory == 0)
sharedMemory = new QSharedMemory(QString("Image-%1").arg(key));
sharedMemory = new QSharedMemory(QString(imageKeyTemplateString).arg(key));
if (sharedMemory->isAttached())
sharedMemory->detach();
bool sharedMemoryIsCreated = sharedMemory->isAttached();
if (!sharedMemoryIsCreated)
sharedMemoryIsCreated = sharedMemory->attach();
bool sharedMemorySizeIsSmallerThanByteCount = sharedMemory->size() < byteCount;
bool sharedMemorySizeIsDoubleBiggerThanByteCount = sharedMemory->size() * 2 > byteCount;
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (!sharedMemoryIsCreated || sharedMemorySizeIsSmallerThanByteCount || sharedMemorySizeIsDoubleBiggerThanByteCount) {
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
......@@ -77,69 +114,111 @@ static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
return 0;
}
QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
static void writeSharedMemory(QSharedMemory *sharedMemory, const QImage &image)
{
out << container.instanceId();
sharedMemory->lock();
const QImage image = container.image();
const QByteArray data(reinterpret_cast<const char*>(image.constBits()), image.byteCount());
qint32 headerData[5];
headerData[0] = image.byteCount();
headerData[1] = image.bytesPerLine();
headerData[2] = image.size().width();
headerData[3] = image.size().height();
headerData[4] = image.format();
qMemCopy(sharedMemory->data(), headerData, 20);
qMemCopy(reinterpret_cast<char*>(sharedMemory->data()) + 20, image.constBits(), image.byteCount());
sharedMemory->unlock();
}
static void writeStream(QDataStream &out, const QImage &image)
{
out << qint32(image.bytesPerLine());
out << image.size();
out << qint32(image.format());
out << qint32(image.byteCount());
out.writeRawData(reinterpret_cast<const char*>(image.constBits()), image.byteCount());
}
QSharedMemory *sharedMemory = createSharedMemory(container.instanceId(), image.byteCount());
QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
{
const int extraDataSize = 20;
out << qint32(sharedMemory != 0); // send if shared memory is used
out << container.instanceId();
out << container.keyNumber();
if (sharedMemory) {
sharedMemory->lock();
qMemCopy(sharedMemory->data(), image.constBits(), image.byteCount());
sharedMemory->unlock();
} else {
out.writeRawData(reinterpret_cast<const char*>(image.constBits()), image.byteCount());
}
const QImage image = container.image();
QSharedMemory *sharedMemory = createSharedMemory(container.keyNumber(), image.byteCount() + extraDataSize);
out << qint32(sharedMemory != 0); // send if shared memory is used
if (sharedMemory)
writeSharedMemory(sharedMemory, image);
else
writeStream(out, image);
return out;
}
void readSharedMemory(qint32 key, QImage *image, qint32 byteSize)
static void readSharedMemory(qint32 key, ImageContainer &container)
{
QSharedMemory sharedMemory(QString("Image-%1").arg(key));
QSharedMemory sharedMemory(QString(imageKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
if (canAttach)
if (canAttach && sharedMemory.size() >= 20)
{
sharedMemory.lock();
qMemCopy(image->bits(), sharedMemory.constData(), byteSize);
qint32 headerData[5];
qMemCopy(headerData, sharedMemory.constData(), 20);
qint32 byteCount = headerData[0];
// qint32 bytesPerLine = headerData[1];
qint32 imageWidth = headerData[2];
qint32 imageHeight = headerData[3];
qint32 imageFormat = headerData[4];
QImage image = QImage(imageWidth, imageHeight, QImage::Format(imageFormat));
qMemCopy(image.bits(), reinterpret_cast<const qint32*>(sharedMemory.constData()) + 5, byteCount);
container.setImage(image);
sharedMemory.unlock();
}
}
QDataStream &operator>>(QDataStream &in, ImageContainer &container)
static void readStream(QDataStream &in, ImageContainer &container)
{
qint32 byteSize;
qint32 byteCount;
qint32 bytesPerLine;
QSize imageSize;
qint32 format;
qint32 sharedmemoryIsUsed;
in >> container.m_instanceId;
qint32 imageFormat;
in >> bytesPerLine;
in >> imageSize;
in >> format;
in >> byteSize;
in >> sharedmemoryIsUsed;
in >> imageFormat;
in >> byteCount;
container.m_image = QImage(imageSize, QImage::Format(format));
QImage image = QImage(imageSize, QImage::Format(imageFormat));
if (sharedmemoryIsUsed)
readSharedMemory(container.instanceId(), &container.m_image, byteSize);
else
in.readRawData(reinterpret_cast<char*>(container.m_image.bits()), byteSize);
in.readRawData(reinterpret_cast<char*>(image.bits()), byteCount);
container.setImage(image);
}
QDataStream &operator>>(QDataStream &in, ImageContainer &container)
{
qint32 sharedMemoryIsUsed;
in >> container.m_instanceId;
in >> container.m_keyNumber;
in >> sharedMemoryIsUsed;
if (sharedMemoryIsUsed) {
readSharedMemory(container.keyNumber(), container);
} else
readStream(in, container);
return in;
}
......
......@@ -41,14 +41,20 @@ class ImageContainer
friend QDataStream &operator>>(QDataStream &in, ImageContainer &container);
public:
ImageContainer();
ImageContainer(qint32 instanceId, const QImage &image);
ImageContainer(qint32 instanceId, const QImage &image, qint32 keyNumber);
qint32 instanceId() const;
QImage image() const;
qint32 keyNumber() const;
void setImage(const QImage &image);
static void removeSharedMemorys(const QVector<qint32> &keyNumberVector);
private:
QImage m_image;
qint32 m_instanceId;
qint32 m_keyNumber;
};
QDataStream &operator<<(QDataStream &out, const ImageContainer &container);
......
......@@ -55,6 +55,7 @@
#include "changestatecommand.h"
#include "completecomponentcommand.h"
#include "synchronizecommand.h"
#include "removesharedmemorycommand.h"
#include "tokencommand.h"
#include "informationchangedcommand.h"
......@@ -274,6 +275,11 @@ void NodeInstanceClientProxy::changeNodeSource(const ChangeNodeSourceCommand &co
{
nodeInstanceServer()->changeNodeSource(command);
}
void NodeInstanceClientProxy::removeSharedMemory(const RemoveSharedMemoryCommand &command)
{
nodeInstanceServer()->removeSharedMemory(command);
}
void NodeInstanceClientProxy::redirectToken(const TokenCommand &command)
{
nodeInstanceServer()->token(command);
......@@ -296,6 +302,7 @@ void NodeInstanceClientProxy::dispatchCommand(const QVariant &command)
static const int completeComponentCommandType = QMetaType::type("CompleteComponentCommand");
static const int synchronizeCommandType = QMetaType::type("SynchronizeCommand");
static const int changeNodeSourceCommandType = QMetaType::type("ChangeNodeSourceCommand");
static const int removeSharedMemoryCommandType = QMetaType::type("RemoveSharedMemoryCommand");
static const int tokenCommandType = QMetaType::type("TokenCommand");
if (command.userType() == createInstancesCommandType) {
......@@ -326,6 +333,8 @@ void NodeInstanceClientProxy::dispatchCommand(const QVariant &command)
completeComponent(command.value<CompleteComponentCommand>());
else if (command.userType() == changeNodeSourceCommandType)
changeNodeSource(command.value<ChangeNodeSourceCommand>());
else if (command.userType() == removeSharedMemoryCommandType)
removeSharedMemory(command.value<RemoveSharedMemoryCommand>());
else if (command.userType() == tokenCommandType)
redirectToken(command.value<TokenCommand>());
else if (command.userType() == synchronizeCommandType) {
......
......@@ -100,6 +100,7 @@ protected:
void changeState(const ChangeStateCommand &command);
void completeComponent(const CompleteComponentCommand &command);
void changeNodeSource(const ChangeNodeSourceCommand &command);
void removeSharedMemory(const RemoveSharedMemoryCommand &command);
void redirectToken(const TokenCommand &command);
private slots:
......
......@@ -74,6 +74,7 @@
#include "createscenecommand.h"
#include "changenodesourcecommand.h"
#include "tokencommand.h"
#include "removesharedmemorycommand.h"
#include "dummycontextobject.h"
......@@ -343,6 +344,10 @@ void NodeInstanceServer::token(const TokenCommand &/*command*/)
}
void NodeInstanceServer::removeSharedMemory(const RemoveSharedMemoryCommand &/*command*/)
{
}
void NodeInstanceServer::setupImports(const QVector<AddImportContainer> &containerVector)
{
foreach (const AddImportContainer &container, containerVector) {
......@@ -1056,7 +1061,7 @@ PixmapChangedCommand NodeInstanceServer::createPixmapChangedCommand(const QList<
foreach (const ServerNodeInstance &instance, instanceList) {
if (instance.isValid() && instance.hasContent())
imageVector.append(ImageContainer(instance.instanceId(), instance.renderImage()));