A few fixes to prevent compositor crashes
[profile/ivi/qtwayland.git] / src / compositor / wayland_wrapper / wlsurfacebuffer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Qt Compositor.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "wlsurfacebuffer.h"
42
43 #include "wlsurface.h"
44 #include "wlcompositor.h"
45
46 #ifdef QT_COMPOSITOR_WAYLAND_GL
47 #include "hardware_integration/graphicshardwareintegration.h"
48 #include <qpa/qplatformopenglcontext.h>
49 #endif
50
51 #include <QtCore/QDebug>
52
53 namespace Wayland {
54
55 SurfaceBuffer::SurfaceBuffer(Surface *surface)
56     : QPlatformScreenBuffer()
57     , m_surface(surface)
58     , m_compositor(surface->compositor())
59     , m_buffer(0)
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)
64     , m_texture(0)
65     , m_is_shm_resolved(false)
66     , m_is_shm(false)
67 {
68 }
69
70 SurfaceBuffer::~SurfaceBuffer()
71 {
72     if (m_is_registered_for_buffer)
73         destructBufferState();
74 }
75
76 void SurfaceBuffer::initialize(wl_buffer *buffer)
77 {
78     m_buffer = buffer;
79     m_texture = 0;
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;
84     m_destroyed = false;
85     m_handle = 0;
86     m_is_shm_resolved = false;
87     m_is_shm = false;
88     m_destroy_listener.surfaceBuffer = this;
89     m_destroy_listener.listener.notify = destroy_listener_callback;
90     if (buffer)
91         wl_signal_add(&buffer->resource.destroy_signal, &m_destroy_listener.listener);
92     m_damageRect = QRect();
93 }
94
95 void SurfaceBuffer::destructBufferState()
96 {
97     Q_ASSERT(!m_page_flipper_has_buffer);
98
99     destroyTexture();
100
101     if (m_buffer) {
102         if (m_handle) {
103             if (m_is_shm) {
104                 delete static_cast<QImage *>(m_handle);
105 #ifdef QT_COMPOSITOR_WAYLAND_GL
106             } else {
107                 GraphicsHardwareIntegration *hwIntegration = m_compositor->graphicsHWIntegration();
108                 hwIntegration->unlockNativeBuffer(m_handle, m_compositor->directRenderContext());
109 #endif
110             }
111         }
112         wl_list_remove(&m_destroy_listener.listener.link);
113         sendRelease();
114     }
115     m_buffer = 0;
116     m_handle = 0;
117     m_is_registered_for_buffer = false;
118     m_is_displayed = false;
119 }
120
121 bool SurfaceBuffer::isShmBuffer() const
122 {
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;
127     }
128     return m_is_shm;
129 }
130
131 void SurfaceBuffer::sendRelease()
132 {
133     Q_ASSERT(m_buffer);
134     wl_buffer_send_release(&m_buffer->resource);
135 }
136
137 void SurfaceBuffer::setPageFlipperHasBuffer(bool owns)
138 {
139     m_page_flipper_has_buffer = owns;
140 }
141
142 void SurfaceBuffer::release()
143 {
144     m_compositor->scheduleReleaseBuffer(this);
145 }
146
147 void SurfaceBuffer::scheduledRelease()
148 {
149     m_page_flipper_has_buffer = false;
150     if (!m_surface_has_buffer)
151         destructBufferState();
152     if (!m_surface) {
153         delete this;
154     }
155 }
156
157 void SurfaceBuffer::disown()
158 {
159     m_surface_has_buffer = false;
160
161     if (!m_page_flipper_has_buffer) {
162         destructBufferState();
163     }
164 }
165
166 void SurfaceBuffer::setDisplayed()
167 {
168     m_is_displayed = true;
169     m_damageRect = QRect();
170 }
171
172 void SurfaceBuffer::setDamage(const QRect &rect)
173 {
174         if (m_damageRect.isValid()) {
175         m_damageRect = m_damageRect.united(rect);
176     }
177     m_damageRect = rect;
178 }
179
180 void SurfaceBuffer::destroyTexture()
181 {
182 #ifdef QT_COMPOSITOR_WAYLAND_GL
183         if (m_texture) {
184             glDeleteTextures(1,&m_texture);
185             m_texture = 0;
186         }
187 #endif
188 }
189
190 void SurfaceBuffer::handleAboutToBeDisplayed()
191 {
192     qDebug() << Q_FUNC_INFO;
193 }
194
195 void SurfaceBuffer::handleDisplayed()
196 {
197     qDebug() << Q_FUNC_INFO;
198 }
199
200 void *SurfaceBuffer::handle() const
201 {
202     if (!m_buffer)
203         return 0;
204
205     if (!m_handle) {
206         SurfaceBuffer *that = const_cast<SurfaceBuffer *>(this);
207         if (isShmBuffer()) {
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
215         } else {
216             GraphicsHardwareIntegration *hwIntegration = m_compositor->graphicsHWIntegration();
217             that->m_handle = hwIntegration->lockNativeBuffer(m_buffer, m_compositor->directRenderContext());
218 #endif
219         }
220     }
221     return m_handle;
222 }
223
224 void SurfaceBuffer::destroy_listener_callback(wl_listener *listener, void *data)
225 {
226         Q_UNUSED(data);
227         struct surface_buffer_destroy_listener *destroy_listener =
228                 reinterpret_cast<struct surface_buffer_destroy_listener *>(listener);
229         SurfaceBuffer *d = destroy_listener->surfaceBuffer;
230         d->destroyTexture();
231         d->m_destroyed = true;
232         d->m_buffer = 0;
233 }
234
235 void SurfaceBuffer::createTexture(GraphicsHardwareIntegration *hwIntegration, QOpenGLContext *context)
236 {
237 #ifdef QT_COMPOSITOR_WAYLAND_GL
238     m_texture = hwIntegration->createTextureFromBuffer(m_buffer, context);
239 #else
240     Q_UNUSED(hwIntegration);
241     Q_UNUSED(context);
242 #endif
243 }
244
245 }