1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qmeegolivepixmapdata.h"
43 #include "qmeegorasterpixmapdata.h"
44 #include <private/qimage_p.h>
45 #include <private/qwindowsurface_gl_p.h>
46 #include <private/qeglcontext_p.h>
47 #include <private/qapplication_p.h>
48 #include <private/qgraphicssystem_runtime_p.h>
49 #include <private/qpixmap_x11_p.h>
50 #include <stdio.h>
51 
52 static QMeeGoLivePixmapDataList all_live_pixmaps;
53 
54 static EGLint lock_attribs[] = {
55     EGL_MAP_PRESERVE_PIXELS_KHR, EGL_TRUE,
56     EGL_LOCK_USAGE_HINT_KHR, EGL_READ_SURFACE_BIT_KHR | EGL_WRITE_SURFACE_BIT_KHR,
57     EGL_NONE
58 };
59 
60 static EGLint preserved_attribs[] = {
61     EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
62     EGL_NONE
63 };
64 
65 // as copied from qwindowsurface.cpp
qt_scrollRectInImage(QImage & img,const QRect & rect,const QPoint & offset)66 void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
67 {
68     // make sure we don't detach
69     uchar *mem = const_cast<uchar*>(const_cast<const QImage &>(img).bits());
70 
71     int lineskip = img.bytesPerLine();
72     int depth = img.depth() >> 3;
73 
74     const QRect imageRect(0, 0, img.width(), img.height());
75     const QRect r = rect & imageRect & imageRect.translated(-offset);
76     const QPoint p = rect.topLeft() + offset;
77 
78     if (r.isEmpty())
79         return;
80 
81     const uchar *src;
82     uchar *dest;
83 
84     if (r.top() < p.y()) {
85         src = mem + r.bottom() * lineskip + r.left() * depth;
86         dest = mem + (p.y() + r.height() - 1) * lineskip + p.x() * depth;
87         lineskip = -lineskip;
88     } else {
89         src = mem + r.top() * lineskip + r.left() * depth;
90         dest = mem + p.y() * lineskip + p.x() * depth;
91     }
92 
93     const int w = r.width();
94     int h = r.height();
95     const int bytes = w * depth;
96 
97     // overlapping segments?
98     if (offset.y() == 0 && qAbs(offset.x()) < w) {
99         do {
100             ::memmove(dest, src, bytes);
101             dest += lineskip;
102             src += lineskip;
103         } while (--h);
104     } else {
105         do {
106             ::memcpy(dest, src, bytes);
107             dest += lineskip;
108             src += lineskip;
109         } while (--h);
110     }
111 }
112 
113 /* Public */
114 
QMeeGoLivePixmapData(int w,int h,QImage::Format format)115 QMeeGoLivePixmapData::QMeeGoLivePixmapData(int w, int h, QImage::Format format) : QGLPixmapData(QPixmapData::PixmapType)
116 {
117     QImage image(w, h, format);
118     QX11PixmapData *pmd = new QX11PixmapData(QPixmapData::PixmapType);
119     pmd->fromImage(image, Qt::NoOpaqueDetection);
120     backingX11Pixmap = new QPixmap(pmd);
121 
122     initializeThroughEGLImage();
123 
124     pos = all_live_pixmaps.insert(all_live_pixmaps.begin(), this);
125 }
126 
QMeeGoLivePixmapData(Qt::HANDLE h)127 QMeeGoLivePixmapData::QMeeGoLivePixmapData(Qt::HANDLE h) : QGLPixmapData(QPixmapData::PixmapType)
128 {
129     backingX11Pixmap = new QPixmap(QPixmap::fromX11Pixmap(h));
130     initializeThroughEGLImage();
131 
132     pos = all_live_pixmaps.insert(all_live_pixmaps.begin(), this);
133 }
134 
~QMeeGoLivePixmapData()135 QMeeGoLivePixmapData::~QMeeGoLivePixmapData()
136 {
137     delete backingX11Pixmap;
138     all_live_pixmaps.erase(pos);
139 }
140 
initializeThroughEGLImage()141 void QMeeGoLivePixmapData::initializeThroughEGLImage()
142 {
143     if (texture()->id != 0)
144         return;
145 
146     QGLShareContextScope ctx(qt_gl_share_widget()->context());
147     QMeeGoExtensions::ensureInitialized();
148 
149     EGLImageKHR eglImage = EGL_NO_IMAGE_KHR;
150     GLuint newTextureId = 0;
151 
152     eglImage = QEgl::eglCreateImageKHR(QEgl::display(), EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
153                                        (EGLClientBuffer) backingX11Pixmap->handle(), preserved_attribs);
154 
155     if (eglImage == EGL_NO_IMAGE_KHR) {
156         qWarning("eglCreateImageKHR failed (live texture)!");
157         return;
158     }
159 
160     glGenTextures(1, &newTextureId);
161     glBindTexture(GL_TEXTURE_2D, newTextureId);
162 
163     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (EGLImageKHR) eglImage);
164     if (glGetError() == GL_NO_ERROR) {
165         resize(backingX11Pixmap->width(), backingX11Pixmap->height());
166         texture()->id = newTextureId;
167         texture()->options &= ~QGLContext::InvertedYBindOption;
168         m_hasAlpha = backingX11Pixmap->hasAlphaChannel();
169     } else {
170         qWarning("Failed to create a texture from an egl image (live texture)!");
171         glDeleteTextures(1, &newTextureId);
172     }
173 
174     QEgl::eglDestroyImageKHR(QEgl::display(), eglImage);
175 }
176 
createCompatiblePixmapData() const177 QPixmapData *QMeeGoLivePixmapData::createCompatiblePixmapData() const
178 {
179     qWarning("Create compatible called on live pixmap! Expect fail soon...");
180     return new QMeeGoRasterPixmapData(pixelType());
181 }
182 
lock(EGLSyncKHR fenceSync)183 QImage* QMeeGoLivePixmapData::lock(EGLSyncKHR fenceSync)
184 {
185     QGLShareContextScope ctx(qt_gl_share_widget()->context());
186     QMeeGoExtensions::ensureInitialized();
187 
188     if (fenceSync) {
189         QMeeGoExtensions::eglClientWaitSyncKHR(QEgl::display(),
190                                                fenceSync,
191                                                EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
192                                                EGL_FOREVER_KHR);
193     }
194 
195     void *data = 0;
196     int pitch = 0;
197     int surfaceWidth = 0;
198     int surfaceHeight = 0;
199     EGLSurface surface = 0;
200     QImage::Format format;
201     lockedImage = QImage();
202 
203     surface = getSurfaceForBackingPixmap();
204     if (! QMeeGoExtensions::eglLockSurfaceKHR(QEgl::display(), surface, lock_attribs)) {
205         qWarning("Failed to lock surface (live texture)!");
206         return &lockedImage;
207     }
208 
209     eglQuerySurface(QEgl::display(), surface, EGL_BITMAP_POINTER_KHR, (EGLint*) &data);
210     eglQuerySurface(QEgl::display(), surface, EGL_BITMAP_PITCH_KHR, (EGLint*) &pitch);
211     eglQuerySurface(QEgl::display(), surface, EGL_WIDTH, (EGLint*) &surfaceWidth);
212     eglQuerySurface(QEgl::display(), surface, EGL_HEIGHT, (EGLint*) &surfaceHeight);
213 
214     // Ok, here we know we just support those two formats. Real solution would be:
215     // query also the format.
216     if (backingX11Pixmap->depth() > 16)
217         format = QImage::Format_ARGB32_Premultiplied;
218     else
219         format = QImage::Format_RGB16;
220 
221     if (data == NULL || pitch == 0) {
222         qWarning("Failed to query the live texture!");
223         return &lockedImage;
224     }
225 
226     if (width() != surfaceWidth || height() != surfaceHeight) {
227         qWarning("Live texture dimensions don't match!");
228         QMeeGoExtensions::eglUnlockSurfaceKHR(QEgl::display(), surface);
229         return &lockedImage;
230     }
231 
232     lockedImage = QImage((uchar *) data, width(), height(), pitch, format);
233     return &lockedImage;
234 }
235 
release(QImage *)236 bool QMeeGoLivePixmapData::release(QImage* /*img*/)
237 {
238     QGLShareContextScope ctx(qt_gl_share_widget()->context());
239     QMeeGoExtensions::ensureInitialized();
240 
241     if (QMeeGoExtensions::eglUnlockSurfaceKHR(QEgl::display(), getSurfaceForBackingPixmap())) {
242         lockedImage = QImage();
243         return true;
244     } else {
245         lockedImage = QImage();
246         return false;
247     }
248 }
249 
handle()250 Qt::HANDLE QMeeGoLivePixmapData::handle()
251 {
252     return backingX11Pixmap->handle();
253 }
254 
scroll(int dx,int dy,const QRect & rect)255 bool QMeeGoLivePixmapData::scroll(int dx, int dy, const QRect &rect)
256 {
257     lock(NULL);
258 
259     if (!lockedImage.isNull())
260         qt_scrollRectInImage(lockedImage, rect, QPoint(dx, dy));
261 
262     release(&lockedImage);
263     return true;
264 }
265 
getSurfaceForBackingPixmap()266 EGLSurface QMeeGoLivePixmapData::getSurfaceForBackingPixmap()
267 {
268     initializeThroughEGLImage();
269 
270     // This code is a crative remix of the stuff that can be found in the
271     // Qt's TFP implementation in /src/opengl/qgl_x11egl.cpp ::bindiTextureFromNativePixmap
272     QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(backingX11Pixmap->data_ptr().data());
273     Q_ASSERT(pixmapData->classId() == QPixmapData::X11Class);
274     bool hasAlpha = pixmapData->hasAlphaChannel();
275 
276     if (pixmapData->gl_surface &&
277         hasAlpha == (pixmapData->flags & QX11PixmapData::GlSurfaceCreatedWithAlpha))
278         return pixmapData->gl_surface;
279 
280     // Check to see if the surface is still valid
281     if (pixmapData->gl_surface &&
282         hasAlpha != ((pixmapData->flags & QX11PixmapData::GlSurfaceCreatedWithAlpha) > 0)) {
283         // Surface is invalid!
284         destroySurfaceForPixmapData(pixmapData);
285     }
286 
287     if (pixmapData->gl_surface == 0) {
288         EGLConfig config = QEgl::defaultConfig(QInternal::Pixmap,
289                                                QEgl::OpenGL,
290                                                hasAlpha ? QEgl::Translucent : QEgl::NoOptions);
291 
292         pixmapData->gl_surface = (void*)QEgl::createSurface(backingX11Pixmap, config);
293 
294         if (hasAlpha)
295             pixmapData->flags |= QX11PixmapData::GlSurfaceCreatedWithAlpha;
296         else
297             pixmapData->flags &= ~QX11PixmapData::GlSurfaceCreatedWithAlpha;
298 
299         if (pixmapData->gl_surface == (void*)EGL_NO_SURFACE)
300             return NULL;
301     }
302 
303     return pixmapData->gl_surface;
304 }
305 
destroySurfaceForPixmapData(QPixmapData * pmd)306 void QMeeGoLivePixmapData::destroySurfaceForPixmapData(QPixmapData* pmd)
307 {
308     Q_ASSERT(pmd->classId() == QPixmapData::X11Class);
309     QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(pmd);
310     if (pixmapData->gl_surface) {
311         eglDestroySurface(QEgl::display(), (EGLSurface)pixmapData->gl_surface);
312         pixmapData->gl_surface = 0;
313     }
314 }
315 
invalidateSurfaces()316 void QMeeGoLivePixmapData::invalidateSurfaces()
317 {
318     foreach (QMeeGoLivePixmapData *data, all_live_pixmaps) {
319         QX11PixmapData *pixmapData = static_cast<QX11PixmapData*>(data->backingX11Pixmap->data_ptr().data());
320         *data->texture() = QGLTexture();
321         pixmapData->gl_surface = 0;
322     }
323 }
324