Skip to content

Commit

Permalink
Compositor 0.3
Browse files Browse the repository at this point in the history
Don't require OpenGL
  • Loading branch information
AaronDewes committed Aug 24, 2020
1 parent 21eaf7d commit cdfdbe4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 65 deletions.
2 changes: 1 addition & 1 deletion buildroot/package/compositor/compositor.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#############################################################


COMPOSITOR_VERSION = 0.2
COMPOSITOR_VERSION = 0.3
COMPOSITOR_SITE = $(TOPDIR)/../qwindow-compositor
COMPOSITOR_SITE_METHOD = local
COMPOSITOR_LICENSE = BSD-3c
Expand Down
Binary file removed qwindow-compositor/background.jpg
Binary file not shown.
1 change: 1 addition & 0 deletions qwindow-compositor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int main(int argc, char *argv[])
window.setCompositor(&compositor);
window.resize(800,600);
window.show();
window.init();

return app.exec();
}
2 changes: 1 addition & 1 deletion qwindow-compositor/qwindow-compositor.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<RCC>
<qresource>
<file alias="background.jpg">background.jpg</file>
<file alias="wallpaper.png">wallpaper.png</file>
</qresource>
</RCC>
Binary file added qwindow-compositor/wallpaper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 45 additions & 54 deletions qwindow-compositor/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@
#include "window.h"

#include <QMouseEvent>
#include <QOpenGLWindow>
#include <QOpenGLTexture>
#include <QOpenGLFunctions>
#include <QWindow>
#include <QBackingStore>
#include <QMatrix4x4>
#include <QPainter>

#include "compositor.h"
#include <QtWaylandCompositor/qwaylandseat.h>

Window::Window()
:QWindow()
, m_backingStore(new QBackingStore(this))
{
}

Expand All @@ -71,10 +72,12 @@ void Window::setCompositor(Compositor *comp) {
connect(m_compositor, &Compositor::dragStarted, this, &Window::startDrag);
}

void Window::initializeGL()
void Window::init()
{
m_textureBlitter.create();
m_compositor->create(); // the compositor's hardware integration may depend on GL
m_compositor->create();
m_compositor->startRender();
drawBackground();
m_compositor->endRender();
}

void Window::drawBackground()
Expand All @@ -86,12 +89,14 @@ void Window::drawBackground()
if(this->geometry().width() < backgroundImage.width() || this->geometry().height() < backgroundImage.height()) {
backgroundImage = m_backgroundImage.scaled(this->geometry().size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
QPainter painter(this);
QPaintDevice *device = m_backingStore->paintDevice();
QPainter painter(device);
int deltaX = this->geometry().width() - backgroundImage.width();
int deltaY = this->geometry().height() - backgroundImage.height();
painter.drawImage(this->geometry(), backgroundColorImage);
painter.translate(deltaX / 2, deltaY / 2);
painter.drawImage(backgroundImage.rect(), backgroundImage);
painter.end();
}

QPointF Window::getAnchorPosition(const QPointF &position, int resizeEdge, const QSize &windowSize)
Expand All @@ -112,53 +117,6 @@ QPointF Window::getAnchoredPosition(const QPointF &anchorPosition, int resizeEdg
return anchorPosition - getAnchorPosition(QPointF(), resizeEdge, windowSize);
}

void Window::paintGL()
{
m_compositor->startRender();
QOpenGLFunctions *functions = context()->functions();
functions->glClearColor(1.f, .6f, .0f, 0.5f);
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

m_textureBlitter.bind();
drawBackground();

functions->glEnable(GL_BLEND);
functions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLenum currentTarget = GL_TEXTURE_2D;
const auto views = m_compositor->views();
for (View *view : views) {
if (view->isCursor())
continue;
auto texture = view->getTexture();
if (!texture)
continue;
if (texture->target() != currentTarget) {
currentTarget = texture->target();
m_textureBlitter.bind(currentTarget);
}
QWaylandSurface *surface = view->surface();
if ((surface && surface->hasContent()) || view->isBufferLocked()) {
QSize s = view->size();
if (!s.isEmpty()) {
if (m_mouseView == view && m_grabState == ResizeGrab && m_resizeAnchored)
view->setPosition(getAnchoredPosition(m_resizeAnchorPosition, m_resizeEdge, s));
QPointF pos = view->position() + view->parentPosition();
QRectF surfaceGeometry(pos, s);
auto surfaceOrigin = view->textureOrigin();
auto sf = view->animationFactor();
QRectF targetRect(surfaceGeometry.topLeft() * sf, surfaceGeometry.size() * sf);
QMatrix4x4 targetTransform = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(), size()));
m_textureBlitter.blit(texture->textureId(), targetTransform, surfaceOrigin);
}
}
}
functions->glDisable(GL_BLEND);

m_textureBlitter.release();
m_compositor->endRender();
}

View *Window::viewAt(const QPointF &point)
{
View *ret = nullptr;
Expand Down Expand Up @@ -281,3 +239,36 @@ void Window::keyReleaseEvent(QKeyEvent *e)
{
m_compositor->defaultSeat()->sendKeyReleaseEvent(e->nativeScanCode());
}

void Window::update()
{
if (!isExposed())
return;

QRect rect(0, 0, width(), height());
m_backingStore->beginPaint(rect);

drawBackground();
m_backingStore->endPaint();
m_backingStore->flush(rect);
}

bool Window::event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest) {
update();
return true;
}
return QWindow::event(event);
}

void Window::exposeEvent(QExposeEvent *)
{
if (isExposed())
update();
}

void Window::resizeEvent(QResizeEvent *resizeEvent)
{
m_backingStore->resize(resizeEvent->size());
}
21 changes: 12 additions & 9 deletions qwindow-compositor/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,30 @@
#ifndef COMPOSITORWINDOW_H
#define COMPOSITORWINDOW_H

#include <QOpenGLWindow>
#include <QWindow>
#include <QPointer>
#include <QOpenGLTextureBlitter>

QT_BEGIN_NAMESPACE

class Compositor;
class View;
class QOpenGLTexture;

class Window : public QOpenGLWindow
class Window : public QWindow
{
public:
Window();

void setCompositor(Compositor *comp);
void init();

public slots:
void update();

protected:
void initializeGL() override;
void paintGL() override;
bool event(QEvent *event) override;

void resizeEvent(QResizeEvent *event) override;
void exposeEvent(QExposeEvent *event) override;

void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
Expand All @@ -94,9 +98,7 @@ private slots:
static QPointF getAnchoredPosition(const QPointF &anchorPosition, int resizeEdge, const QSize &windowSize);
static QPointF getAnchorPosition(const QPointF &position, int resizeEdge, const QSize &windowSize);

QOpenGLTextureBlitter m_textureBlitter;
QImage m_backgroundImage = QImage(QLatin1String(":/background.jpg"));
QOpenGLTexture *m_backgroundTexture = nullptr;
QImage m_backgroundImage = QImage(QLatin1String(":/wallpaper.png"));
Compositor *m_compositor = nullptr;
QPointer<View> m_mouseView;
GrabState m_grabState = NoGrab;
Expand All @@ -107,6 +109,7 @@ private slots:
QPointF m_mouseOffset;
QPointF m_initialMousePos;
View *m_dragIconView = nullptr;
QBackingStore *m_backingStore = nullptr;
};

QT_END_NAMESPACE
Expand Down

0 comments on commit cdfdbe4

Please sign in to comment.