2011-02-22 12:08:19 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-22 12:08:19 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
|
|
|
**
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
#include "imagecontainer.h"
|
|
|
|
|
2012-08-30 17:10:42 +02:00
|
|
|
#include <QSharedMemory>
|
|
|
|
#include <QCache>
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
2012-08-30 17:10:42 +02:00
|
|
|
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
ImageContainer::ImageContainer()
|
|
|
|
: m_instanceId(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageContainer::ImageContainer(qint32 instanceId, const QImage &image)
|
2012-08-30 17:10:42 +02:00
|
|
|
: m_image(image),
|
|
|
|
m_instanceId(instanceId)
|
2010-11-30 21:32:52 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 ImageContainer::instanceId() const
|
|
|
|
{
|
|
|
|
return m_instanceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage ImageContainer::image() const
|
|
|
|
{
|
|
|
|
return m_image;
|
|
|
|
}
|
|
|
|
|
2012-08-30 17:10:42 +02:00
|
|
|
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
|
|
|
|
{
|
|
|
|
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
|
|
|
|
if (sharedMemory == 0)
|
|
|
|
sharedMemory = new QSharedMemory(QString::number(key));
|
|
|
|
|
|
|
|
if (sharedMemory->isAttached())
|
|
|
|
sharedMemory->detach();
|
|
|
|
|
|
|
|
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
|
|
|
|
|
|
|
|
if (sharedMemoryIsCreated) {
|
|
|
|
globalSharedMemoryCache.insert(key, sharedMemory);
|
|
|
|
return sharedMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
|
|
|
|
{
|
|
|
|
out << container.instanceId();
|
|
|
|
|
|
|
|
const QImage image = container.image();
|
|
|
|
const QByteArray data(reinterpret_cast<const char*>(image.constBits()), image.byteCount());
|
|
|
|
|
|
|
|
out << qint32(image.bytesPerLine());
|
|
|
|
out << image.size();
|
|
|
|
out << qint32(image.format());
|
|
|
|
out << qint32(image.byteCount());
|
2012-08-30 17:10:42 +02:00
|
|
|
|
|
|
|
QSharedMemory *sharedMemory = createSharedMemory(container.instanceId(), image.byteCount());
|
|
|
|
|
|
|
|
out << qint32(sharedMemory != 0); // send if shared memory is used
|
|
|
|
|
|
|
|
if (sharedMemory) {
|
|
|
|
qMemCopy(sharedMemory->data(), image.constBits(), image.byteCount());
|
|
|
|
} else {
|
|
|
|
out.writeRawData(reinterpret_cast<const char*>(image.constBits()), image.byteCount());
|
|
|
|
}
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-08-30 17:10:42 +02:00
|
|
|
void readSharedMemory(qint32 key, QImage *image, qint32 byteSize)
|
|
|
|
{
|
|
|
|
QSharedMemory sharedMemory(QString::number(key));
|
|
|
|
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
|
|
|
|
if (canAttach)
|
|
|
|
{
|
|
|
|
qMemCopy(image->bits(), sharedMemory.constData(), byteSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 21:32:52 +01:00
|
|
|
QDataStream &operator>>(QDataStream &in, ImageContainer &container)
|
|
|
|
{
|
|
|
|
|
|
|
|
qint32 byteSize;
|
|
|
|
qint32 bytesPerLine;
|
|
|
|
QSize imageSize;
|
|
|
|
qint32 format;
|
2012-08-30 17:10:42 +02:00
|
|
|
qint32 sharedmemoryIsUsed;
|
2010-11-30 21:32:52 +01:00
|
|
|
|
|
|
|
in >> container.m_instanceId;
|
|
|
|
|
|
|
|
in >> bytesPerLine;
|
|
|
|
in >> imageSize;
|
|
|
|
in >> format;
|
|
|
|
in >> byteSize;
|
2012-08-30 17:10:42 +02:00
|
|
|
in >> sharedmemoryIsUsed;
|
2010-11-30 21:32:52 +01:00
|
|
|
|
|
|
|
container.m_image = QImage(imageSize, QImage::Format(format));
|
2012-08-30 17:10:42 +02:00
|
|
|
|
|
|
|
if (sharedmemoryIsUsed)
|
|
|
|
readSharedMemory(container.instanceId(), &container.m_image, byteSize);
|
|
|
|
else
|
|
|
|
in.readRawData(reinterpret_cast<char*>(container.m_image.bits()), byteSize);
|
2010-11-30 21:32:52 +01:00
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|