1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "wlsurfacebuffer.h"
43 #include "wlsurface.h"
44 #include "wlcompositor.h"
46 #ifdef QT_COMPOSITOR_WAYLAND_GL
47 #include "hardware_integration/graphicshardwareintegration.h"
48 #include <qpa/qplatformopenglcontext.h>
51 #include <QtCore/QDebug>
55 SurfaceBuffer::SurfaceBuffer(Surface *surface)
56 : QPlatformScreenBuffer()
58 , m_compositor(surface->compositor())
60 , m_is_registered_for_buffer(false)
61 , m_surface_has_buffer(false)
62 , m_page_flipper_has_buffer(false)
63 , m_is_displayed(false)
65 , m_is_shm_resolved(false)
70 SurfaceBuffer::~SurfaceBuffer()
72 if (m_is_registered_for_buffer)
73 destructBufferState();
76 void SurfaceBuffer::initialize(wl_buffer *buffer)
80 m_is_registered_for_buffer = true;
81 m_surface_has_buffer = true;
82 m_page_flipper_has_buffer = false;
83 m_is_displayed = false;
86 m_is_shm_resolved = false;
88 m_destroy_listener.surfaceBuffer = this;
89 m_destroy_listener.listener.notify = destroy_listener_callback;
91 wl_signal_add(&buffer->resource.destroy_signal, &m_destroy_listener.listener);
92 m_damageRect = QRect();
95 void SurfaceBuffer::destructBufferState()
97 Q_ASSERT(!m_page_flipper_has_buffer);
104 delete static_cast<QImage *>(m_handle);
105 #ifdef QT_COMPOSITOR_WAYLAND_GL
107 GraphicsHardwareIntegration *hwIntegration = m_compositor->graphicsHWIntegration();
108 hwIntegration->unlockNativeBuffer(m_handle, m_compositor->directRenderContext());
112 wl_list_remove(&m_destroy_listener.listener.link);
117 m_is_registered_for_buffer = false;
118 m_is_displayed = false;
121 bool SurfaceBuffer::isShmBuffer() const
123 if (!m_is_shm_resolved) {
124 SurfaceBuffer *that = const_cast<SurfaceBuffer *>(this);
125 that->m_is_shm = wl_buffer_is_shm(m_buffer);
126 that->m_is_shm_resolved = true;
131 void SurfaceBuffer::sendRelease()
134 wl_buffer_send_release(&m_buffer->resource);
137 void SurfaceBuffer::setPageFlipperHasBuffer(bool owns)
139 m_page_flipper_has_buffer = owns;
142 void SurfaceBuffer::release()
144 m_compositor->scheduleReleaseBuffer(this);
147 void SurfaceBuffer::scheduledRelease()
149 m_page_flipper_has_buffer = false;
150 if (!m_surface_has_buffer)
151 destructBufferState();
157 void SurfaceBuffer::disown()
159 m_surface_has_buffer = false;
161 if (!m_page_flipper_has_buffer) {
162 destructBufferState();
166 void SurfaceBuffer::setDisplayed()
168 m_is_displayed = true;
169 m_damageRect = QRect();
172 void SurfaceBuffer::setDamage(const QRect &rect)
174 if (m_damageRect.isValid()) {
175 m_damageRect = m_damageRect.united(rect);
180 void SurfaceBuffer::destroyTexture()
182 #ifdef QT_COMPOSITOR_WAYLAND_GL
184 glDeleteTextures(1,&m_texture);
190 void SurfaceBuffer::handleAboutToBeDisplayed()
192 qDebug() << Q_FUNC_INFO;
195 void SurfaceBuffer::handleDisplayed()
197 qDebug() << Q_FUNC_INFO;
200 void *SurfaceBuffer::handle() const
206 SurfaceBuffer *that = const_cast<SurfaceBuffer *>(this);
208 const uchar *data = static_cast<const uchar *>(wl_shm_buffer_get_data(m_buffer));
209 int stride = wl_shm_buffer_get_stride(m_buffer);
210 int width = m_buffer->width;
211 int height = m_buffer->height;
212 QImage *image = new QImage(data,width,height,stride, QImage::Format_ARGB32_Premultiplied);
213 that->m_handle = image;
214 #ifdef QT_COMPOSITOR_WAYLAND_GL
216 GraphicsHardwareIntegration *hwIntegration = m_compositor->graphicsHWIntegration();
217 that->m_handle = hwIntegration->lockNativeBuffer(m_buffer, m_compositor->directRenderContext());
224 void SurfaceBuffer::destroy_listener_callback(wl_listener *listener, void *data)
227 struct surface_buffer_destroy_listener *destroy_listener =
228 reinterpret_cast<struct surface_buffer_destroy_listener *>(listener);
229 SurfaceBuffer *d = destroy_listener->surfaceBuffer;
231 d->m_destroyed = true;
235 void SurfaceBuffer::createTexture(GraphicsHardwareIntegration *hwIntegration, QOpenGLContext *context)
237 #ifdef QT_COMPOSITOR_WAYLAND_GL
238 m_texture = hwIntegration->createTextureFromBuffer(m_buffer, context);
240 Q_UNUSED(hwIntegration);