1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*
* Copyright (C) 2013-2014 Canonical, Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// local
#include "applicationscreenshotprovider.h"
#include "application_manager.h"
#include "application.h"
// QPA-mirserver
#include "logging.h"
// mir
#include <mir/scene/session.h>
// Qt
#include <QMutex>
#include <QMutexLocker>
#include <QWaitCondition>
namespace qtmir
{
ApplicationScreenshotProvider::ApplicationScreenshotProvider(ApplicationManager *appManager)
: QQuickImageProvider(QQuickImageProvider::Image)
, m_appManager(appManager)
{
}
QImage ApplicationScreenshotProvider::requestImage(const QString &imageId, QSize *size,
const QSize &requestedSize)
{
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider::requestImage - imageId=" << imageId;
QString appId = imageId.split('/').first();
Application* app = static_cast<Application*>(m_appManager->findApplication(appId));
if (app == nullptr) {
qWarning() << "ApplicationScreenshotProvider - app with appId" << appId << "not found";
return QImage();
}
// TODO: if app not ready, return an app-provided splash image. If app has been stopped with saved state
// return the screenshot that was saved to disk.
if (!app->session() || !app->session()->default_surface()) {
qWarning() << "ApplicationScreenshotProvider - app session not found - asking for screenshot too early";
return QImage();
}
QImage screenshotImage;
QMutex screenshotMutex;
QWaitCondition screenshotTakenCondition;
app->session()->take_snapshot(
[&](mir::scene::Snapshot const& snapshot)
{
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - Mir snapshot ready with size"
<< snapshot.size.height.as_int() << "x" << snapshot.size.width.as_int();
{
// since we mirror, no need to offset starting position of the pixels
QImage fullSizeScreenshot = QImage( (const uchar*)snapshot.pixels,
snapshot.size.width.as_int(),
snapshot.size.height.as_int(),
QImage::Format_ARGB32_Premultiplied).mirrored();
QMutexLocker screenshotMutexLocker(&screenshotMutex);
if (requestedSize.isValid()) {
*size = requestedSize.boundedTo(fullSizeScreenshot.size());
screenshotImage = fullSizeScreenshot.scaled(*size, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
} else {
*size = fullSizeScreenshot.size();
screenshotImage = fullSizeScreenshot;
}
screenshotTakenCondition.wakeAll();
}
});
{
QMutexLocker screenshotMutexLocker(&screenshotMutex);
if (screenshotImage.isNull()) {
// mir is taking a snapshot in a separate thread. Wait here until it's done.
screenshotTakenCondition.wait(&screenshotMutex);
} else {
// mir took a snapshot synchronously or it was asynchronous but already finished.
// In either case, there's no need to wait.
}
}
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - working with size" << screenshotImage;
return screenshotImage;
}
} // namespace qtmir
|