KWin
Loading...
Searching...
No Matches
window.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
6 SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10// this needs to be on top, epoxy has an error if you include it after GL/gl.h,
11// which Qt does include
13
14#include "compositor.h"
15#include "core/renderbackend.h"
17#include "internalwindow.h"
18#include "swapchain.h"
19#include "window.h"
20
21#include <logging.h>
22
23#include <libdrm/drm_fourcc.h>
24#include <qpa/qwindowsysteminterface.h>
25
26namespace KWin
27{
28namespace QPA
29{
30static quint32 s_windowId = 0;
31
32Window::Window(QWindow *window)
33 : QPlatformWindow(window)
34 , m_windowId(++s_windowId)
35 , m_scale(kwinApp()->devicePixelRatio())
36{
37 Q_ASSERT(!window->property("_KWIN_WINDOW_IS_OFFSCREEN").toBool());
38}
39
41{
42 unmap();
43}
44
45Swapchain *Window::swapchain(const QHash<uint32_t, QList<uint64_t>> &formats)
46{
47 const QSize nativeSize = geometry().size() * devicePixelRatio();
48 if (!m_swapchain || m_swapchain->size() != nativeSize
49 || !formats.contains(m_swapchain->format())
50 || m_swapchain->modifiers() != formats[m_swapchain->format()]) {
51 const bool software = window()->surfaceType() == QSurface::RasterSurface; // RasterGLSurface is unsupported by us
52
53 GraphicsBufferAllocator *allocator;
54 if (software) {
55 static ShmGraphicsBufferAllocator shmAllocator;
56 allocator = &shmAllocator;
57 } else {
59 }
60
61 for (auto it = formats.begin(); it != formats.end(); it++) {
62 if (auto info = FormatInfo::get(it.key()); info && info->bitsPerColor == 8 && info->alphaBits == 8) {
63 const auto options = GraphicsBufferOptions{
64 .size = nativeSize,
65 .format = it.key(),
66 .modifiers = it.value(),
67 .software = software,
68 };
69 auto buffer = allocator->allocate(options);
70 if (!buffer) {
71 continue;
72 }
73 m_swapchain = std::make_unique<Swapchain>(allocator, options, buffer);
74 break;
75 }
76 }
77 }
78 return m_swapchain.get();
79}
80
82{
83 m_swapchain.reset();
84}
85
86void Window::setVisible(bool visible)
87{
88 if (visible) {
89 map();
90 } else {
91 unmap();
92 }
93
94 QPlatformWindow::setVisible(visible);
95}
96
97QSurfaceFormat Window::format() const
98{
99 return m_format;
100}
101
103{
104#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
105 QWindowSystemInterface::handleWindowActivated(window());
106#else
107 QWindowSystemInterface::handleFocusWindowChanged(window());
108#endif
109}
110
111void Window::setGeometry(const QRect &rect)
112{
113 const QRect oldGeometry = geometry();
114 QPlatformWindow::setGeometry(rect);
115
116 if (window()->isVisible() && rect.isValid()) {
117 QWindowSystemInterface::handleGeometryChange(window(), geometry());
118 }
119
120 if (isExposed() && oldGeometry.size() != rect.size()) {
121 QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(), rect.size()));
122 }
123}
124
125WId Window::winId() const
126{
127 return m_windowId;
128}
129
131{
132 return m_scale;
133}
134
136{
137 return m_handle;
138}
139
140void Window::map()
141{
142 if (m_handle) {
143 return;
144 }
145
146 m_handle = new InternalWindow(window());
147}
148
149void Window::unmap()
150{
151 if (!m_handle) {
152 return;
153 }
154
155 m_handle->destroyWindow();
156 m_handle = nullptr;
157
159}
160
161}
162}
RenderBackend * backend() const
Definition compositor.h:68
static Compositor * self()
virtual GraphicsBuffer * allocate(const GraphicsBufferOptions &options)=0
QSize size() const
Definition swapchain.cpp:28
QSurfaceFormat format() const override
Definition window.cpp:97
InternalWindow * internalWindow() const
Definition window.cpp:135
~Window() override
Definition window.cpp:40
void setGeometry(const QRect &rect) override
Definition window.cpp:111
qreal devicePixelRatio() const override
Definition window.cpp:130
void setVisible(bool visible) override
Definition window.cpp:86
void requestActivateWindow() override
Definition window.cpp:102
Swapchain * swapchain(const QHash< uint32_t, QList< uint64_t > > &formats)
Definition window.cpp:45
WId winId() const override
Definition window.cpp:125
void invalidateSurface() override
Definition window.cpp:81
virtual GraphicsBufferAllocator * graphicsBufferAllocator() const
Options * options
Definition main.cpp:73
static std::optional< FormatInfo > get(uint32_t drmFormat)
QSize size
The size of the buffer, in device pixels.