~unity-team/qtmir/fix-some-lifecycle-bugs

« back to all changes in this revision

Viewing changes to src/modules/Unity/Application/applicationscreenshotprovider.cpp

  • Committer: CI bot
  • Author(s): Daniel d'Andrada
  • Date: 2014-08-25 07:45:19 UTC
  • mfrom: (228.1.10 lifecycle)
  • Revision ID: ps-jenkins@lists.canonical.com-20140825074519-tk0lgkzfmt94uar8
Revamp screenshotting to use an image provider again, removing screenshot-related methods on Application

Implementing sourceSize support to scale down images if needed to save memory Fixes: 1359819, 1359821
Approved by: Michał Sawicz, Gerry Boland, Michael Zanetti

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
// mir
26
26
#include <mir/scene/session.h>
27
27
 
 
28
// Qt
 
29
#include <QMutex>
 
30
#include <QMutexLocker>
 
31
#include <QWaitCondition>
 
32
 
28
33
namespace qtmir
29
34
{
30
35
 
37
42
QImage ApplicationScreenshotProvider::requestImage(const QString &imageId, QSize *size,
38
43
                                                   const QSize &requestedSize)
39
44
{
40
 
    // We ignore requestedSize here intentionally to avoid keeping scaled copies around
41
 
    Q_UNUSED(requestedSize)
42
 
 
43
 
    qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider::requestImage - appId=" << imageId;
 
45
    qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider::requestImage - imageId=" << imageId;
44
46
 
45
47
    QString appId = imageId.split('/').first();
46
48
 
57
59
        return QImage();
58
60
    }
59
61
 
60
 
    QImage image = app->screenshotImage();
61
 
 
62
 
    qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - working with size" << image;
63
 
    size->setWidth(image.width());
64
 
    size->setHeight(image.height());
65
 
 
66
 
    return image;
 
62
    QImage screenshotImage;
 
63
    QMutex screenshotMutex;
 
64
    QWaitCondition screenshotTakenCondition;
 
65
 
 
66
    app->session()->take_snapshot(
 
67
        [&](mir::scene::Snapshot const& snapshot)
 
68
        {
 
69
            qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - Mir snapshot ready with size"
 
70
                                        << snapshot.size.height.as_int() << "x" << snapshot.size.width.as_int();
 
71
 
 
72
            {
 
73
                // since we mirror, no need to offset starting position of the pixels
 
74
                QImage fullSizeScreenshot = QImage( (const uchar*)snapshot.pixels,
 
75
                            snapshot.size.width.as_int(),
 
76
                            snapshot.size.height.as_int(),
 
77
                            QImage::Format_ARGB32_Premultiplied).mirrored();
 
78
 
 
79
                QMutexLocker screenshotMutexLocker(&screenshotMutex);
 
80
 
 
81
                if (requestedSize.isValid()) {
 
82
                    *size = requestedSize.boundedTo(fullSizeScreenshot.size());
 
83
                    screenshotImage = fullSizeScreenshot.scaled(*size, Qt::IgnoreAspectRatio,
 
84
                        Qt::SmoothTransformation);
 
85
                } else {
 
86
                    *size = fullSizeScreenshot.size();
 
87
                    screenshotImage = fullSizeScreenshot;
 
88
                }
 
89
 
 
90
                screenshotTakenCondition.wakeAll();
 
91
            }
 
92
        });
 
93
 
 
94
    {
 
95
        QMutexLocker screenshotMutexLocker(&screenshotMutex);
 
96
        if (screenshotImage.isNull()) {
 
97
            // mir is taking a snapshot in a separate thread. Wait here until it's done.
 
98
            screenshotTakenCondition.wait(&screenshotMutex);
 
99
        } else {
 
100
            // mir took a snapshot synchronously or it was asynchronous but already finished.
 
101
            // In either case, there's no need to wait.
 
102
        }
 
103
    }
 
104
 
 
105
    qCDebug(QTMIR_APPLICATIONS) << "ApplicationScreenshotProvider - working with size" << screenshotImage;
 
106
 
 
107
    return screenshotImage;
67
108
}
68
109
 
69
110
} // namespace qtmir