~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/src/platforms/mirserver/screenwindow.cpp

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013-2016 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include "screenwindow.h"
 
18
#include "screen.h"
 
19
 
 
20
// Mir
 
21
#include <mir/geometry/size.h>
 
22
#include <mir/graphics/display_buffer.h>
 
23
 
 
24
// Qt
 
25
#include <qpa/qwindowsysteminterface.h>
 
26
#include <qpa/qplatformscreen.h>
 
27
#include <QQuickWindow>
 
28
#include <QtQuick/private/qsgrenderloop_p.h>
 
29
#include <QDebug>
 
30
 
 
31
#include "logging.h"
 
32
 
 
33
static WId newWId()
 
34
{
 
35
    static WId id = 0;
 
36
 
 
37
    if (id == std::numeric_limits<WId>::max())
 
38
        qWarning("MirServer QPA: Out of window IDs");
 
39
 
 
40
    return ++id;
 
41
}
 
42
 
 
43
ScreenWindow::ScreenWindow(QWindow *window)
 
44
    : QPlatformWindow(window)
 
45
    , m_exposed(false)
 
46
    , m_winId(newWId())
 
47
{
 
48
    // Note: window->screen() is set to the primaryScreen(), if not specified explicitly.
 
49
    const auto myScreen = static_cast<Screen *>(window->screen()->handle());
 
50
    myScreen->setWindow(this);
 
51
    qCDebug(QTMIR_SCREENS) << "ScreenWindow" << this << "with window ID" << uint(m_winId) << "backed by" << myScreen << "with ID" << myScreen->outputId().as_value();
 
52
 
 
53
    QRect screenGeometry(screen()->availableGeometry());
 
54
    if (window->geometry() != screenGeometry) {
 
55
        setGeometry(screenGeometry);
 
56
        window->setGeometry(screenGeometry);
 
57
    }
 
58
    window->setSurfaceType(QSurface::OpenGLSurface);
 
59
}
 
60
 
 
61
ScreenWindow::~ScreenWindow()
 
62
{
 
63
    qCDebug(QTMIR_SCREENS) << "Destroying ScreenWindow" << this;
 
64
    static_cast<Screen *>(screen())->setWindow(nullptr);
 
65
}
 
66
 
 
67
bool ScreenWindow::isExposed() const
 
68
{
 
69
    return m_exposed;
 
70
}
 
71
 
 
72
void ScreenWindow::setExposed(const bool exposed)
 
73
{
 
74
    qCDebug(QTMIR_SCREENS) << "ScreenWindow::setExposed" << this << exposed << screen();
 
75
    if (m_exposed == exposed)
 
76
        return;
 
77
 
 
78
    m_exposed = exposed;
 
79
    if (!window())
 
80
        return;
 
81
 
 
82
    // If backing a QQuickWindow, need to stop/start its renderer immediately
 
83
    auto quickWindow = static_cast<QQuickWindow *>(window());
 
84
    if (!quickWindow)
 
85
        return;
 
86
 
 
87
    auto renderer = QSGRenderLoop::instance();
 
88
    if (exposed) {
 
89
        renderer->show(quickWindow);
 
90
        QWindowSystemInterface::handleExposeEvent(window(), geometry()); // else it won't redraw
 
91
    } else {
 
92
        quickWindow->setPersistentOpenGLContext(false);
 
93
        quickWindow->setPersistentSceneGraph(false);
 
94
        renderer->hide(quickWindow); // ExposeEvent will arrive too late, need to stop compositor immediately
 
95
    }
 
96
}
 
97
 
 
98
void ScreenWindow::setScreen(QPlatformScreen *newScreen)
 
99
{
 
100
    // Dis-associate the old screen
 
101
    if (screen()) {
 
102
        static_cast<Screen *>(screen())->setWindow(nullptr);
 
103
    }
 
104
 
 
105
    // Associate new screen and announce to Qt
 
106
    auto myScreen = static_cast<Screen *>(newScreen);
 
107
    Q_ASSERT(myScreen);
 
108
    myScreen->setWindow(this);
 
109
 
 
110
    QWindowSystemInterface::handleWindowScreenChanged(window(), myScreen->screen());
 
111
    setExposed(true); //GERRY - assumption setScreen only called while compositor running
 
112
 
 
113
    qCDebug(QTMIR_SCREENS) << "ScreenWindow" << this << "with window ID" << uint(m_winId) << "NEWLY backed by" << myScreen;
 
114
}
 
115
 
 
116
void ScreenWindow::swapBuffers()
 
117
{
 
118
    static_cast<Screen *>(screen())->swapBuffers();
 
119
}
 
120
 
 
121
void ScreenWindow::makeCurrent()
 
122
{
 
123
    static_cast<Screen *>(screen())->makeCurrent();
 
124
}
 
125
 
 
126
void ScreenWindow::doneCurrent()
 
127
{
 
128
    static_cast<Screen *>(screen())->doneCurrent();
 
129
}