From 7b722dcd0711f32a5f08acf76bfb58f62c67a279 Mon Sep 17 00:00:00 2001 From: Morten Johan Sorvig Date: Fri, 23 Aug 2013 09:21:26 +0200 Subject: [PATCH] Implement high-dpi support Wayland 1.2 added support for display scaling, where wl_output has a "scale" event informing the the client that the compositor will scale the output surfaces by the given factor. The client then has the option of providing large surfaces to match the target pixel densety and bypass the compositor's scaling. This is done by calling wl_surface::set_buffer_scale. Re-use the current high-dpi support in Qt by implementing devicePixelRatio() for QWaylandScreen and QWaylandWindow. Provide high resolution buffers both for raster and OpenGL graphics. Introduce a new coordinate system: buffer coordinates, which is related to the window coordinate system via an "int scale" scale factor. This scale factor corresponds to Qts qreal devicePixelRatio, but we keep the name and the type in the QtWayland implementation. Change-Id: Ie10d7e5b4833480a9a25d96a26ad02150eb6e83f Reviewed-by: Giulio Camuffo --- .../platforms/wayland/qwaylandbuffer_p.h | 1 + .../platforms/wayland/qwaylandscreen.cpp | 20 ++++++++++++++++++- .../platforms/wayland/qwaylandscreen_p.h | 4 ++++ .../wayland/qwaylandshmbackingstore.cpp | 18 +++++++++++------ .../wayland/qwaylandshmbackingstore_p.h | 3 ++- .../platforms/wayland/qwaylandwindow.cpp | 17 +++++++++++++++- .../platforms/wayland/qwaylandwindow_p.h | 3 +++ 7 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/plugins/platforms/wayland/qwaylandbuffer_p.h b/src/plugins/platforms/wayland/qwaylandbuffer_p.h index 2bb9990f3dd..0c9903f5a04 100644 --- a/src/plugins/platforms/wayland/qwaylandbuffer_p.h +++ b/src/plugins/platforms/wayland/qwaylandbuffer_p.h @@ -58,6 +58,7 @@ public: virtual ~QWaylandBuffer() { } wl_buffer *buffer() {return mBuffer;} virtual QSize size() const = 0; + virtual int scale() const { return 1; } protected: struct wl_buffer *mBuffer; diff --git a/src/plugins/platforms/wayland/qwaylandscreen.cpp b/src/plugins/platforms/wayland/qwaylandscreen.cpp index 6e48c442e43..675c8404236 100644 --- a/src/plugins/platforms/wayland/qwaylandscreen.cpp +++ b/src/plugins/platforms/wayland/qwaylandscreen.cpp @@ -59,6 +59,7 @@ QWaylandScreen::QWaylandScreen(QWaylandDisplay *waylandDisplay, int version, uin , m_outputId(id) , mWaylandDisplay(waylandDisplay) , mExtendedOutput(0) + , mScale(1) , mDepth(32) , mRefreshRate(60000) , mTransform(-1) @@ -83,7 +84,9 @@ QWaylandDisplay * QWaylandScreen::display() const QRect QWaylandScreen::geometry() const { - return mGeometry; + // Scale geometry for QScreen. This makes window and screen + // geometry be in the same coordinate system. + return QRect(mGeometry.topLeft(), mGeometry.size() / mScale); } int QWaylandScreen::depth() const @@ -127,6 +130,16 @@ Qt::ScreenOrientation QWaylandScreen::orientation() const return m_orientation; } +int QWaylandScreen::scale() const +{ + return mScale; +} + +qreal QWaylandScreen::devicePixelRatio() const +{ + return qreal(mScale); +} + qreal QWaylandScreen::refreshRate() const { return mRefreshRate / 1000.f; @@ -188,6 +201,11 @@ void QWaylandScreen::output_geometry(int32_t x, int32_t y, mGeometry.moveTopLeft(QPoint(x, y)); } +void QWaylandScreen::output_scale(int32_t factor) +{ + mScale = factor; +} + void QWaylandScreen::output_done() { // the done event is sent after all the geometry and the mode events are sent, diff --git a/src/plugins/platforms/wayland/qwaylandscreen_p.h b/src/plugins/platforms/wayland/qwaylandscreen_p.h index d3173e0c9a1..d0c5d710fe4 100644 --- a/src/plugins/platforms/wayland/qwaylandscreen_p.h +++ b/src/plugins/platforms/wayland/qwaylandscreen_p.h @@ -72,6 +72,8 @@ public: void setOrientationUpdateMask(Qt::ScreenOrientations mask); Qt::ScreenOrientation orientation() const; + int scale() const; + qreal devicePixelRatio() const Q_DECL_OVERRIDE; qreal refreshRate() const; QString name() const { return mOutputName; } @@ -95,12 +97,14 @@ private: const QString &make, const QString &model, int32_t transform) Q_DECL_OVERRIDE; + void output_scale(int32_t factor) Q_DECL_OVERRIDE; void output_done() Q_DECL_OVERRIDE; int m_outputId; QWaylandDisplay *mWaylandDisplay; QWaylandExtendedOutput *mExtendedOutput; QRect mGeometry; + int mScale; int mDepth; int mRefreshRate; int mTransform; diff --git a/src/plugins/platforms/wayland/qwaylandshmbackingstore.cpp b/src/plugins/platforms/wayland/qwaylandshmbackingstore.cpp index d407335d3f7..9ec800138e9 100644 --- a/src/plugins/platforms/wayland/qwaylandshmbackingstore.cpp +++ b/src/plugins/platforms/wayland/qwaylandshmbackingstore.cpp @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE QWaylandShmBuffer::QWaylandShmBuffer(QWaylandDisplay *display, - const QSize &size, QImage::Format format) + const QSize &size, QImage::Format format, int scale) : mMarginsImage(0) { int stride = size.width() * 4; @@ -87,6 +87,8 @@ QWaylandShmBuffer::QWaylandShmBuffer(QWaylandDisplay *display, } mImage = QImage(data, size.width(), size.height(), stride, format); + mImage.setDevicePixelRatio(qreal(scale)); + mShmPool = wl_shm_create_pool(display->shm(), fd, alloc); mBuffer = wl_shm_pool_create_buffer(mShmPool,0, size.width(), size.height(), stride, WL_SHM_FORMAT_ARGB8888); @@ -101,8 +103,10 @@ QWaylandShmBuffer::~QWaylandShmBuffer(void) wl_shm_pool_destroy(mShmPool); } -QImage *QWaylandShmBuffer::imageInsideMargins(const QMargins &margins) +QImage *QWaylandShmBuffer::imageInsideMargins(const QMargins &marginsIn) { + QMargins margins = marginsIn * int(mImage.devicePixelRatio()); + if (!margins.isNull() && margins != mMargins) { if (mMarginsImage) { delete mMarginsImage; @@ -112,6 +116,7 @@ QImage *QWaylandShmBuffer::imageInsideMargins(const QMargins &margins) int b_s_width = mImage.size().width() - margins.left() - margins.right(); int b_s_height = mImage.size().height() - margins.top() - margins.bottom(); mMarginsImage = new QImage(b_s_data, b_s_width,b_s_height,mImage.bytesPerLine(),mImage.format()); + mMarginsImage->setDevicePixelRatio(mImage.devicePixelRatio()); } if (margins.isNull()) { delete mMarginsImage; @@ -228,7 +233,7 @@ void QWaylandShmBackingStore::flush(QWindow *window, const QRegion ®ion, cons if (damageAll) { //need to damage it all, otherwise the attach offset may screw up - waylandWindow()->damage(QRect(QPoint(0,0),mFrontBuffer->size())); + waylandWindow()->damage(QRect(QPoint(0,0), window->size())); } else { QVector rects = region.rects(); for (int i = 0; i < rects.size(); i++) { @@ -249,7 +254,8 @@ void QWaylandShmBackingStore::resize(const QSize &size, const QRegion &) void QWaylandShmBackingStore::resize(const QSize &size) { QMargins margins = windowDecorationMargins(); - QSize sizeWithMargins = size + QSize(margins.left()+margins.right(),margins.top()+margins.bottom()); + int scale = waylandWindow()->scale(); + QSize sizeWithMargins = (size + QSize(margins.left()+margins.right(),margins.top()+margins.bottom())) * scale; QImage::Format format = QPlatformScreen::platformScreenForWindow(window())->format(); @@ -260,7 +266,7 @@ void QWaylandShmBackingStore::resize(const QSize &size) delete mBackBuffer; //we delete the attached buffer when we flush } - mBackBuffer = new QWaylandShmBuffer(mDisplay, sizeWithMargins, format); + mBackBuffer = new QWaylandShmBuffer(mDisplay, sizeWithMargins, format, scale); if (windowDecoration() && window()->isVisible()) windowDecoration()->update(); @@ -354,7 +360,7 @@ void QWaylandShmBackingStore::done(void *data, wl_callback *callback, uint32_t t delete window->attached(); } window->attachOffset(self->mFrontBuffer); - window->damage(QRect(QPoint(0,0),self->mFrontBuffer->size())); + window->damage(QRect(QPoint(0,0),window->geometry().size())); window->commit(); } } diff --git a/src/plugins/platforms/wayland/qwaylandshmbackingstore_p.h b/src/plugins/platforms/wayland/qwaylandshmbackingstore_p.h index 1212e52fe33..b1e326c37af 100644 --- a/src/plugins/platforms/wayland/qwaylandshmbackingstore_p.h +++ b/src/plugins/platforms/wayland/qwaylandshmbackingstore_p.h @@ -57,9 +57,10 @@ class QWaylandWindow; class Q_WAYLAND_CLIENT_EXPORT QWaylandShmBuffer : public QWaylandBuffer { public: QWaylandShmBuffer(QWaylandDisplay *display, - const QSize &size, QImage::Format format); + const QSize &size, QImage::Format format, int scale = 1); ~QWaylandShmBuffer(); QSize size() const { return mImage.size(); } + int scale() const Q_DECL_OVERRIDE { return int(mImage.devicePixelRatio()); } QImage *image() { return &mImage; } QImage *imageInsideMargins(const QMargins &margins); diff --git a/src/plugins/platforms/wayland/qwaylandwindow.cpp b/src/plugins/platforms/wayland/qwaylandwindow.cpp index 9f7bdda1fa9..fd06972ff89 100644 --- a/src/plugins/platforms/wayland/qwaylandwindow.cpp +++ b/src/plugins/platforms/wayland/qwaylandwindow.cpp @@ -126,6 +126,12 @@ QWaylandWindow::QWaylandWindow(QWindow *window) mShellSurface->setTopLevel(); } + // Enable high-dpi rendering. Scale() returns the screen scale factor and will + // typically be integer 1 (normal-dpi) or 2 (high-dpi). Call set_buffer_scale() + // to inform the compositor that high-resolution buffers will be provided. + if (mDisplay->compositorVersion() >= 3) + set_buffer_scale(scale()); + setOrientationMask(window->screen()->orientationUpdateMask()); setWindowFlags(window->flags()); setGeometry_helper(window->geometry()); @@ -369,7 +375,6 @@ void QWaylandWindow::requestResize() void QWaylandWindow::attach(QWaylandBuffer *buffer, int x, int y) { mBuffer = buffer; - if (mBuffer) attach(mBuffer->buffer(), x, y); else @@ -707,6 +712,16 @@ bool QWaylandWindow::isExposed() const return QPlatformWindow::isExposed(); } +int QWaylandWindow::scale() const +{ + return screen()->scale(); +} + +qreal QWaylandWindow::devicePixelRatio() const +{ + return screen()->devicePixelRatio(); +} + bool QWaylandWindow::setMouseGrabEnabled(bool grab) { if (window()->type() != Qt::Popup) { diff --git a/src/plugins/platforms/wayland/qwaylandwindow_p.h b/src/plugins/platforms/wayland/qwaylandwindow_p.h index 0c55cd88d50..0d982d43e18 100644 --- a/src/plugins/platforms/wayland/qwaylandwindow_p.h +++ b/src/plugins/platforms/wayland/qwaylandwindow_p.h @@ -142,6 +142,9 @@ public: void setMask(const QRegion ®ion) Q_DECL_OVERRIDE; + int scale() const; + qreal devicePixelRatio() const Q_DECL_OVERRIDE; + void requestActivateWindow() Q_DECL_OVERRIDE; bool isExposed() const Q_DECL_OVERRIDE; void unfocus();