diff --git a/src/wayland/server/buffer_interface.cpp b/src/wayland/server/buffer_interface.cpp index d511940935..cd6b7baca9 100644 --- a/src/wayland/server/buffer_interface.cpp +++ b/src/wayland/server/buffer_interface.cpp @@ -25,50 +25,70 @@ namespace KWayland namespace Server { +class BufferInterface::Private +{ +public: + Private(wl_resource *resource, SurfaceInterface *parent); + QImage::Format format() const; + void createImage(); + void releaseImage(); + wl_resource *buffer; + wl_shm_buffer *shmBuffer; + SurfaceInterface *surface; + int refCount; + QImage image; +}; + +BufferInterface::Private::Private(wl_resource *resource, SurfaceInterface *parent) + : buffer(resource) + , shmBuffer(wl_shm_buffer_get(resource)) + , surface(parent) + , refCount(0) +{ +} + + BufferInterface::BufferInterface(wl_resource *resource, SurfaceInterface *parent) : QObject(parent) - , m_buffer(resource) - , m_shmBuffer(wl_shm_buffer_get(m_buffer)) - , m_surface(parent) - , m_refCount(0) + , d(new Private(resource, parent)) { } BufferInterface::~BufferInterface() { - Q_ASSERT(m_refCount == 0); - releaseImage(); + Q_ASSERT(d->refCount == 0); + d->releaseImage(); } -void BufferInterface::releaseImage() +void BufferInterface::Private::releaseImage() { - if (m_image.isNull()) { + if (image.isNull()) { return; } // first destroy it - m_image = QImage(); - wl_shm_buffer_end_access(m_shmBuffer); + image = QImage(); + wl_shm_buffer_end_access(shmBuffer); } void BufferInterface::ref() { - m_refCount++; + d->refCount++; } void BufferInterface::unref() { - Q_ASSERT(m_refCount > 0); - m_refCount--; - if (m_refCount == 0) { - releaseImage(); - wl_buffer_send_release(m_buffer); + Q_ASSERT(d->refCount > 0); + d->refCount--; + if (d->refCount == 0) { + d->releaseImage(); + wl_buffer_send_release(d->buffer); deleteLater(); } } -QImage::Format BufferInterface::format() const +QImage::Format BufferInterface::Private::format() const { - switch (wl_shm_buffer_get_format(m_shmBuffer)) { + switch (wl_shm_buffer_get_format(shmBuffer)) { case WL_SHM_FORMAT_ARGB8888: return QImage::Format_ARGB32; case WL_SHM_FORMAT_XRGB8888: @@ -80,27 +100,42 @@ QImage::Format BufferInterface::format() const QImage BufferInterface::data() { - if (m_image.isNull()) { - createImage(); + if (d->image.isNull()) { + d->createImage(); } - return m_image; + return d->image; } -void BufferInterface::createImage() +void BufferInterface::Private::createImage() { - if (!m_shmBuffer) { + if (!shmBuffer) { return; } const QImage::Format imageFormat = format(); if (imageFormat == QImage::Format_Invalid) { return; } - wl_shm_buffer_begin_access(m_shmBuffer); - m_image = QImage((const uchar*)wl_shm_buffer_get_data(m_shmBuffer), - wl_shm_buffer_get_width(m_shmBuffer), - wl_shm_buffer_get_height(m_shmBuffer), - wl_shm_buffer_get_stride(m_shmBuffer), - imageFormat); + wl_shm_buffer_begin_access(shmBuffer); + image = QImage((const uchar*)wl_shm_buffer_get_data(shmBuffer), + wl_shm_buffer_get_width(shmBuffer), + wl_shm_buffer_get_height(shmBuffer), + wl_shm_buffer_get_stride(shmBuffer), + imageFormat); +} + +bool BufferInterface::isReferenced() const +{ + return d->refCount > 0; +} + +SurfaceInterface *BufferInterface::surface() const +{ + return d->surface; +} + +wl_shm_buffer *BufferInterface::shmBuffer() +{ + return d->shmBuffer; } } diff --git a/src/wayland/server/buffer_interface.h b/src/wayland/server/buffer_interface.h index 3d494d9de7..9cf84c5f3b 100644 --- a/src/wayland/server/buffer_interface.h +++ b/src/wayland/server/buffer_interface.h @@ -42,30 +42,18 @@ public: virtual ~BufferInterface(); void ref(); void unref(); - bool isReferenced() const { - return m_refCount > 0; - } + bool isReferenced() const; - SurfaceInterface *surface() const { - return m_surface; - } - wl_shm_buffer *shmBuffer() { - return m_shmBuffer; - } + SurfaceInterface *surface() const; + wl_shm_buffer *shmBuffer(); QImage data(); private: friend class SurfaceInterface; explicit BufferInterface(wl_resource *resource, SurfaceInterface *parent); - QImage::Format format() const; - void createImage(); - void releaseImage(); - wl_resource *m_buffer; - wl_shm_buffer *m_shmBuffer; - SurfaceInterface *m_surface; - int m_refCount; - QImage m_image; + class Private; + QScopedPointer d; }; }