From f312eb3c05613a9ca988a92bf155264d619d8b6a Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Tue, 16 Oct 2018 09:06:20 +0200 Subject: Compositor: Implement support for the viewporter extension [ChangeLog][Compositor] Added support for the viewporter Wayland extension. Change-Id: I1d33652fab6ff18da4ae1ae3497f0ca43517420a Reviewed-by: Pier Luigi Fiorini --- src/3rdparty/protocol/qt_attribution.json | 17 + src/3rdparty/protocol/viewporter.xml | 186 +++++++++ .../compositor_api/qwaylandquickcompositor.cpp | 6 +- .../compositor_api/qwaylandquickitem.cpp | 4 + src/compositor/compositor_api/qwaylandsurface.cpp | 40 +- src/compositor/compositor_api/qwaylandsurface.h | 3 + src/compositor/compositor_api/qwaylandsurface_p.h | 4 + src/compositor/extensions/extensions.pri | 4 + src/compositor/extensions/qwaylandviewporter.cpp | 243 ++++++++++++ src/compositor/extensions/qwaylandviewporter.h | 63 +++ src/compositor/extensions/qwaylandviewporter_p.h | 93 +++++ sync.profile | 2 + tests/auto/compositor/compositor/compositor.pro | 3 +- tests/auto/compositor/compositor/mockclient.cpp | 2 + tests/auto/compositor/compositor/mockclient.h | 2 + .../auto/compositor/compositor/tst_compositor.cpp | 424 ++++++++++++++++++++- 16 files changed, 1089 insertions(+), 7 deletions(-) create mode 100644 src/3rdparty/protocol/viewporter.xml create mode 100644 src/compositor/extensions/qwaylandviewporter.cpp create mode 100644 src/compositor/extensions/qwaylandviewporter.h create mode 100644 src/compositor/extensions/qwaylandviewporter_p.h diff --git a/src/3rdparty/protocol/qt_attribution.json b/src/3rdparty/protocol/qt_attribution.json index 3b286dbd4..477293118 100644 --- a/src/3rdparty/protocol/qt_attribution.json +++ b/src/3rdparty/protocol/qt_attribution.json @@ -55,6 +55,23 @@ Copyright © 2015 Jonas Ådahl" Copyright (c) 2013 BMW Car IT GmbH" }, + { + "Id": "wayland-viewporter-protocol", + "Name": "Wayland Viewporter Protocol", + "QDocModule": "qtwaylandcompositor", + "QtUsage": "Used in the Qt Wayland Compositor API", + "Files": "viewporter.xml", + + "Description": "The Wayland viewporter extension allows a client to scale or crop a surface without modifying the buffer", + "Homepage": "https://wayland.freedesktop.org", + "Version": "1", + "DownloadLocation": "https://cgit.freedesktop.org/wayland/wayland-protocols/plain/stable/viewporter/viewporter.xml", + "LicenseId": "MIT", + "License": "MIT License", + "LicenseFile": "MIT_LICENSE.txt", + "Copyright": "Copyright © 2013-2016 Collabora, Ltd." + }, + { "Id": "wayland-xdg-decoration-protocol", "Name": "Wayland xdg-decoration Protocol", diff --git a/src/3rdparty/protocol/viewporter.xml b/src/3rdparty/protocol/viewporter.xml new file mode 100644 index 000000000..c732d8c35 --- /dev/null +++ b/src/3rdparty/protocol/viewporter.xml @@ -0,0 +1,186 @@ + + + + + Copyright © 2013-2016 Collabora, Ltd. + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + + The global interface exposing surface cropping and scaling + capabilities is used to instantiate an interface extension for a + wl_surface object. This extended interface will then allow + cropping and scaling the surface contents, effectively + disconnecting the direct relationship between the buffer and the + surface size. + + + + + Informs the server that the client will not be using this + protocol object anymore. This does not affect any other objects, + wp_viewport objects included. + + + + + + + + + + Instantiate an interface extension for the given wl_surface to + crop and scale its content. If the given wl_surface already has + a wp_viewport object associated, the viewport_exists + protocol error is raised. + + + + + + + + + An additional interface to a wl_surface object, which allows the + client to specify the cropping and scaling of the surface + contents. + + This interface works with two concepts: the source rectangle (src_x, + src_y, src_width, src_height), and the destination size (dst_width, + dst_height). The contents of the source rectangle are scaled to the + destination size, and content outside the source rectangle is ignored. + This state is double-buffered, and is applied on the next + wl_surface.commit. + + The two parts of crop and scale state are independent: the source + rectangle, and the destination size. Initially both are unset, that + is, no scaling is applied. The whole of the current wl_buffer is + used as the source, and the surface size is as defined in + wl_surface.attach. + + If the destination size is set, it causes the surface size to become + dst_width, dst_height. The source (rectangle) is scaled to exactly + this size. This overrides whatever the attached wl_buffer size is, + unless the wl_buffer is NULL. If the wl_buffer is NULL, the surface + has no content and therefore no size. Otherwise, the size is always + at least 1x1 in surface local coordinates. + + If the source rectangle is set, it defines what area of the wl_buffer is + taken as the source. If the source rectangle is set and the destination + size is not set, then src_width and src_height must be integers, and the + surface size becomes the source rectangle size. This results in cropping + without scaling. If src_width or src_height are not integers and + destination size is not set, the bad_size protocol error is raised when + the surface state is applied. + + The coordinate transformations from buffer pixel coordinates up to + the surface-local coordinates happen in the following order: + 1. buffer_transform (wl_surface.set_buffer_transform) + 2. buffer_scale (wl_surface.set_buffer_scale) + 3. crop and scale (wp_viewport.set*) + This means, that the source rectangle coordinates of crop and scale + are given in the coordinates after the buffer transform and scale, + i.e. in the coordinates that would be the surface-local coordinates + if the crop and scale was not applied. + + If src_x or src_y are negative, the bad_value protocol error is raised. + Otherwise, if the source rectangle is partially or completely outside of + the non-NULL wl_buffer, then the out_of_buffer protocol error is raised + when the surface state is applied. A NULL wl_buffer does not raise the + out_of_buffer error. + + The x, y arguments of wl_surface.attach are applied as normal to + the surface. They indicate how many pixels to remove from the + surface size from the left and the top. In other words, they are + still in the surface-local coordinate system, just like dst_width + and dst_height are. + + If the wl_surface associated with the wp_viewport is destroyed, + all wp_viewport requests except 'destroy' raise the protocol error + no_surface. + + If the wp_viewport object is destroyed, the crop and scale + state is removed from the wl_surface. The change will be applied + on the next wl_surface.commit. + + + + + The associated wl_surface's crop and scale state is removed. + The change is applied on the next wl_surface.commit. + + + + + + + + + + + + + Set the source rectangle of the associated wl_surface. See + wp_viewport for the description, and relation to the wl_buffer + size. + + If all of x, y, width and height are -1.0, the source rectangle is + unset instead. Any other set of values where width or height are zero + or negative, or x or y are negative, raise the bad_value protocol + error. + + The crop and scale state is double-buffered state, and will be + applied on the next wl_surface.commit. + + + + + + + + + + Set the destination size of the associated wl_surface. See + wp_viewport for the description, and relation to the wl_buffer + size. + + If width is -1 and height is -1, the destination size is unset + instead. Any other pair of values for width and height that + contains zero or negative values raises the bad_value protocol + error. + + The crop and scale state is double-buffered state, and will be + applied on the next wl_surface.commit. + + + + + + + diff --git a/src/compositor/compositor_api/qwaylandquickcompositor.cpp b/src/compositor/compositor_api/qwaylandquickcompositor.cpp index 8e8a903e3..426008a60 100644 --- a/src/compositor/compositor_api/qwaylandquickcompositor.cpp +++ b/src/compositor/compositor_api/qwaylandquickcompositor.cpp @@ -53,6 +53,7 @@ #include "qwaylandquickitem.h" #include "qwaylandoutput.h" #include +#include #include "qwaylandsurfacegrabber.h" QT_BEGIN_NAMESPACE @@ -60,8 +61,9 @@ QT_BEGIN_NAMESPACE class QWaylandQuickCompositorPrivate : public QWaylandCompositorPrivate { public: - QWaylandQuickCompositorPrivate(QWaylandCompositor *compositor) + explicit QWaylandQuickCompositorPrivate(QWaylandCompositor *compositor) : QWaylandCompositorPrivate(compositor) + , m_viewporter(new QWaylandViewporter(compositor)) { } protected: @@ -69,6 +71,8 @@ protected: { return new QWaylandQuickSurface(); } +private: + QScopedPointer m_viewporter; }; QWaylandQuickCompositor::QWaylandQuickCompositor(QObject *parent) diff --git a/src/compositor/compositor_api/qwaylandquickitem.cpp b/src/compositor/compositor_api/qwaylandquickitem.cpp index bc8365982..f848a0169 100644 --- a/src/compositor/compositor_api/qwaylandquickitem.cpp +++ b/src/compositor/compositor_api/qwaylandquickitem.cpp @@ -1317,6 +1317,10 @@ QSGNode *QWaylandQuickItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeDat d->provider->setSmooth(smooth()); node->setRect(rect); + qreal scale = surface()->bufferScale(); + QRectF source = surface()->sourceGeometry(); + node->setSourceRect(QRectF(source.topLeft() * scale, source.size() * scale)); + return node; } else { Q_ASSERT(!d->provider); diff --git a/src/compositor/compositor_api/qwaylandsurface.cpp b/src/compositor/compositor_api/qwaylandsurface.cpp index 8ad2389b6..eef51283c 100644 --- a/src/compositor/compositor_api/qwaylandsurface.cpp +++ b/src/compositor/compositor_api/qwaylandsurface.cpp @@ -235,6 +235,7 @@ void QWaylandSurfacePrivate::surface_commit(Resource *) // Needed in order to know whether we want to emit signals later QSize oldBufferSize = bufferSize; + QRectF oldSourceGeometry = sourceGeometry; QSize oldDestinationSize = destinationSize; bool oldHasContent = hasContent; int oldBufferScale = bufferScale; @@ -244,7 +245,9 @@ void QWaylandSurfacePrivate::surface_commit(Resource *) bufferRef = pending.buffer; bufferScale = pending.bufferScale; bufferSize = bufferRef.size(); - destinationSize = pending.destinationSize.isEmpty() ? bufferSize / bufferScale : pending.destinationSize; + QSize surfaceSize = bufferSize / bufferScale; + sourceGeometry = !pending.sourceGeometry.isValid() ? QRect(QPoint(), surfaceSize) : pending.sourceGeometry; + destinationSize = pending.destinationSize.isEmpty() ? sourceGeometry.size().toSize() : pending.destinationSize; damage = pending.damage.intersected(QRect(QPoint(), destinationSize)); hasContent = bufferRef.hasContent(); frameCallbacks << pendingFrameCallbacks; @@ -252,6 +255,9 @@ void QWaylandSurfacePrivate::surface_commit(Resource *) opaqueRegion = pending.opaqueRegion.intersected(QRect(QPoint(), destinationSize)); QPoint offsetForNextFrame = pending.offset; + if (viewport) + viewport->checkCommittedState(); + // Clear per-commit state pending.buffer = QWaylandBufferRef(); pending.offset = QPoint(); @@ -284,6 +290,9 @@ void QWaylandSurfacePrivate::surface_commit(Resource *) if (oldDestinationSize != destinationSize) emit q->destinationSizeChanged(); + if (oldSourceGeometry != sourceGeometry) + emit q->sourceGeometryChanged(); + if (oldHasContent != hasContent) emit q->hasContentChanged(); @@ -470,6 +479,35 @@ bool QWaylandSurface::hasContent() const return d->hasContent; } +/*! + * \qmlproperty rect QtWaylandCompositor::WaylandSurface::sourceGeometry + * + * This property describes the portion of the attached Wayland buffer that should + * be drawn on the screen. The coordinates are from the corner of the buffer and are + * scaled by \l bufferScale. + * + * \sa bufferScale + * \sa bufferSize + * \sa destinationSize + */ + +/*! + * \property QWaylandSurface::sourceGeometry + * + * This property describes the portion of the attached QWaylandBuffer that should + * be drawn on the screen. The coordinates are from the corner of the buffer and are + * scaled by \l bufferScale. + * + * \sa bufferScale + * \sa bufferSize + * \sa destinationSize + */ +QRectF QWaylandSurface::sourceGeometry() const +{ + Q_D(const QWaylandSurface); + return d->sourceGeometry; +} + /*! * \qmlproperty size QtWaylandCompositor::WaylandSurface::destinationSize * diff --git a/src/compositor/compositor_api/qwaylandsurface.h b/src/compositor/compositor_api/qwaylandsurface.h index 13f09f33c..9bf842900 100644 --- a/src/compositor/compositor_api/qwaylandsurface.h +++ b/src/compositor/compositor_api/qwaylandsurface.h @@ -81,6 +81,7 @@ class Q_WAYLAND_COMPOSITOR_EXPORT QWaylandSurface : public QWaylandObject Q_OBJECT Q_DECLARE_PRIVATE(QWaylandSurface) Q_PROPERTY(QWaylandClient *client READ client CONSTANT) + Q_PROPERTY(QRectF sourceGeometry READ sourceGeometry NOTIFY sourceGeometryChanged) Q_PROPERTY(QSize destinationSize READ destinationSize NOTIFY destinationSizeChanged) #if QT_DEPRECATED_SINCE(5, 13) Q_PROPERTY(QSize size READ size NOTIFY sizeChanged) // Qt 6: Remove @@ -113,6 +114,7 @@ public: bool hasContent() const; + QRectF sourceGeometry() const; QSize destinationSize() const; #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED QSize size() const; @@ -162,6 +164,7 @@ Q_SIGNALS: void damaged(const QRegion &rect); void parentChanged(QWaylandSurface *newParent, QWaylandSurface *oldParent); void childAdded(QWaylandSurface *child); + void sourceGeometryChanged(); void destinationSizeChanged(); #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED void sizeChanged(); diff --git a/src/compositor/compositor_api/qwaylandsurface_p.h b/src/compositor/compositor_api/qwaylandsurface_p.h index b34367801..85643623d 100644 --- a/src/compositor/compositor_api/qwaylandsurface_p.h +++ b/src/compositor/compositor_api/qwaylandsurface_p.h @@ -73,6 +73,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -144,6 +145,7 @@ public: //member variables QRegion damage; QWaylandBufferRef bufferRef; QWaylandSurfaceRole *role = nullptr; + QWaylandViewporterPrivate::Viewport *viewport = nullptr; struct { QWaylandBufferRef buffer; @@ -152,6 +154,7 @@ public: //member variables bool newlyAttached; QRegion inputRegion; int bufferScale; + QRectF sourceGeometry; QSize destinationSize; QRegion opaqueRegion; } pending; @@ -167,6 +170,7 @@ public: //member variables QRegion inputRegion; QRegion opaqueRegion; + QRectF sourceGeometry; QSize destinationSize; QSize bufferSize; int bufferScale = 1; diff --git a/src/compositor/extensions/extensions.pri b/src/compositor/extensions/extensions.pri index 5c708f891..38fe79a2f 100644 --- a/src/compositor/extensions/extensions.pri +++ b/src/compositor/extensions/extensions.pri @@ -9,6 +9,7 @@ WAYLANDSERVERSOURCES += \ ../extensions/qt-key-unstable-v1.xml \ ../extensions/qt-windowmanager.xml \ ../3rdparty/protocol/text-input-unstable-v2.xml \ + ../3rdparty/protocol/viewporter.xml \ ../3rdparty/protocol/xdg-shell-unstable-v6.xml \ ../3rdparty/protocol/xdg-shell.xml \ ../3rdparty/protocol/xdg-decoration-unstable-v1.xml \ @@ -27,6 +28,8 @@ HEADERS += \ extensions/qwaylandtextinputmanager_p.h \ extensions/qwaylandqtwindowmanager.h \ extensions/qwaylandqtwindowmanager_p.h \ + extensions/qwaylandviewporter.h \ + extensions/qwaylandviewporter_p.h \ extensions/qwaylandxdgshellv5.h \ extensions/qwaylandxdgshellv5_p.h \ extensions/qwaylandxdgshellv6.h \ @@ -49,6 +52,7 @@ SOURCES += \ extensions/qwaylandtextinput.cpp \ extensions/qwaylandtextinputmanager.cpp \ extensions/qwaylandqtwindowmanager.cpp \ + extensions/qwaylandviewporter.cpp \ extensions/qwaylandxdgshellv5.cpp \ extensions/qwaylandxdgshellv6.cpp \ extensions/qwaylandxdgshell.cpp \ diff --git a/src/compositor/extensions/qwaylandviewporter.cpp b/src/compositor/extensions/qwaylandviewporter.cpp new file mode 100644 index 000000000..3856c135d --- /dev/null +++ b/src/compositor/extensions/qwaylandviewporter.cpp @@ -0,0 +1,243 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWaylandCompositor module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwaylandviewporter_p.h" + +#include +#include + +#include + +QT_BEGIN_NAMESPACE + +/*! + \class QWaylandViewporter + \inmodule QtWaylandCompositor + \since 5.13 + \brief Provides an extension for surface resizing and cropping. + + The QWaylandViewporter extension provides a way for clients to resize and crop surface + contents. + + QWaylandViewporter corresponds to the Wayland interface, \c wp_viewporter. +*/ + +/*! + Constructs a QWaylandViewporter object. +*/ +QWaylandViewporter::QWaylandViewporter() + : QWaylandCompositorExtensionTemplate(*new QWaylandViewporterPrivate) +{ +} + +/*! + * Constructs a QWaylandViewporter object for the provided \a compositor. + */ +QWaylandViewporter::QWaylandViewporter(QWaylandCompositor *compositor) + : QWaylandCompositorExtensionTemplate(compositor, *new QWaylandViewporterPrivate()) +{ +} + +/*! + Initializes the extension. +*/ +void QWaylandViewporter::initialize() +{ + Q_D(QWaylandViewporter); + + QWaylandCompositorExtensionTemplate::initialize(); + auto *compositor = static_cast(extensionContainer()); + if (!compositor) { + qWarning() << "Failed to find QWaylandCompositor when initializing QWaylandViewporter"; + return; + } + d->init(compositor->display(), 1); +} + +/*! + Returns the Wayland interface for the QWaylandViewporter. +*/ +const wl_interface *QWaylandViewporter::interface() +{ + return QWaylandViewporterPrivate::interface(); +} + +void QWaylandViewporterPrivate::wp_viewporter_destroy(Resource *resource) +{ + // Viewport objects are allowed ot outlive the viewporter + wl_resource_destroy(resource->handle); +} + +void QWaylandViewporterPrivate::wp_viewporter_get_viewport(Resource *resource, uint id, wl_resource *surfaceResource) +{ + auto *surface = QWaylandSurface::fromResource(surfaceResource); + if (!surface) { + qWarning() << "Couldn't find surface for viewporter"; + return; + } + + auto *surfacePrivate = QWaylandSurfacePrivate::get(surface); + if (surfacePrivate->viewport) { + wl_resource_post_error(resource->handle, WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS, + "viewport already exists for surface"); + return; + } + + surfacePrivate->viewport = new Viewport(surface, resource->client(), id); +} + +QWaylandViewporterPrivate::Viewport::Viewport(QWaylandSurface *surface, wl_client *client, int id) + : QtWaylandServer::wp_viewport(client, id, /*version*/ 1) + , m_surface(surface) +{ + Q_ASSERT(surface); +} + +QWaylandViewporterPrivate::Viewport::~Viewport() +{ + if (m_surface) { + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + Q_ASSERT(surfacePrivate->viewport == this); + surfacePrivate->viewport = nullptr; + } +} + +// This function has to be called immediately after a surface is committed, before no +// other client events have been dispatched, or we may incorrectly error out on an +// incomplete pending state. See comment below. +void QWaylandViewporterPrivate::Viewport::checkCommittedState() +{ + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + + // We can't use the current state for destination/source when checking, + // as that has fallbacks to the buffer size so we can't distinguish + // between the set/unset case. We use the pending state because no other + // requests has modified it yet. + QSize destination = surfacePrivate->pending.destinationSize; + QRectF source = surfacePrivate->pending.sourceGeometry; + + if (!destination.isValid() && source.size() != source.size().toSize()) { + wl_resource_post_error(resource()->handle, error_bad_size, + "non-integer size (%fx%f) with unset destination", + source.width(), source.height()); + return; + } + + QRectF max = QRectF(QPointF(), m_surface->bufferSize() / m_surface->bufferScale()); + // We can't use QRectF.contains, because that would return false for values on the border + if (max.united(source) != max) { + wl_resource_post_error(resource()->handle, error_out_of_buffer, + "source %f,%f, %fx%f extends outside attached buffer %fx%f", + source.x(), source.y(), source.width(), source.height(), + max.width(), max.height()); + return; + } +} + + +void QWaylandViewporterPrivate::Viewport::wp_viewport_destroy_resource(Resource *resource) +{ + Q_UNUSED(resource); + delete this; +} + +void QWaylandViewporterPrivate::Viewport::wp_viewport_destroy(Resource *resource) +{ + if (m_surface) { + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + surfacePrivate->pending.destinationSize = QSize(); + surfacePrivate->pending.sourceGeometry = QRectF(); + } + wl_resource_destroy(resource->handle); +} + +void QWaylandViewporterPrivate::Viewport::wp_viewport_set_source(QtWaylandServer::wp_viewport::Resource *resource, wl_fixed_t x, wl_fixed_t y, wl_fixed_t width, wl_fixed_t height) +{ + Q_UNUSED(resource); + + if (!m_surface) { + wl_resource_post_error(resource->handle, error_no_surface, + "set_source requested for destroyed surface"); + return; + } + + QPointF position(wl_fixed_to_double(x), wl_fixed_to_double(y)); + QSizeF size(wl_fixed_to_double(width), wl_fixed_to_double(height)); + QRectF sourceGeometry(position, size); + + if (sourceGeometry == QRectF(-1, -1, -1, -1)) { + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + surfacePrivate->pending.sourceGeometry = QRectF(); + return; + } + + if (position.x() < 0 || position.y() < 0) { + wl_resource_post_error(resource->handle, error_bad_value, + "negative position in set_source"); + return; + } + + if (!size.isValid()) { + wl_resource_post_error(resource->handle, error_bad_value, + "negative size in set_source"); + return; + } + + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + surfacePrivate->pending.sourceGeometry = sourceGeometry; +} + +void QWaylandViewporterPrivate::Viewport::wp_viewport_set_destination(QtWaylandServer::wp_viewport::Resource *resource, int32_t width, int32_t height) +{ + Q_UNUSED(resource); + + if (!m_surface) { + wl_resource_post_error(resource->handle, error_no_surface, + "set_destination requested for destroyed surface"); + return; + } + + QSize destinationSize(width, height); + if (!destinationSize.isValid() && destinationSize != QSize(-1, -1)) { + wl_resource_post_error(resource->handle, error_bad_value, + "negative size in set_destination"); + return; + } + auto *surfacePrivate = QWaylandSurfacePrivate::get(m_surface); + surfacePrivate->pending.destinationSize = destinationSize; +} + +QT_END_NAMESPACE diff --git a/src/compositor/extensions/qwaylandviewporter.h b/src/compositor/extensions/qwaylandviewporter.h new file mode 100644 index 000000000..811c74145 --- /dev/null +++ b/src/compositor/extensions/qwaylandviewporter.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWaylandCompositor module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWAYLANDVIEWPORTER_H +#define QWAYLANDVIEWPORTER_H + +#include + +QT_BEGIN_NAMESPACE + +class QWaylandViewporterPrivate; + +class Q_WAYLAND_COMPOSITOR_EXPORT QWaylandViewporter + : public QWaylandCompositorExtensionTemplate +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QWaylandViewporter) + +public: + explicit QWaylandViewporter(); + explicit QWaylandViewporter(QWaylandCompositor *compositor); + + void initialize() override; + + static const struct wl_interface *interface(); +}; + +QT_END_NAMESPACE + +#endif // QWAYLANDVIEWPORTER_H diff --git a/src/compositor/extensions/qwaylandviewporter_p.h b/src/compositor/extensions/qwaylandviewporter_p.h new file mode 100644 index 000000000..d22da6990 --- /dev/null +++ b/src/compositor/extensions/qwaylandviewporter_p.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWaylandCompositor module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QWAYLANDVIEWPORTER_P_H +#define QWAYLANDVIEWPORTER_P_H + +#include "qwaylandviewporter.h" + +#include +#include + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE + +class QWaylandSurface; + +class Q_WAYLAND_COMPOSITOR_EXPORT QWaylandViewporterPrivate + : public QWaylandCompositorExtensionPrivate + , public QtWaylandServer::wp_viewporter +{ + Q_DECLARE_PUBLIC(QWaylandViewporter) +public: + explicit QWaylandViewporterPrivate() = default; + + class Q_WAYLAND_COMPOSITOR_EXPORT Viewport + : public QtWaylandServer::wp_viewport + { + public: + explicit Viewport(QWaylandSurface *surface, wl_client *client, int id); + ~Viewport() override; + void checkCommittedState(); + + protected: + void wp_viewport_destroy_resource(Resource *resource) override; + void wp_viewport_destroy(Resource *resource) override; + void wp_viewport_set_source(Resource *resource, wl_fixed_t x, wl_fixed_t y, wl_fixed_t width, wl_fixed_t height) override; + void wp_viewport_set_destination(Resource *resource, int32_t width, int32_t height) override; + + private: + QPointer m_surface = nullptr; + }; + +protected: + void wp_viewporter_destroy(Resource *resource) override; + void wp_viewporter_get_viewport(Resource *resource, uint32_t id, wl_resource *surface) override; +}; + +QT_END_NAMESPACE + +#endif // QWAYLANDVIEWPORTER_P_H diff --git a/sync.profile b/sync.profile index 4cf07fcde..147f2782f 100644 --- a/sync.profile +++ b/sync.profile @@ -61,6 +61,7 @@ "^qwayland-server-server-buffer-extension.h", "^qwayland-server-text-input-unstable-v2.h", "^qwayland-server-touch-extension.h", + "^qwayland-server-viewporter.h", "^qwayland-server-xdg-decoration-unstable-v1.h", "^qwayland-server-xdg-shell-unstable-v5.h", "^qwayland-server-xdg-shell-unstable-v6.h", @@ -71,6 +72,7 @@ "^wayland-qt-key-unstable-v1-server-protocol.h", "^wayland-server-buffer-extension-server-protocol.h", "^wayland-text-input-unstable-v2-server-protocol.h", + "^wayland-viewporter-server-protocol.h", "^wayland-touch-extension-server-protocol.h", "^wayland-wayland-server-protocol.h", "^wayland-xdg-decoration-unstable-v1-server-protocol.h", diff --git a/tests/auto/compositor/compositor/compositor.pro b/tests/auto/compositor/compositor/compositor.pro index 7cf40557b..4e5cf50b8 100644 --- a/tests/auto/compositor/compositor/compositor.pro +++ b/tests/auto/compositor/compositor/compositor.pro @@ -13,7 +13,8 @@ qtConfig(xkbcommon): \ WAYLANDCLIENTSOURCES += \ ../../../../src/3rdparty/protocol/xdg-shell-unstable-v5.xml \ ../../../../src/3rdparty/protocol/ivi-application.xml \ - ../../../../src/3rdparty/protocol/wayland.xml + ../../../../src/3rdparty/protocol/wayland.xml \ + ../../../../src/3rdparty/protocol/viewporter.xml SOURCES += \ tst_compositor.cpp \ diff --git a/tests/auto/compositor/compositor/mockclient.cpp b/tests/auto/compositor/compositor/mockclient.cpp index f74314407..b6cb4ab46 100644 --- a/tests/auto/compositor/compositor/mockclient.cpp +++ b/tests/auto/compositor/compositor/mockclient.cpp @@ -173,6 +173,8 @@ void MockClient::handleGlobal(uint32_t id, const QByteArray &interface) wl_output_add_listener(output, &outputListener, this); } else if (interface == "wl_shm") { shm = static_cast(wl_registry_bind(registry, id, &wl_shm_interface, 1)); + } else if (interface == "wp_viewporter") { + viewporter = static_cast(wl_registry_bind(registry, id, &wp_viewporter_interface, 1)); } else if (interface == "wl_shell") { wlshell = static_cast(wl_registry_bind(registry, id, &wl_shell_interface, 1)); } else if (interface == "xdg_shell") { diff --git a/tests/auto/compositor/compositor/mockclient.h b/tests/auto/compositor/compositor/mockclient.h index 1d9f32774..bf5d8fc88 100644 --- a/tests/auto/compositor/compositor/mockclient.h +++ b/tests/auto/compositor/compositor/mockclient.h @@ -29,6 +29,7 @@ #include "wayland-wayland-client-protocol.h" #include #include +#include "wayland-viewporter-client-protocol.h" #include #include @@ -69,6 +70,7 @@ public: wl_registry *registry = nullptr; wl_shell *wlshell = nullptr; xdg_shell *xdgShell = nullptr; + wp_viewporter *viewporter = nullptr; ivi_application *iviApplication = nullptr; QList m_seats; diff --git a/tests/auto/compositor/compositor/tst_compositor.cpp b/tests/auto/compositor/compositor/tst_compositor.cpp index 8335690f2..0e11618aa 100644 --- a/tests/auto/compositor/compositor/tst_compositor.cpp +++ b/tests/auto/compositor/compositor/tst_compositor.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,20 @@ private slots: void convertsXdgEdgesToQtEdges(); void xdgShellV6Positioner(); + + void viewporterGlobal(); + void viewportDestination(); + void viewportSource(); + void viewportSourceAndDestination(); + void viewportDestruction(); + void viewportProtocolErrors_data(); + void viewportProtocolErrors(); + void viewportClearDestination(); + void viewportClearSource(); + void viewportExistsError(); + void viewportDestinationNoSurfaceError(); + void viewportSourceNoSurfaceError(); + void viewportHiDpi(); }; void tst_WaylandCompositor::init() { @@ -435,7 +450,7 @@ void tst_WaylandCompositor::mapSurface() QSignalSpy hasContentSpy(waylandSurface, SIGNAL(hasContentChanged())); - QCOMPARE(waylandSurface->size(), QSize()); + QCOMPARE(waylandSurface->bufferSize(), QSize()); QCOMPARE(waylandSurface->destinationSize(), QSize()); QCOMPARE(waylandSurface->hasContent(), false); @@ -450,7 +465,7 @@ void tst_WaylandCompositor::mapSurface() QTRY_COMPARE(hasContentSpy.count(), 1); QCOMPARE(waylandSurface->hasContent(), true); - QCOMPARE(waylandSurface->size(), size); + QCOMPARE(waylandSurface->bufferSize(), size); QCOMPARE(waylandSurface->destinationSize(), size); wl_surface_destroy(surface); @@ -481,7 +496,7 @@ void tst_WaylandCompositor::mapSurfaceHiDpi() wl_surface_damage(surface, 0, 0, surfaceSize.width(), surfaceSize.height()); auto verifyComittedState = [=]() { - QCOMPARE(waylandSurface->size(), bufferSize); + QCOMPARE(waylandSurface->bufferSize(), bufferSize); QCOMPARE(waylandSurface->destinationSize(), surfaceSize); QCOMPARE(waylandSurface->bufferScale(), bufferScale); QCOMPARE(waylandSurface->hasContent(), true); @@ -514,7 +529,7 @@ void tst_WaylandCompositor::mapSurfaceHiDpi() QSignalSpy offsetSpy(waylandSurface, SIGNAL(offsetForNextFrame(const QPoint &))); // No state should be applied before the commit - QCOMPARE(waylandSurface->size(), QSize()); + QCOMPARE(waylandSurface->bufferSize(), QSize()); QCOMPARE(waylandSurface->destinationSize(), QSize()); QCOMPARE(waylandSurface->hasContent(), false); QCOMPARE(waylandSurface->bufferScale(), 1); @@ -1247,5 +1262,406 @@ void tst_WaylandCompositor::xdgShellV6Positioner() QCOMPARE(p.unconstrainedPosition(), QPoint(1 + 800 - 100 / 2 + 4, 2 + 600 / 2 - 50 + 8)); } +class ViewporterTestCompositor: public TestCompositor { + Q_OBJECT +public: + ViewporterTestCompositor() : viewporter(this) {} + QWaylandViewporter viewporter; +}; + +void tst_WaylandCompositor::viewporterGlobal() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); +} + +void tst_WaylandCompositor::viewportDestination() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + const QSize destinationSize(128, 123); + wp_viewport_set_destination(viewport, destinationSize.width(), destinationSize.height()); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->bufferSize(), bufferSize); + QCOMPARE(waylandSurface->destinationSize(), QSize(128, 123)); + QCOMPARE(waylandSurface->sourceGeometry(), QRect(QPoint(), bufferSize)); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportSource() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + const QRectF sourceGeometry(QPointF(10.5, 20.5), QSizeF(30, 40)); + wp_viewport_set_source(viewport, + wl_fixed_from_double(sourceGeometry.x()), + wl_fixed_from_double(sourceGeometry.y()), + wl_fixed_from_double(sourceGeometry.width()), + wl_fixed_from_double(sourceGeometry.height())); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->bufferSize(), bufferSize); + QCOMPARE(waylandSurface->destinationSize(), sourceGeometry.size().toSize()); + QCOMPARE(waylandSurface->sourceGeometry(), sourceGeometry); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportSourceAndDestination() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + + const QSize destinationSize(128, 123); + wp_viewport_set_destination(viewport, destinationSize.width(), destinationSize.height()); + + const QRectF sourceGeometry(QPointF(10, 20), QSizeF(30, 40)); + wp_viewport_set_source(viewport, + wl_fixed_from_double(sourceGeometry.x()), + wl_fixed_from_double(sourceGeometry.y()), + wl_fixed_from_double(sourceGeometry.width()), + wl_fixed_from_double(sourceGeometry.height())); + + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->bufferSize(), bufferSize); + QCOMPARE(waylandSurface->destinationSize(), destinationSize); + QCOMPARE(waylandSurface->sourceGeometry(), sourceGeometry); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportDestruction() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + + const QSize destinationSize(128, 123); + wp_viewport_set_destination(viewport, destinationSize.width(), destinationSize.height()); + + const QRectF sourceGeometry(QPointF(10, 20), QSizeF(30, 40)); + wp_viewport_set_source(viewport, + wl_fixed_from_double(sourceGeometry.x()), + wl_fixed_from_double(sourceGeometry.y()), + wl_fixed_from_double(sourceGeometry.width()), + wl_fixed_from_double(sourceGeometry.height())); + + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->bufferSize(), bufferSize); + QCOMPARE(waylandSurface->destinationSize(), destinationSize); + QCOMPARE(waylandSurface->sourceGeometry(), sourceGeometry); + + wp_viewport_destroy(viewport); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->destinationSize(), bufferSize); + QCOMPARE(waylandSurface->sourceGeometry(), QRectF(QPoint(), bufferSize)); + + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportProtocolErrors_data() +{ + QTest::addColumn("source"); + QTest::addColumn("destination"); + QTest::addColumn("error"); + + QTest::newRow("invalid source position") << QRectF(-1, 0, 16, 16) << QSize(64, 64) << uint(WP_VIEWPORT_ERROR_BAD_VALUE); + QTest::newRow("invalid source size") << QRectF(0, 0, -1, 16) << QSize(64, 64) << uint(WP_VIEWPORT_ERROR_BAD_VALUE); + QTest::newRow("invalid destination size") << QRectF(0, 0, 16, 16) << QSize(-16, 64) << uint(WP_VIEWPORT_ERROR_BAD_VALUE); + QTest::newRow("invalid non-integer source with unset size") << QRectF(0, 0, 15.5, 15.5) << QSize(-1, -1) << uint(WP_VIEWPORT_ERROR_BAD_SIZE); + QTest::newRow("bigger source than buffer") << QRectF(0, 0, 13337, 13337) << QSize(-1, -1) << uint(WP_VIEWPORT_ERROR_OUT_OF_BUFFER); +} + +void tst_WaylandCompositor::viewportProtocolErrors() +{ + QFETCH(QRectF, source); + QFETCH(QSize, destination); + QFETCH(uint, error); + + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + wp_viewport_set_source(viewport, + wl_fixed_from_double(source.x()), + wl_fixed_from_double(source.y()), + wl_fixed_from_double(source.width()), + wl_fixed_from_double(source.height())); + wp_viewport_set_destination(viewport, destination.width(), destination.height()); + wl_surface_commit(surface); + + QTRY_COMPARE(client.error, EPROTO); + QCOMPARE(client.protocolError.interface, &wp_viewport_interface); + QCOMPARE(static_cast(client.protocolError.code), error); +} + +void tst_WaylandCompositor::viewportClearDestination() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + + const QSize destinationSize(128, 123); + wp_viewport_set_destination(viewport, destinationSize.width(), destinationSize.height()); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->bufferSize(), bufferSize); + QCOMPARE(waylandSurface->destinationSize(), destinationSize); + + wp_viewport_set_destination(viewport, -1, -1); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->destinationSize(), bufferSize); + QCOMPARE(waylandSurface->sourceGeometry(), QRectF(QPoint(), bufferSize)); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportClearSource() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + QCOMPARE(waylandSurface->destinationSize(), QSize()); + QCOMPARE(waylandSurface->sourceGeometry(), QRect()); + + const QSize bufferSize(64, 64); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + QRectF source(10, 20, 30, 40); + wp_viewport_set_source(viewport, + wl_fixed_from_double(source.x()), + wl_fixed_from_double(source.y()), + wl_fixed_from_double(source.width()), + wl_fixed_from_double(source.height())); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->sourceGeometry(), source); + + wp_viewport_set_source(viewport, + wl_fixed_from_double(-1), + wl_fixed_from_double(-1), + wl_fixed_from_double(-1), + wl_fixed_from_double(-1)); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->sourceGeometry(), QRectF(QPoint(), bufferSize)); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); + QCOMPARE(client.error, 0); +} + +void tst_WaylandCompositor::viewportExistsError() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + wp_viewporter_get_viewport(client.viewporter, surface); + wp_viewporter_get_viewport(client.viewporter, surface); + + QTRY_COMPARE(client.error, EPROTO); + QCOMPARE(client.protocolError.interface, &wp_viewporter_interface); + QCOMPARE(static_cast(client.protocolError.code), WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS); +} + +void tst_WaylandCompositor::viewportDestinationNoSurfaceError() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + wl_surface_destroy(surface); + wp_viewport_set_destination(viewport, 32, 32); + + QTRY_COMPARE(client.error, EPROTO); + QCOMPARE(client.protocolError.interface, &wp_viewport_interface); + QCOMPARE(static_cast(client.protocolError.code), WP_VIEWPORT_ERROR_NO_SURFACE); +} + +void tst_WaylandCompositor::viewportSourceNoSurfaceError() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + wl_surface_destroy(surface); + wp_viewport_set_source(viewport, + wl_fixed_from_double(0), + wl_fixed_from_double(0), + wl_fixed_from_double(1), + wl_fixed_from_double(1)); + + QTRY_COMPARE(client.error, EPROTO); + QCOMPARE(client.protocolError.interface, &wp_viewport_interface); + QCOMPARE(static_cast(client.protocolError.code), WP_VIEWPORT_ERROR_NO_SURFACE); +} + +void tst_WaylandCompositor::viewportHiDpi() +{ + ViewporterTestCompositor compositor; + compositor.create(); + MockClient client; + QTRY_VERIFY(client.viewporter); + + wl_surface *surface = client.createSurface(); + QTRY_COMPARE(compositor.surfaces.size(), 1); + QWaylandSurface *waylandSurface = compositor.surfaces.at(0); + + const QSize bufferSize(128, 128); + ShmBuffer buffer(bufferSize, client.shm); + wl_surface_attach(surface, buffer.handle, 0, 0); + wl_surface_damage(surface, 0, 0, bufferSize.width(), bufferSize.height()); + constexpr int bufferScale = 2; + wl_surface_set_buffer_scale(surface, bufferScale); + + wl_surface_commit(surface); + QTRY_COMPARE(waylandSurface->destinationSize(), bufferSize / bufferScale); + + wp_viewport *viewport = wp_viewporter_get_viewport(client.viewporter, surface); + const QRectF sourceGeometry(QPointF(10, 20), QSizeF(30, 40)); + wp_viewport_set_source(viewport, + wl_fixed_from_double(sourceGeometry.x()), + wl_fixed_from_double(sourceGeometry.y()), + wl_fixed_from_double(sourceGeometry.width()), + wl_fixed_from_double(sourceGeometry.height())); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->destinationSize(), sourceGeometry.size()); + QCOMPARE(waylandSurface->sourceGeometry(), sourceGeometry); + QCOMPARE(waylandSurface->bufferSize(), bufferSize); + + const QSize destinationSize(128, 123); + wp_viewport_set_destination(viewport, destinationSize.width(), destinationSize.height()); + wl_surface_commit(surface); + + QTRY_COMPARE(waylandSurface->destinationSize(), destinationSize); + QCOMPARE(waylandSurface->sourceGeometry(), sourceGeometry); + QCOMPARE(waylandSurface->bufferSize(), bufferSize); + + QCOMPARE(client.error, 0); + + wp_viewport_destroy(viewport); + wl_surface_destroy(surface); +} + #include QTEST_MAIN(tst_WaylandCompositor); -- cgit v1.2.3