diff options
Diffstat (limited to 'src/imports/sharedimage')
-rw-r--r-- | src/imports/sharedimage/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/imports/sharedimage/plugin.cpp | 7 | ||||
-rw-r--r-- | src/imports/sharedimage/qsharedimageloader.cpp | 266 | ||||
-rw-r--r-- | src/imports/sharedimage/qsharedimageloader_p.h | 81 | ||||
-rw-r--r-- | src/imports/sharedimage/sharedimageprovider.cpp | 160 | ||||
-rw-r--r-- | src/imports/sharedimage/sharedimageprovider.h | 59 |
6 files changed, 5 insertions, 578 deletions
diff --git a/src/imports/sharedimage/CMakeLists.txt b/src/imports/sharedimage/CMakeLists.txt index 90020be7ee..70f3abb76e 100644 --- a/src/imports/sharedimage/CMakeLists.txt +++ b/src/imports/sharedimage/CMakeLists.txt @@ -9,19 +9,13 @@ qt_internal_add_qml_module(sharedimageplugin VERSION "${CMAKE_PROJECT_VERSION}" CLASSNAME QtQuickSharedImagePlugin SKIP_TYPE_REGISTRATION - GENERATE_QMLTYPES - INSTALL_QMLTYPES + PLUGIN_OPTIONAL SOURCES plugin.cpp - qsharedimageloader.cpp qsharedimageloader_p.h - sharedimageprovider.cpp sharedimageprovider.h PUBLIC_LIBRARIES Qt::Core - Qt::CorePrivate - Qt::Gui - Qt::GuiPrivate Qt::Qml - Qt::QuickPrivate + Qt::LabsSharedImagePrivate ) #### Keys ignored in scope 1:.:.:sharedimage.pro:<TRUE>: diff --git a/src/imports/sharedimage/plugin.cpp b/src/imports/sharedimage/plugin.cpp index 3245635b2d..dab04efd92 100644 --- a/src/imports/sharedimage/plugin.cpp +++ b/src/imports/sharedimage/plugin.cpp @@ -37,10 +37,11 @@ ** ****************************************************************************/ +#include <QtLabsSharedImage/private/qtlabssharedimageglobal_p.h> +#include <QtLabsSharedImage/private/qsharedimageprovider_p.h> + #include <qqmlextensionplugin.h> #include <qqmlengine.h> -#include <sharedimageprovider.h> - /*! \qmlmodule Qt.labs.sharedimage 1 @@ -99,8 +100,6 @@ The shared image module does not provide any directly usable QML types. */ -extern void qml_register_types_Qt_labs_sharedimage(); - QT_BEGIN_NAMESPACE class QtQuickSharedImagePlugin : public QQmlEngineExtensionPlugin diff --git a/src/imports/sharedimage/qsharedimageloader.cpp b/src/imports/sharedimage/qsharedimageloader.cpp deleted file mode 100644 index c9e3ef3eb3..0000000000 --- a/src/imports/sharedimage/qsharedimageloader.cpp +++ /dev/null @@ -1,266 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qsharedimageloader_p.h" -#include <private/qobject_p.h> -#include <private/qimage_p.h> -#include <QSharedMemory> - - -QT_BEGIN_NAMESPACE - -Q_LOGGING_CATEGORY(lcSharedImage, "qt.quick.sharedimage"); - -struct SharedImageHeader { - quint8 magic; - quint8 version; - quint16 offset; - qint32 width; - qint32 height; - qint32 bpl; - QImage::Format format; -}; -Q_STATIC_ASSERT(sizeof(SharedImageHeader) % 4 == 0); - -#if QT_CONFIG(sharedmemory) -struct SharedImageInfo { - QString path; - QPointer<QSharedMemory> shmp; -}; - -void cleanupSharedImage(void *cleanupInfo) -{ - if (!cleanupInfo) - return; - SharedImageInfo *sii = static_cast<SharedImageInfo *>(cleanupInfo); - qCDebug(lcSharedImage) << "Cleanup called for" << sii->path; - if (sii->shmp.isNull()) { - qCDebug(lcSharedImage) << "shm is 0 for" << sii->path; - return; - } - QSharedMemory *shm = sii->shmp.data(); - sii->shmp.clear(); - delete shm; // destructor detaches - delete sii; -} -#else -void cleanupSharedImage(void *) {} -#endif - -class QSharedImageLoaderPrivate : public QObjectPrivate -{ - Q_DECLARE_PUBLIC(QSharedImageLoader) - -public: - QSharedImageLoaderPrivate() {} - - QImage load(const QString &path, QSharedImageLoader::ImageParameters *params); - - void storeImageToMem(void *data, const QImage &img); - - bool verifyMem(const void *data, int size); - - QImage createImageFromMem(const void *data, void *cleanupInfo); - -}; - - -void QSharedImageLoaderPrivate::storeImageToMem(void *data, const QImage &img) -{ - Q_ASSERT(data && !img.isNull()); - - SharedImageHeader *h = static_cast<SharedImageHeader *>(data); - h->magic = 'Q'; - h->version = 1; - h->offset = sizeof(SharedImageHeader); - h->width = img.width(); - h->height = img.height(); - h->bpl = img.bytesPerLine(); - h->format = img.format(); - - uchar *p = static_cast<uchar *>(data) + sizeof(SharedImageHeader); - memcpy(p, img.constBits(), img.sizeInBytes()); -} - - -bool QSharedImageLoaderPrivate::verifyMem(const void *data, int size) -{ - if (!data || size < int(sizeof(SharedImageHeader))) - return false; - - const SharedImageHeader *h = static_cast<const SharedImageHeader *>(data); - if ((h->magic != 'Q') - || (h->version < 1) - || (h->offset < sizeof(SharedImageHeader)) - || (h->width <= 0) - || (h->height <= 0) - || (h->bpl <= 0) - || (h->format <= QImage::Format_Invalid) - || (h->format >= QImage::NImageFormats)) { - return false; - } - - int availSize = size - h->offset; - if (h->height * h->bpl > availSize) - return false; - if ((qt_depthForFormat(h->format) * h->width * h->height) > (8 * availSize)) - return false; - - return true; -} - - -QImage QSharedImageLoaderPrivate::createImageFromMem(const void *data, void *cleanupInfo) -{ - const SharedImageHeader *h = static_cast<const SharedImageHeader *>(data); - const uchar *p = static_cast<const uchar *>(data) + h->offset; - - QImage img(p, h->width, h->height, h->bpl, h->format, cleanupSharedImage, cleanupInfo); - return img; -} - - -QImage QSharedImageLoaderPrivate::load(const QString &path, QSharedImageLoader::ImageParameters *params) -{ -#if QT_CONFIG(sharedmemory) - Q_Q(QSharedImageLoader); - - QImage nil; - if (path.isEmpty()) - return nil; - - QScopedPointer<QSharedMemory> shm(new QSharedMemory(q->key(path, params))); - bool locked = false; - - if (!shm->attach(QSharedMemory::ReadOnly)) { - QImage img = q->loadFile(path, params); - if (img.isNull()) - return nil; - size_t size = sizeof(SharedImageHeader) + img.sizeInBytes(); - if (size > size_t(std::numeric_limits<int>::max())) { - qCDebug(lcSharedImage) << "Image" << path << "to large to load"; - return nil; - } else if (shm->create(int(size))) { - qCDebug(lcSharedImage) << "Created new shm segment of size" << size << "for image" << path; - if (!shm->lock()) { - qCDebug(lcSharedImage) << "Lock1 failed!?" << shm->errorString(); - return nil; - } - locked = true; - storeImageToMem(shm->data(), img); - } else if (shm->error() == QSharedMemory::AlreadyExists) { - // race handling: other process may have created the share while - // we loaded the image, so try again to just attach - if (!shm->attach(QSharedMemory::ReadOnly)) { - qCDebug(lcSharedImage) << "Attach to existing failed?" << shm->errorString(); - return nil; - } - } else { - qCDebug(lcSharedImage) << "Create failed?" << shm->errorString(); - return nil; - } - } - - Q_ASSERT(shm->isAttached()); - - if (!locked) { - if (!shm->lock()) { - qCDebug(lcSharedImage) << "Lock2 failed!?" << shm->errorString(); - return nil; - } - locked = true; - } - - if (!verifyMem(shm->constData(), shm->size())) { - qCDebug(lcSharedImage) << "Verifymem failed!?"; - shm->unlock(); - return nil; - } - - QSharedMemory *shmp = shm.take(); - SharedImageInfo *sii = new SharedImageInfo; - sii->path = path; - sii->shmp = shmp; - QImage shImg = createImageFromMem(shmp->constData(), sii); - - if (!shmp->unlock()) { - qCDebug(lcSharedImage) << "UnLock failed!?"; - } - - return shImg; -#else - Q_UNUSED(path); - Q_UNUSED(params); - return QImage(); -#endif -} - - -QSharedImageLoader::QSharedImageLoader(QObject *parent) - : QObject(*new QSharedImageLoaderPrivate, parent) -{ -} - -QSharedImageLoader::~QSharedImageLoader() -{ -} - -QImage QSharedImageLoader::load(const QString &path, ImageParameters *params) -{ - Q_D(QSharedImageLoader); - - return d->load(path, params); -} - -QImage QSharedImageLoader::loadFile(const QString &path, ImageParameters *params) -{ - Q_UNUSED(params); - - return QImage(path); -} - -QString QSharedImageLoader::key(const QString &path, ImageParameters *params) -{ - Q_UNUSED(params); - - return path; -} - - -QT_END_NAMESPACE diff --git a/src/imports/sharedimage/qsharedimageloader_p.h b/src/imports/sharedimage/qsharedimageloader_p.h deleted file mode 100644 index ce10d0d72c..0000000000 --- a/src/imports/sharedimage/qsharedimageloader_p.h +++ /dev/null @@ -1,81 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSHAREDIMAGELOADER_H -#define QSHAREDIMAGELOADER_H - -#include <QImage> -#include <QVariant> -#include <QLoggingCategory> -#include <qqml.h> - -QT_BEGIN_NAMESPACE - -Q_DECLARE_LOGGING_CATEGORY(lcSharedImage); - -class QSharedImageLoaderPrivate; - -class QSharedImageLoader : public QObject -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QSharedImageLoader) - - // We need to provide some type, in order to mention the 1.0 version. - QML_ANONYMOUS - QML_ADDED_IN_VERSION(1, 0) - -public: - typedef QVector<QVariant> ImageParameters; - - QSharedImageLoader(QObject *parent = nullptr); - ~QSharedImageLoader(); - - QImage load(const QString &path, ImageParameters *params = nullptr); - -protected: - virtual QImage loadFile(const QString &path, ImageParameters *params); - virtual QString key(const QString &path, ImageParameters *params); - -private: - Q_DISABLE_COPY(QSharedImageLoader) -}; - -QT_END_NAMESPACE - -#endif // QSHAREDIMAGELOADER_H diff --git a/src/imports/sharedimage/sharedimageprovider.cpp b/src/imports/sharedimage/sharedimageprovider.cpp deleted file mode 100644 index aad6ea3b78..0000000000 --- a/src/imports/sharedimage/sharedimageprovider.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <sharedimageprovider.h> -#include <qsharedimageloader_p.h> -#include <private/qquickpixmapcache_p.h> -#include <private/qimage_p.h> -#include <QImageReader> -#include <QFileInfo> -#include <QDir> - -Q_DECLARE_METATYPE(QQuickImageProviderOptions) - -class QuickSharedImageLoader : public QSharedImageLoader -{ - Q_OBJECT - friend class SharedImageProvider; - -public: - enum ImageParameter { - OriginalSize = 0, - RequestedSize, - ProviderOptions, - NumImageParameters - }; - - QuickSharedImageLoader(QObject *parent = nullptr) - : QSharedImageLoader(parent) - { - } - -protected: - QImage loadFile(const QString &path, ImageParameters *params) override - { - QImageReader imgio(path); - QSize realSize = imgio.size(); - QSize requestSize; - QQuickImageProviderOptions options; - if (params) { - requestSize = params->value(RequestedSize).toSize(); - options = params->value(ProviderOptions).value<QQuickImageProviderOptions>(); - } - - QSize scSize = QQuickImageProviderWithOptions::loadSize(imgio.size(), requestSize, imgio.format(), options); - - if (scSize.isValid()) - imgio.setScaledSize(scSize); - - QImage image; - if (imgio.read(&image)) { - if (realSize.isEmpty()) - realSize = image.size(); - // Make sure we have acceptable format for texture uploader, or it will convert & lose sharing - // This mimics the testing & conversion normally done by the quick pixmapcache & texturefactory - if (image.format() != QImage::Format_RGB32 && image.format() != QImage::Format_ARGB32_Premultiplied) { - QImage::Format newFmt = QImage::Format_RGB32; - if (image.hasAlphaChannel() && image.data_ptr()->checkForAlphaPixels()) - newFmt = QImage::Format_ARGB32_Premultiplied; - qCDebug(lcSharedImage) << "Convert on load from format" << image.format() << "to" << newFmt; - image = image.convertToFormat(newFmt); - } - } - - if (params && params->count() > OriginalSize) - params->replace(OriginalSize, realSize); - - return image; - } - - QString key(const QString &path, ImageParameters *params) override - { - QSize reqSz; - QQuickImageProviderOptions opts; - if (params) { - reqSz = params->value(RequestedSize).toSize(); - opts = params->value(ProviderOptions).value<QQuickImageProviderOptions>(); - } - if (!reqSz.isValid()) - return path; - int aspect = opts.preserveAspectRatioCrop() || opts.preserveAspectRatioFit() ? 1 : 0; - - QString key = path + QStringLiteral("_%1x%2_%3").arg(reqSz.width()).arg(reqSz.height()).arg(aspect); - qCDebug(lcSharedImage) << "KEY:" << key; - return key; - } -}; - - -SharedImageProvider::SharedImageProvider() - : QQuickImageProviderWithOptions(QQuickImageProvider::Image), loader(new QuickSharedImageLoader) -{ -} - -QImage SharedImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options) -{ - QFileInfo fi(QDir::root(), id); - QString path = fi.canonicalFilePath(); - if (path.isEmpty()) - return QImage(); - - QSharedImageLoader::ImageParameters params(QuickSharedImageLoader::NumImageParameters); - params[QuickSharedImageLoader::RequestedSize].setValue(requestedSize); - params[QuickSharedImageLoader::ProviderOptions].setValue(options); - - QImage img = loader->load(path, ¶ms); - if (img.isNull()) { - // May be sharing problem, fall back to normal local load - img = loader->loadFile(path, ¶ms); - if (!img.isNull()) - qCWarning(lcSharedImage) << "Sharing problem; loading" << id << "unshared"; - } - - //... QSize realSize = params.value(QSharedImageLoader::OriginalSize).toSize(); - // quickpixmapcache's readImage() reports back the original size, prior to requestedSize scaling, in the *size - // parameter. That value is currently ignored by quick however, which only cares about the present size of the - // returned image. So handling and sharing of info on pre-scaled size is currently not implemented. - if (size) { - *size = img.size(); - } - - return img; -} - -#include "sharedimageprovider.moc" diff --git a/src/imports/sharedimage/sharedimageprovider.h b/src/imports/sharedimage/sharedimageprovider.h deleted file mode 100644 index e6b6c77134..0000000000 --- a/src/imports/sharedimage/sharedimageprovider.h +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef SHAREDIMAGEPROVIDER_H -#define SHAREDIMAGEPROVIDER_H - -#include <QQuickImageProvider> -#include <private/qquickpixmapcache_p.h> -#include <QScopedPointer> - -class QuickSharedImageLoader; - -class SharedImageProvider : public QQuickImageProviderWithOptions -{ -public: - SharedImageProvider(); - - QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options) override; - -protected: - QScopedPointer<QuickSharedImageLoader> loader; -}; -#endif // SHAREDIMAGEPROVIDER_H |