forked from qt-creator/qt-creator
QmlDesigner: Add AsynchronousExplicitImageCache
Task-number: QDS-5859 Change-Id: I962be68f22ba0c0118c14149a0d0ecf395934db5 Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** 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 General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "asynchronousexplicitimagecache.h"
|
||||
|
||||
#include "imagecachestorage.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
AsynchronousExplicitImageCache::AsynchronousExplicitImageCache(ImageCacheStorageInterface &storage)
|
||||
: m_storage(storage)
|
||||
{
|
||||
m_backgroundThread = std::thread{[this] {
|
||||
while (isRunning()) {
|
||||
if (auto [hasEntry, entry] = getEntry(); hasEntry) {
|
||||
request(entry.name,
|
||||
entry.extraId,
|
||||
entry.requestType,
|
||||
std::move(entry.captureCallback),
|
||||
std::move(entry.abortCallback),
|
||||
m_storage);
|
||||
}
|
||||
|
||||
waitForEntries();
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
AsynchronousExplicitImageCache::~AsynchronousExplicitImageCache()
|
||||
{
|
||||
clean();
|
||||
wait();
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::request(Utils::SmallStringView name,
|
||||
Utils::SmallStringView extraId,
|
||||
AsynchronousExplicitImageCache::RequestType requestType,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
ImageCacheStorageInterface &storage)
|
||||
{
|
||||
const auto id = extraId.empty() ? Utils::PathString{name}
|
||||
: Utils::PathString::join({name, "+", extraId});
|
||||
|
||||
const auto entry = requestType == RequestType::Image
|
||||
? storage.fetchImage(id, Sqlite::TimeStamp{})
|
||||
: storage.fetchSmallImage(id, Sqlite::TimeStamp{});
|
||||
|
||||
if (entry.hasEntry && !entry.image.isNull())
|
||||
captureCallback(entry.image);
|
||||
else
|
||||
abortCallback(ImageCache::AbortReason::Failed);
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::wait()
|
||||
{
|
||||
stopThread();
|
||||
m_condition.notify_all();
|
||||
if (m_backgroundThread.joinable())
|
||||
m_backgroundThread.join();
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::requestImage(Utils::PathString name,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
Utils::SmallString extraId)
|
||||
{
|
||||
addEntry(std::move(name),
|
||||
std::move(extraId),
|
||||
std::move(captureCallback),
|
||||
std::move(abortCallback),
|
||||
RequestType::Image);
|
||||
m_condition.notify_all();
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::requestSmallImage(Utils::PathString name,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
Utils::SmallString extraId)
|
||||
{
|
||||
addEntry(std::move(name),
|
||||
std::move(extraId),
|
||||
std::move(captureCallback),
|
||||
std::move(abortCallback),
|
||||
RequestType::SmallImage);
|
||||
m_condition.notify_all();
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::clean()
|
||||
{
|
||||
clearEntries();
|
||||
}
|
||||
|
||||
std::tuple<bool, AsynchronousExplicitImageCache::RequestEntry> AsynchronousExplicitImageCache::getEntry()
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
|
||||
if (m_requestEntries.empty())
|
||||
return {false, RequestEntry{}};
|
||||
|
||||
RequestEntry entry = m_requestEntries.front();
|
||||
m_requestEntries.pop_front();
|
||||
|
||||
return {true, entry};
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::addEntry(Utils::PathString &&name,
|
||||
Utils::SmallString &&extraId,
|
||||
ImageCache::CaptureImageCallback &&captureCallback,
|
||||
ImageCache::AbortCallback &&abortCallback,
|
||||
RequestType requestType)
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
|
||||
m_requestEntries.emplace_back(std::move(name),
|
||||
std::move(extraId),
|
||||
std::move(captureCallback),
|
||||
std::move(abortCallback),
|
||||
requestType);
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::clearEntries()
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
for (RequestEntry &entry : m_requestEntries)
|
||||
entry.abortCallback(ImageCache::AbortReason::Abort);
|
||||
m_requestEntries.clear();
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::waitForEntries()
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
if (m_requestEntries.empty())
|
||||
m_condition.wait(lock, [&] { return m_requestEntries.size() || m_finishing; });
|
||||
}
|
||||
|
||||
void AsynchronousExplicitImageCache::stopThread()
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
m_finishing = true;
|
||||
}
|
||||
|
||||
bool AsynchronousExplicitImageCache::isRunning()
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
return !m_finishing || m_requestEntries.size();
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
||||
@@ -0,0 +1,111 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2020 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** 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 General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** 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-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "asynchronousimagecacheinterface.h"
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
class ImageCacheStorageInterface;
|
||||
|
||||
class AsynchronousExplicitImageCache
|
||||
{
|
||||
public:
|
||||
~AsynchronousExplicitImageCache();
|
||||
|
||||
AsynchronousExplicitImageCache(ImageCacheStorageInterface &storage);
|
||||
|
||||
void requestImage(Utils::PathString name,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
Utils::SmallString extraId = {});
|
||||
void requestSmallImage(Utils::PathString name,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
Utils::SmallString extraId = {});
|
||||
|
||||
void clean();
|
||||
|
||||
private:
|
||||
enum class RequestType { Image, SmallImage, Icon };
|
||||
struct RequestEntry
|
||||
{
|
||||
RequestEntry() = default;
|
||||
RequestEntry(Utils::PathString name,
|
||||
Utils::SmallString extraId,
|
||||
ImageCache::CaptureImageCallback &&captureCallback,
|
||||
ImageCache::AbortCallback &&abortCallback,
|
||||
RequestType requestType)
|
||||
: name{std::move(name)}
|
||||
, extraId{std::move(extraId)}
|
||||
, captureCallback{std::move(captureCallback)}
|
||||
, abortCallback{std::move(abortCallback)}
|
||||
, requestType{requestType}
|
||||
{}
|
||||
|
||||
Utils::PathString name;
|
||||
Utils::SmallString extraId;
|
||||
ImageCache::CaptureImageCallback captureCallback;
|
||||
ImageCache::AbortCallback abortCallback;
|
||||
RequestType requestType = RequestType::Image;
|
||||
};
|
||||
|
||||
std::tuple<bool, RequestEntry> getEntry();
|
||||
void addEntry(Utils::PathString &&name,
|
||||
Utils::SmallString &&extraId,
|
||||
ImageCache::CaptureImageCallback &&captureCallback,
|
||||
ImageCache::AbortCallback &&abortCallback,
|
||||
RequestType requestType);
|
||||
void clearEntries();
|
||||
void waitForEntries();
|
||||
void stopThread();
|
||||
bool isRunning();
|
||||
static void request(Utils::SmallStringView name,
|
||||
Utils::SmallStringView extraId,
|
||||
AsynchronousExplicitImageCache::RequestType requestType,
|
||||
ImageCache::CaptureImageCallback captureCallback,
|
||||
ImageCache::AbortCallback abortCallback,
|
||||
ImageCacheStorageInterface &storage);
|
||||
|
||||
private:
|
||||
void wait();
|
||||
|
||||
private:
|
||||
std::deque<RequestEntry> m_requestEntries;
|
||||
mutable std::mutex m_mutex;
|
||||
std::condition_variable m_condition;
|
||||
std::thread m_backgroundThread;
|
||||
ImageCacheStorageInterface &m_storage;
|
||||
bool m_finishing{false};
|
||||
};
|
||||
|
||||
} // namespace QmlDesigner
|
||||
@@ -43,7 +43,6 @@ class ImageCacheCollectorInterface;
|
||||
class AsynchronousImageCache final : public AsynchronousImageCacheInterface
|
||||
{
|
||||
public:
|
||||
|
||||
~AsynchronousImageCache();
|
||||
|
||||
AsynchronousImageCache(ImageCacheStorageInterface &storage,
|
||||
@@ -62,7 +61,6 @@ public:
|
||||
ImageCache::AuxiliaryData auxiliaryData = {}) override;
|
||||
|
||||
void clean();
|
||||
// void waitForFinished();
|
||||
|
||||
private:
|
||||
enum class RequestType { Image, SmallImage, Icon };
|
||||
|
||||
@@ -107,6 +107,7 @@ function(extend_with_qmldesigner_core target_name)
|
||||
filemanager/removeuiobjectmembervisitor.cpp
|
||||
filemanager/removeuiobjectmembervisitor.h
|
||||
|
||||
imagecache/asynchronousexplicitimagecache.cpp
|
||||
imagecache/asynchronousimagecache.cpp
|
||||
imagecache/imagecachecollector.cpp
|
||||
imagecache/imagecachecollector.h
|
||||
@@ -129,6 +130,7 @@ function(extend_with_qmldesigner_core target_name)
|
||||
include/abstractview.h
|
||||
include/anchorline.h
|
||||
include/annotation.h
|
||||
include/asynchronousexplicitimagecache.h
|
||||
include/asynchronousimagecache.h
|
||||
include/basetexteditmodifier.h
|
||||
include/bindingproperty.h
|
||||
|
||||
Reference in New Issue
Block a user