1 /***************************************************************************
2 **
3 ** Copyright (C) 2013 - 2014 BlackBerry Limited. All rights reserved.
4 ** Contact: https://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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40
41 #include "qqnxeglwindow.h"
42 #include "qqnxscreen.h"
43 #include "qqnxglcontext.h"
44
45 #include <QDebug>
46
47 #include <errno.h>
48
49 #if defined(QQNXEGLWINDOW_DEBUG)
50 #define qEglWindowDebug qDebug
51 #else
52 #define qEglWindowDebug QT_NO_QDEBUG_MACRO
53 #endif
54
55 QT_BEGIN_NAMESPACE
56
QQnxEglWindow(QWindow * window,screen_context_t context,bool needRootWindow)57 QQnxEglWindow::QQnxEglWindow(QWindow *window, screen_context_t context, bool needRootWindow) :
58 QQnxWindow(window, context, needRootWindow),
59 m_newSurfaceRequested(true),
60 m_eglDisplay(EGL_NO_DISPLAY),
61 m_eglSurface(EGL_NO_SURFACE)
62 {
63 initWindow();
64
65 m_requestedBufferSize = shouldMakeFullScreen() ? screen()->geometry().size() : window->geometry().size();
66 }
67
~QQnxEglWindow()68 QQnxEglWindow::~QQnxEglWindow()
69 {
70 // Cleanup EGL surface if it exists
71 destroyEGLSurface();
72 }
73
isInitialized() const74 bool QQnxEglWindow::isInitialized() const
75 {
76 return m_eglSurface != EGL_NO_SURFACE;
77 }
78
ensureInitialized(QQnxGLContext * context)79 void QQnxEglWindow::ensureInitialized(QQnxGLContext* context)
80 {
81 if (m_newSurfaceRequested.testAndSetOrdered(true, false)) {
82 const QMutexLocker locker(&m_mutex); // Set geomety must not reset the requestedBufferSize till
83 // the surface is created
84
85 if (m_requestedBufferSize != bufferSize() || m_eglSurface == EGL_NO_SURFACE) {
86 if (m_eglSurface != EGL_NO_SURFACE) {
87 context->doneCurrent();
88 destroyEGLSurface();
89 }
90 createEGLSurface(context);
91 } else {
92 // Must've been a sequence of unprocessed changes returning us to the original size.
93 resetBuffers();
94 }
95 }
96 }
97
createEGLSurface(QQnxGLContext * context)98 void QQnxEglWindow::createEGLSurface(QQnxGLContext *context)
99 {
100 if (context->format().renderableType() != QSurfaceFormat::OpenGLES) {
101 qFatal("QQnxEglWindow: renderable type is not OpenGLES");
102 return;
103 }
104
105 // Set window usage
106 int usage = SCREEN_USAGE_OPENGL_ES2;
107 #if _SCREEN_VERSION >= _SCREEN_MAKE_VERSION(1, 0, 0)
108 if (context->format().majorVersion() == 3)
109 usage |= SCREEN_USAGE_OPENGL_ES3;
110 #endif
111
112 const int result = screen_set_window_property_iv(nativeHandle(), SCREEN_PROPERTY_USAGE, &usage);
113 if (Q_UNLIKELY(result != 0))
114 qFatal("QQnxEglWindow: failed to set window usage, errno=%d", errno);
115
116 if (!m_requestedBufferSize.isValid()) {
117 qWarning("QQNX: Trying to create 0 size EGL surface. "
118 "Please set a valid window size before calling QOpenGLContext::makeCurrent()");
119 return;
120 }
121
122 m_eglDisplay = context->eglDisplay();
123 m_eglConfig = context->eglConfig();
124 m_format = context->format();
125
126 // update the window's buffers before we create the EGL surface
127 setBufferSize(m_requestedBufferSize);
128
129 const EGLint eglSurfaceAttrs[] =
130 {
131 EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
132 EGL_NONE
133 };
134
135 qEglWindowDebug() << "Creating EGL surface from" << this << context
136 << window()->surfaceType() << window()->type();
137
138 // Create EGL surface
139 EGLSurface eglSurface = eglCreateWindowSurface(
140 m_eglDisplay,
141 m_eglConfig,
142 (EGLNativeWindowType) nativeHandle(),
143 eglSurfaceAttrs);
144
145 if (eglSurface == EGL_NO_SURFACE)
146 qWarning("QQNX: failed to create EGL surface, err=%d", eglGetError());
147
148 m_eglSurface = eglSurface;
149 }
150
destroyEGLSurface()151 void QQnxEglWindow::destroyEGLSurface()
152 {
153 // Destroy EGL surface if it exists
154 if (m_eglSurface != EGL_NO_SURFACE) {
155 EGLBoolean eglResult = eglDestroySurface(m_eglDisplay, m_eglSurface);
156 if (Q_UNLIKELY(eglResult != EGL_TRUE))
157 qFatal("QQNX: failed to destroy EGL surface, err=%d", eglGetError());
158 }
159
160 m_eglSurface = EGL_NO_SURFACE;
161 }
162
surface() const163 EGLSurface QQnxEglWindow::surface() const
164 {
165 return m_eglSurface;
166 }
167
setGeometry(const QRect & rect)168 void QQnxEglWindow::setGeometry(const QRect &rect)
169 {
170 //If this is the root window, it has to be shown fullscreen
171 const QRect &newGeometry = shouldMakeFullScreen() ? screen()->geometry() : rect;
172
173 //We need to request that the GL context updates
174 // the EGLsurface on which it is rendering.
175 {
176 // We want the setting of the atomic bool in the GL context to be atomic with
177 // setting m_requestedBufferSize and therefore extended the scope to include
178 // that test.
179 const QMutexLocker locker(&m_mutex);
180 m_requestedBufferSize = newGeometry.size();
181 if (isInitialized() && bufferSize() != newGeometry.size())
182 m_newSurfaceRequested.testAndSetRelease(false, true);
183 }
184 QQnxWindow::setGeometry(newGeometry);
185 }
186
pixelFormat() const187 int QQnxEglWindow::pixelFormat() const
188 {
189 // Extract size of color channels from window format
190 const int redSize = m_format.redBufferSize();
191 if (Q_UNLIKELY(redSize == -1))
192 qFatal("QQnxWindow: red size not defined");
193
194 const int greenSize = m_format.greenBufferSize();
195 if (Q_UNLIKELY(greenSize == -1))
196 qFatal("QQnxWindow: green size not defined");
197
198 const int blueSize = m_format.blueBufferSize();
199 if (Q_UNLIKELY(blueSize == -1))
200 qFatal("QQnxWindow: blue size not defined");
201
202 // select matching native format
203 if (redSize == 5 && greenSize == 6 && blueSize == 5)
204 return SCREEN_FORMAT_RGB565;
205 else if (redSize == 8 && greenSize == 8 && blueSize == 8)
206 return SCREEN_FORMAT_RGBA8888;
207
208 qFatal("QQnxWindow: unsupported pixel format");
209 }
210
resetBuffers()211 void QQnxEglWindow::resetBuffers()
212 {
213 m_requestedBufferSize = QSize();
214 }
215
216 QT_END_NAMESPACE
217