2
* Copyright (C) 2013-2015 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "applicationscreenshotprovider.h"
19
#include "application_manager.h"
20
#include "application.h"
27
#include <mir/scene/session.h>
31
#include <QMutexLocker>
32
#include <QWaitCondition>
37
ApplicationScreenshotProvider::ApplicationScreenshotProvider(ApplicationManager *appManager)
38
: QQuickImageProvider(QQuickImageProvider::Image)
39
, m_appManager(appManager)
43
QImage ApplicationScreenshotProvider::requestImage(const QString &imageId, QSize *size,
44
const QSize &requestedSize)
46
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider::requestImage - imageId=" << imageId;
48
QString appId = imageId.split('/').first();
50
Application* app = static_cast<Application*>(m_appManager->findApplication(appId));
52
qWarning() << "ApplicationScreenshotProvider - app with appId" << appId << "not found";
56
// TODO: if app not ready, return an app-provided splash image. If app has been stopped with saved state
57
// return the screenshot that was saved to disk.
58
SessionInterface* session = app->session();
59
if (!session || !session->session() || !session->session()->default_surface()) {
60
qWarning() << "ApplicationScreenshotProvider - app session not found - asking for screenshot too early";
64
QImage screenshotImage;
65
QMutex screenshotMutex;
66
QWaitCondition screenshotTakenCondition;
67
bool screenShotDone = false;
69
session->session()->take_snapshot(
70
[&](mir::scene::Snapshot const& snapshot)
72
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - Mir snapshot ready with size"
73
<< snapshot.size.height.as_int() << "x" << snapshot.size.width.as_int();
76
// since we mirror, no need to offset starting position of the pixels
77
QImage fullSizeScreenshot = QImage( (const uchar*)snapshot.pixels,
78
snapshot.size.width.as_int(),
79
snapshot.size.height.as_int(),
80
QImage::Format_ARGB32_Premultiplied).mirrored();
82
if (!fullSizeScreenshot.isNull()) {
83
if (requestedSize.isValid()) {
84
*size = requestedSize.boundedTo(fullSizeScreenshot.size());
85
screenshotImage = fullSizeScreenshot.scaled(*size, Qt::IgnoreAspectRatio,
86
Qt::SmoothTransformation);
88
*size = fullSizeScreenshot.size();
89
screenshotImage = fullSizeScreenshot;
93
{ // Sync point with Qt's ImageProviderThread
94
QMutexLocker screenshotMutexLocker(&screenshotMutex);
95
screenShotDone = true;
98
screenshotTakenCondition.wakeAll();
102
{ // Sync point with Mir's snapshot thread
103
QMutexLocker screenshotMutexLocker(&screenshotMutex);
104
if (!screenShotDone) {
105
screenshotTakenCondition.wait(&screenshotMutex);
109
qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - working with size" << screenshotImage;
111
return screenshotImage;