Qt 4.8
qwaylandglwindowsurface.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 
44 #include "qwaylanddisplay.h"
45 #include "qwaylandwindow.h"
46 #include "qwaylandscreen.h"
47 
48 #include <QtOpenGL/QGLFramebufferObject>
49 #include <QtOpenGL/QGLContext>
50 
51 #include <QtOpenGL/private/qglengineshadermanager_p.h>
52 
54 
55 static void drawTexture(const QRectF &rect, GLuint tex_id, const QSize &texSize, const QRectF &br)
56 {
57 #if !defined(QT_OPENGL_ES_2)
59 #endif
60  const GLenum target = GL_TEXTURE_2D;
61  QRectF src = br.isEmpty()
62  ? QRectF(QPointF(), texSize)
63  : QRectF(QPointF(br.x(), texSize.height() - br.bottom()), br.size());
64 
65  if (target == GL_TEXTURE_2D) {
66  qreal width = texSize.width();
67  qreal height = texSize.height();
68 
69  src.setLeft(src.left() / width);
70  src.setRight(src.right() / width);
71  src.setTop(src.top() / height);
72  src.setBottom(src.bottom() / height);
73  }
74 
75  const GLfloat tx1 = src.left();
76  const GLfloat tx2 = src.right();
77  const GLfloat ty1 = src.top();
78  const GLfloat ty2 = src.bottom();
79 
80  GLfloat texCoordArray[4*2] = {
81  tx1, ty2, tx2, ty2, tx2, ty1, tx1, ty1
82  };
83 
84  GLfloat vertexArray[4*2];
85  vertexArray[0] = rect.left(); vertexArray[1] = rect.top();
86  vertexArray[2] = rect.right(); vertexArray[3] = rect.top();
87  vertexArray[4] = rect.right(); vertexArray[5] = rect.bottom();
88  vertexArray[6] = rect.left(); vertexArray[7] = rect.bottom();
89 
92 
93  glBindTexture(target, tex_id);
94 
97  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
100 
101  glBindTexture(target, 0);
102 }
103 
104 static void blitTexture(QGLContext *ctx, GLuint texture, const QSize &viewport, const QSize &texSize, const QRect &targetRect, const QRect &sourceRect)
105 {
106  glDisable(GL_DEPTH_TEST);
107  glDisable(GL_SCISSOR_TEST);
108  glDisable(GL_BLEND);
109  glViewport(0, 0, viewport.width(), viewport.height());
110 
111  QGLShaderProgram *blitProgram =
113  blitProgram->bind();
114  blitProgram->setUniformValue("imageTexture", 0 /*QT_IMAGE_TEXTURE_UNIT*/);
115 
116  // The shader manager's blit program does not multiply the
117  // vertices by the pmv matrix, so we need to do the effect
118  // of the orthographic projection here ourselves.
119  QRectF r;
120  qreal w = viewport.width();
121  qreal h = viewport.height();
122  r.setLeft((targetRect.left() / w) * 2.0f - 1.0f);
123  if (targetRect.right() == (viewport.width() - 1))
124  r.setRight(1.0f);
125  else
126  r.setRight((targetRect.right() / w) * 2.0f - 1.0f);
127  r.setBottom((targetRect.top() / h) * 2.0f - 1.0f);
128  if (targetRect.bottom() == (viewport.height() - 1))
129  r.setTop(1.0f);
130  else
131  r.setTop((targetRect.bottom() / w) * 2.0f - 1.0f);
132 
133  drawTexture(r, texture, texSize, sourceRect);
134 }
135 
137  : QWindowSurface(window)
138  , mDisplay(QWaylandScreen::waylandScreenFromWidget(window)->display())
139  , mPaintDevice(0)
140 {
141 
142 }
143 
145 {
146  delete mPaintDevice;
147 }
148 
150 {
151  return mPaintDevice;
152 }
153 
155 {
156  window()->platformWindow()->glContext()->makeCurrent();
157  glClearColor(0,0,0,0xff);
158  glClear(GL_COLOR_BUFFER_BIT);
159 }
160 
162 {
163  Q_UNUSED(offset);
164  Q_UNUSED(region);
165  QWaylandWindow *ww = (QWaylandWindow *) widget->platformWindow();
166 
167  if (mPaintDevice->isBound())
169 
170  QRect rect(0,0,size().width(),size().height());
171  QGLContext *ctx = QGLContext::fromPlatformGLContext(ww->glContext());
173  ww->glContext()->swapBuffers();
174 }
175 
177 {
178  QWindowSurface::resize(size);
179  window()->platformWindow()->glContext()->makeCurrent();
180  delete mPaintDevice;
182 }
183 
QSize size() const
Returns the size of the texture attached to this framebuffer object.
GLuint texture() const
Returns the texture id for the texture attached as the default rendering target in this framebuffer o...
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
void setLeft(qreal pos)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:670
#define GL_DEPTH_TEST
QRect rect(const QWidget *widget) const
Returns the rectangle for widget in the coordinates of this window surface.
The QGLFramebufferObject class encapsulates an OpenGL framebuffer object.
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
void flush(QWidget *widget, const QRegion &region, const QPoint &offset)
Flushes the given region from the specified widget onto the screen.
void beginPaint(const QRegion &)
This function is called before painting onto the surface begins, with the region in which the paintin...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
void setTop(qreal pos)
Sets the top edge of the rectangle to the given y coordinate.
Definition: qrect.h:674
QGLShaderProgram * blitProgram()
#define glVertexAttribPointer
Definition: glfunctions.h:71
void setBottom(qreal pos)
Sets the bottom edge of the rectangle to the given y coordinate.
Definition: qrect.h:676
virtual QPoint offset(const QWidget *widget) const
Returns the offset of widget in the coordinates of this window surface.
static void blitTexture(QGLContext *ctx, GLuint texture, const QSize &viewport, const QSize &texSize, const QRect &targetRect, const QRect &sourceRect)
#define glEnableVertexAttribArray
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
#define GL_TEXTURE_2D
static QGLEngineSharedShaders * shadersForContext(const QGLContext *context)
void setRight(qreal pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:672
#define GL_SCISSOR_TEST
static const QGLContext * currentContext()
Returns the current context, i.e.
Definition: qgl.cpp:3545
#define GL_FALSE
NSWindow * window
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
void resize(const QSize &size)
QGLFramebufferObject * mPaintDevice
virtual void swapBuffers()=0
Reimplement in subclass to native swap buffers calls.
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
#define GL_COLOR_BUFFER_BIT
#define GL_TRIANGLE_FAN
#define GL_FLOAT
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
The QWindowSurface class provides the drawing area for top-level windows.
virtual QPlatformGLContext * glContext() const
Reimplement to return the glContext associated with the window.
#define GL_BLEND
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
static void drawTexture(const QRectF &rect, GLuint tex_id, const QSize &texSize, const QRectF &br)
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
unsigned int GLenum
Definition: main.cpp:50
qreal x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:664
#define glDisableVertexAttribArray
#define ctx
Definition: qgl.cpp:6094
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int height() const
Returns the height.
Definition: qsize.h:129
static const GLuint QT_TEXTURE_COORDS_ATTR
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool release()
Switches rendering back to the default, windowing system provided framebuffer.
QSizeF size() const
Returns the size of the rectangle.
Definition: qrect.h:713
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isBound() const
Returns true if the framebuffer object is currently bound to a context, otherwise false is returned...
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
static const GLuint QT_VERTEX_COORDS_ATTR
bool bind()
Binds this shader program to the active QGLContext and makes it the current shader program...
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:658
The QGLShaderProgram class allows OpenGL shader programs to be linked and used.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QWidget * window() const
Returns a pointer to the top-level window associated with this surface.
QPaintDevice * paintDevice()
Implement this function to return the appropriate paint device.