2021-02-04 09:07:20 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "surfaceitem.h"
|
2021-08-12 09:07:38 +00:00
|
|
|
#include "deleted.h"
|
2021-02-04 09:07:20 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
SurfaceItem::SurfaceItem(Window *window, Item *parent)
|
2021-08-12 09:07:38 +00:00
|
|
|
: Item(parent)
|
|
|
|
, m_window(window)
|
2021-02-04 09:07:20 +00:00
|
|
|
{
|
2022-04-22 17:39:12 +00:00
|
|
|
connect(window, &Window::windowClosed, this, &SurfaceItem::handleWindowClosed);
|
2021-08-12 09:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
Window *SurfaceItem::window() const
|
2021-08-12 09:07:38 +00:00
|
|
|
{
|
|
|
|
return m_window;
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:39:12 +00:00
|
|
|
void SurfaceItem::handleWindowClosed(Window *original, Deleted *deleted)
|
2021-08-12 09:07:38 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(original)
|
|
|
|
m_window = deleted;
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:25:42 +00:00
|
|
|
QMatrix4x4 SurfaceItem::surfaceToBufferMatrix() const
|
|
|
|
{
|
|
|
|
return m_surfaceToBufferMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::setSurfaceToBufferMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
m_surfaceToBufferMatrix = matrix;
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:07:20 +00:00
|
|
|
QRegion SurfaceItem::shape() const
|
|
|
|
{
|
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion SurfaceItem::opaque() const
|
|
|
|
{
|
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::addDamage(const QRegion ®ion)
|
|
|
|
{
|
|
|
|
m_damage += region;
|
|
|
|
scheduleRepaint(region);
|
|
|
|
|
2021-08-12 09:07:38 +00:00
|
|
|
Q_EMIT m_window->damaged(m_window, region);
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::resetDamage()
|
|
|
|
{
|
|
|
|
m_damage = QRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion SurfaceItem::damage() const
|
|
|
|
{
|
|
|
|
return m_damage;
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:06:04 +00:00
|
|
|
SurfacePixmap *SurfaceItem::pixmap() const
|
2021-02-04 09:07:20 +00:00
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
if (m_pixmap && m_pixmap->isValid()) {
|
|
|
|
return m_pixmap.data();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
2021-04-09 07:06:04 +00:00
|
|
|
if (m_previousPixmap && m_previousPixmap->isValid()) {
|
|
|
|
return m_previousPixmap.data();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:06:04 +00:00
|
|
|
SurfacePixmap *SurfaceItem::previousPixmap() const
|
2021-02-04 09:07:20 +00:00
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
return m_previousPixmap.data();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::referencePreviousPixmap()
|
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
if (m_previousPixmap && m_previousPixmap->isDiscarded()) {
|
2021-02-04 09:07:20 +00:00
|
|
|
m_referencePixmapCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::unreferencePreviousPixmap()
|
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
if (!m_previousPixmap || !m_previousPixmap->isDiscarded()) {
|
2021-02-04 09:07:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_referencePixmapCounter--;
|
|
|
|
if (m_referencePixmapCounter == 0) {
|
2021-04-09 07:06:04 +00:00
|
|
|
m_previousPixmap.reset();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::updatePixmap()
|
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
if (m_pixmap.isNull()) {
|
|
|
|
m_pixmap.reset(createPixmap());
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
2021-04-09 07:06:04 +00:00
|
|
|
if (m_pixmap->isValid()) {
|
|
|
|
m_pixmap->update();
|
2021-02-04 09:07:20 +00:00
|
|
|
} else {
|
2021-04-09 07:06:04 +00:00
|
|
|
m_pixmap->create();
|
|
|
|
if (m_pixmap->isValid()) {
|
2021-09-15 13:13:08 +00:00
|
|
|
unreferencePreviousPixmap();
|
2021-02-04 09:07:20 +00:00
|
|
|
discardQuads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::discardPixmap()
|
|
|
|
{
|
2021-04-09 07:06:04 +00:00
|
|
|
if (!m_pixmap.isNull()) {
|
|
|
|
if (m_pixmap->isValid()) {
|
|
|
|
m_previousPixmap.reset(m_pixmap.take());
|
|
|
|
m_previousPixmap->markAsDiscarded();
|
2021-09-15 13:13:08 +00:00
|
|
|
referencePreviousPixmap();
|
2021-02-04 09:07:20 +00:00
|
|
|
} else {
|
2021-04-09 07:06:04 +00:00
|
|
|
m_pixmap.reset();
|
2021-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addDamage(rect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceItem::preprocess()
|
|
|
|
{
|
|
|
|
updatePixmap();
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:51:23 +00:00
|
|
|
WindowQuadList SurfaceItem::buildQuads() const
|
|
|
|
{
|
|
|
|
const QRegion region = shape();
|
|
|
|
|
|
|
|
WindowQuadList quads;
|
|
|
|
quads.reserve(region.rectCount());
|
|
|
|
|
|
|
|
for (const QRectF rect : region) {
|
2021-06-10 10:32:37 +00:00
|
|
|
WindowQuad quad;
|
2021-05-21 10:51:23 +00:00
|
|
|
|
2021-07-21 19:25:42 +00:00
|
|
|
const QPointF bufferTopLeft = m_surfaceToBufferMatrix.map(rect.topLeft());
|
|
|
|
const QPointF bufferTopRight = m_surfaceToBufferMatrix.map(rect.topRight());
|
|
|
|
const QPointF bufferBottomRight = m_surfaceToBufferMatrix.map(rect.bottomRight());
|
|
|
|
const QPointF bufferBottomLeft = m_surfaceToBufferMatrix.map(rect.bottomLeft());
|
2021-05-21 10:51:23 +00:00
|
|
|
|
2021-06-10 10:02:10 +00:00
|
|
|
quad[0] = WindowVertex(rect.topLeft(), bufferTopLeft);
|
|
|
|
quad[1] = WindowVertex(rect.topRight(), bufferTopRight);
|
|
|
|
quad[2] = WindowVertex(rect.bottomRight(), bufferBottomRight);
|
|
|
|
quad[3] = WindowVertex(rect.bottomLeft(), bufferBottomLeft);
|
2021-05-21 10:51:23 +00:00
|
|
|
|
|
|
|
quads << quad;
|
|
|
|
}
|
|
|
|
|
|
|
|
return quads;
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:58:58 +00:00
|
|
|
SurfaceTexture::~SurfaceTexture()
|
2021-04-09 07:06:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:58:58 +00:00
|
|
|
SurfacePixmap::SurfacePixmap(SurfaceTexture *texture, QObject *parent)
|
2021-04-09 07:06:04 +00:00
|
|
|
: QObject(parent)
|
2021-10-20 14:58:58 +00:00
|
|
|
, m_texture(texture)
|
2021-04-09 07:06:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfacePixmap::update()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-20 14:58:58 +00:00
|
|
|
SurfaceTexture *SurfacePixmap::texture() const
|
2021-04-09 07:06:04 +00:00
|
|
|
{
|
2021-10-20 14:58:58 +00:00
|
|
|
return m_texture.data();
|
2021-04-09 07:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SurfacePixmap::hasAlphaChannel() const
|
|
|
|
{
|
|
|
|
return m_hasAlphaChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize SurfacePixmap::size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect SurfacePixmap::contentsRect() const
|
|
|
|
{
|
|
|
|
return m_contentsRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SurfacePixmap::isDiscarded() const
|
|
|
|
{
|
|
|
|
return m_isDiscarded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfacePixmap::markAsDiscarded()
|
|
|
|
{
|
|
|
|
m_isDiscarded = true;
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:07:20 +00:00
|
|
|
} // namespace KWin
|