2014-08-28 07:52:35 +00:00
|
|
|
/********************************************************************
|
2014-09-17 13:57:56 +00:00
|
|
|
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-08-28 07:52:35 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) version 3, or any
|
|
|
|
later version accepted by the membership of KDE e.V. (or its
|
|
|
|
successor approved by the membership of KDE e.V.), which shall
|
|
|
|
act as a proxy defined in Section 6 of version 3 of the license.
|
2014-08-28 07:52:35 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
2014-08-28 07:52:35 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-09-17 13:57:56 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2014-08-28 07:52:35 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-28 07:52:35 +00:00
|
|
|
*********************************************************************/
|
|
|
|
#include "surface_interface.h"
|
2014-08-28 12:22:53 +00:00
|
|
|
#include "buffer_interface.h"
|
2014-08-28 07:52:35 +00:00
|
|
|
#include "compositor_interface.h"
|
2014-09-19 05:06:31 +00:00
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
2014-08-28 07:52:35 +00:00
|
|
|
|
2014-09-17 14:10:38 +00:00
|
|
|
namespace KWayland
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-17 14:10:38 +00:00
|
|
|
namespace Server
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
class SurfaceInterface::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct State {
|
|
|
|
QRegion damage = QRegion();
|
|
|
|
QRegion opaque = QRegion();
|
|
|
|
QRegion input = QRegion();
|
|
|
|
qint32 scale = 1;
|
|
|
|
OutputInterface::Transform transform = OutputInterface::Transform::Normal;
|
|
|
|
QList<wl_resource*> callbacks = QList<wl_resource*>();
|
|
|
|
QPoint offset = QPoint();
|
|
|
|
BufferInterface *buffer = nullptr;
|
|
|
|
};
|
|
|
|
Private(SurfaceInterface *q, CompositorInterface *c);
|
|
|
|
~Private();
|
|
|
|
|
|
|
|
void create(wl_client *client, quint32 version, quint32 id);
|
|
|
|
void destroy();
|
|
|
|
|
|
|
|
static SurfaceInterface *get(wl_resource *native);
|
|
|
|
|
|
|
|
CompositorInterface *compositor;
|
|
|
|
wl_resource *surface = nullptr;
|
|
|
|
wl_client *client = nullptr;
|
|
|
|
State current;
|
|
|
|
State pending;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void commit();
|
|
|
|
void damage(const QRect &rect);
|
|
|
|
void setScale(qint32 scale);
|
|
|
|
void setTransform(OutputInterface::Transform transform);
|
|
|
|
void addFrameCallback(uint32_t callback);
|
|
|
|
void attachBuffer(wl_resource *buffer, const QPoint &offset);
|
|
|
|
|
|
|
|
static Private *cast(wl_resource *r) {
|
|
|
|
return reinterpret_cast<Private*>(wl_resource_get_user_data(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unbind(wl_resource *r);
|
|
|
|
static void destroyFrameCallback(wl_resource *r);
|
|
|
|
|
|
|
|
static void destroyCallback(wl_client *client, wl_resource *resource);
|
|
|
|
static void attachCallback(wl_client *client, wl_resource *resource, wl_resource *buffer, int32_t sx, int32_t sy);
|
|
|
|
static void damageCallback(wl_client *client, wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height);
|
|
|
|
static void frameCallaback(wl_client *client, wl_resource *resource, uint32_t callback);
|
|
|
|
static void opaqueRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
|
|
|
|
static void inputRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region);
|
|
|
|
static void commitCallback(wl_client *client, wl_resource *resource);
|
|
|
|
// since version 2
|
|
|
|
static void bufferTransformCallback(wl_client *client, wl_resource *resource, int32_t transform);
|
|
|
|
// since version 3
|
|
|
|
static void bufferScaleCallback(wl_client *client, wl_resource *resource, int32_t scale);
|
|
|
|
|
|
|
|
SurfaceInterface *q;
|
|
|
|
static const struct wl_surface_interface s_interface;
|
|
|
|
static QList<SurfaceInterface::Private*> s_allSurfaces;
|
2014-08-28 07:52:35 +00:00
|
|
|
};
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
QList<SurfaceInterface::Private*> SurfaceInterface::Private::s_allSurfaces;
|
|
|
|
|
|
|
|
SurfaceInterface::Private::Private(SurfaceInterface *q, CompositorInterface *c)
|
|
|
|
: compositor(c)
|
|
|
|
, q(q)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
s_allSurfaces << this;
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
SurfaceInterface::Private::~Private()
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
destroy();
|
2014-09-19 05:06:31 +00:00
|
|
|
s_allSurfaces.removeAll(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceInterface *SurfaceInterface::Private::get(wl_resource *native)
|
|
|
|
{
|
|
|
|
auto it = std::find_if(s_allSurfaces.constBegin(), s_allSurfaces.constEnd(), [native](Private *s) {
|
|
|
|
return s->surface == native;
|
|
|
|
});
|
|
|
|
if (it == s_allSurfaces.constEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (*it)->q;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct wl_surface_interface SurfaceInterface::Private::s_interface = {
|
|
|
|
destroyCallback,
|
|
|
|
attachCallback,
|
|
|
|
damageCallback,
|
|
|
|
frameCallaback,
|
|
|
|
opaqueRegionCallback,
|
|
|
|
inputRegionCallback,
|
|
|
|
commitCallback,
|
|
|
|
bufferTransformCallback,
|
|
|
|
bufferScaleCallback
|
|
|
|
};
|
|
|
|
|
|
|
|
SurfaceInterface::SurfaceInterface(CompositorInterface *parent)
|
|
|
|
: QObject(/*parent*/)
|
|
|
|
, d(new Private(this, parent))
|
|
|
|
{
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
SurfaceInterface::~SurfaceInterface() = default;
|
|
|
|
|
2014-08-28 07:52:35 +00:00
|
|
|
void SurfaceInterface::create(wl_client *client, quint32 version, quint32 id)
|
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
d->create(client, version, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceInterface::Private::create(wl_client *c, quint32 version, quint32 id)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!surface);
|
|
|
|
Q_ASSERT(!client);
|
|
|
|
client = c;
|
|
|
|
surface = wl_resource_create(client, &wl_surface_interface, version, id);
|
|
|
|
if (!surface) {
|
2014-08-28 07:52:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
wl_resource_set_implementation(surface, &s_interface, this, unbind);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceInterface::frameRendered(quint32 msec)
|
|
|
|
{
|
|
|
|
// notify all callbacks
|
2014-09-19 05:06:31 +00:00
|
|
|
while (!d->current.callbacks.isEmpty()) {
|
|
|
|
wl_resource *r = d->current.callbacks.takeFirst();
|
2014-08-28 07:52:35 +00:00
|
|
|
wl_callback_send_done(r, msec);
|
|
|
|
wl_resource_destroy(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::unbind(wl_resource *r)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
auto s = cast(r);
|
|
|
|
s->surface = nullptr;
|
|
|
|
s->q->deleteLater();
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::destroy()
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
for (wl_resource *c : current.callbacks) {
|
2014-08-28 07:52:35 +00:00
|
|
|
wl_resource_destroy(c);
|
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
for (wl_resource *c : pending.callbacks) {
|
2014-08-28 07:52:35 +00:00
|
|
|
wl_resource_destroy(c);
|
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
if (current.buffer) {
|
|
|
|
current.buffer->unref();
|
2014-08-28 12:22:53 +00:00
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
if (surface) {
|
|
|
|
wl_resource_destroy(surface);
|
|
|
|
surface = nullptr;
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::commit()
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
for (wl_resource *c : current.callbacks) {
|
2014-08-28 07:52:35 +00:00
|
|
|
wl_resource_destroy(c);
|
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
const bool opaqueRegionChanged = current.opaque != pending.opaque;
|
|
|
|
const bool inputRegionChanged = current.input != pending.input;
|
|
|
|
const bool scaleFactorChanged = current.scale != pending.scale;
|
|
|
|
const bool transformFactorChanged = current.transform != pending.transform;
|
|
|
|
if (current.buffer) {
|
|
|
|
current.buffer->unref();
|
2014-08-28 12:22:53 +00:00
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
if (pending.buffer) {
|
|
|
|
pending.buffer->ref();
|
2014-08-28 12:22:53 +00:00
|
|
|
}
|
2014-08-28 07:52:35 +00:00
|
|
|
// copy values
|
2014-09-19 05:06:31 +00:00
|
|
|
current = pending;
|
|
|
|
pending = State{};
|
2014-08-28 07:52:35 +00:00
|
|
|
if (opaqueRegionChanged) {
|
2014-09-19 05:06:31 +00:00
|
|
|
emit q->opaqueChanged(current.opaque);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
if (inputRegionChanged) {
|
2014-09-19 05:06:31 +00:00
|
|
|
emit q->inputChanged(current.input);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
if (scaleFactorChanged) {
|
2014-09-19 05:06:31 +00:00
|
|
|
emit q->scaleChanged(current.scale);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
if (transformFactorChanged) {
|
2014-09-19 05:06:31 +00:00
|
|
|
emit q->transformChanged(current.transform);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
if (!current.damage.isEmpty()) {
|
|
|
|
emit q->damaged(current.damage);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::damage(const QRect &rect)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
// TODO: documentation says we need to remove the parts outside of the surface
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.damage = pending.damage.united(rect);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::setScale(qint32 scale)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.scale = scale;
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::setTransform(OutputInterface::Transform transform)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.transform = transform;
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::addFrameCallback(uint32_t callback)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
wl_resource *r = wl_resource_create(client, &wl_callback_interface, 1, callback);
|
2014-08-28 07:52:35 +00:00
|
|
|
if (!r) {
|
2014-09-19 05:06:31 +00:00
|
|
|
wl_resource_post_no_memory(surface);
|
2014-08-28 07:52:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_resource_set_implementation(r, nullptr, this, destroyFrameCallback);
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.callbacks << r;
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::attachBuffer(wl_resource *buffer, const QPoint &offset)
|
2014-08-28 12:22:53 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.offset = offset;
|
|
|
|
if (pending.buffer) {
|
|
|
|
delete pending.buffer;
|
2014-08-28 12:22:53 +00:00
|
|
|
}
|
2014-09-19 05:06:31 +00:00
|
|
|
pending.buffer = new BufferInterface(buffer, q);
|
2014-08-28 12:22:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::destroyFrameCallback(wl_resource *r)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
auto s = cast(r);
|
|
|
|
s->current.callbacks.removeAll(r);
|
|
|
|
s->pending.callbacks.removeAll(r);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::destroyCallback(wl_client *client, wl_resource *resource)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->q->deleteLater();
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::attachCallback(wl_client *client, wl_resource *resource, wl_resource *buffer, int32_t sx, int32_t sy)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->attachBuffer(buffer, QPoint(sx, sy));
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::damageCallback(wl_client *client, wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->damage(QRect(x, y, width, height));
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::frameCallaback(wl_client *client, wl_resource *resource, uint32_t callback)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
2014-09-19 05:06:31 +00:00
|
|
|
auto s = cast(resource);
|
|
|
|
Q_ASSERT(client == s->client);
|
2014-08-28 07:52:35 +00:00
|
|
|
s->addFrameCallback(callback);
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::opaqueRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
|
|
|
Q_UNUSED(resource)
|
|
|
|
Q_UNUSED(region)
|
|
|
|
// TODO: implement me
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::inputRegionCallback(wl_client *client, wl_resource *resource, wl_resource *region)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
|
|
|
Q_UNUSED(resource)
|
|
|
|
Q_UNUSED(region)
|
|
|
|
// TODO: implement me
|
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::commitCallback(wl_client *client, wl_resource *resource)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->commit();
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::bufferTransformCallback(wl_client *client, wl_resource *resource, int32_t transform)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->setTransform(OutputInterface::Transform(transform));
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 05:06:31 +00:00
|
|
|
void SurfaceInterface::Private::bufferScaleCallback(wl_client *client, wl_resource *resource, int32_t scale)
|
2014-08-28 07:52:35 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(client)
|
2014-09-19 05:06:31 +00:00
|
|
|
cast(resource)->setScale(scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_resource *SurfaceInterface::surface() const
|
|
|
|
{
|
|
|
|
return d->surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_client *SurfaceInterface::client() const
|
|
|
|
{
|
|
|
|
return d->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion SurfaceInterface::damage() const
|
|
|
|
{
|
|
|
|
return d->current.damage;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion SurfaceInterface::opaque() const
|
|
|
|
{
|
|
|
|
return d->current.opaque;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion SurfaceInterface::input() const
|
|
|
|
{
|
|
|
|
return d->current.input;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 SurfaceInterface::scale() const
|
|
|
|
{
|
|
|
|
return d->current.scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputInterface::Transform SurfaceInterface::transform() const
|
|
|
|
{
|
|
|
|
return d->current.transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferInterface *SurfaceInterface::buffer()
|
|
|
|
{
|
|
|
|
return d->current.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint SurfaceInterface::offset() const
|
|
|
|
{
|
|
|
|
return d->current.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceInterface *SurfaceInterface::get(wl_resource *native)
|
|
|
|
{
|
|
|
|
return Private::get(native);
|
2014-08-28 07:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|