~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/plugins/platforms/minimalegl/qminimaleglscreen.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
 
 
42
#include "qminimaleglscreen.h"
 
43
#include "qminimaleglwindow.h"
 
44
 
 
45
#include <QtPlatformSupport/private/qeglconvenience_p.h>
 
46
#include <QtPlatformSupport/private/qeglplatformcontext_p.h>
 
47
 
 
48
#ifdef Q_OPENKODE
 
49
#include <KD/kd.h>
 
50
#include <KD/NV_initialize.h>
 
51
#endif //Q_OPENKODE
 
52
 
 
53
QT_BEGIN_NAMESPACE
 
54
 
 
55
// #define QEGL_EXTRA_DEBUG
 
56
 
 
57
class QMinimalEglContext : public QEGLPlatformContext
 
58
{
 
59
public:
 
60
    QMinimalEglContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display,
 
61
                  EGLenum eglApi = EGL_OPENGL_ES_API)
 
62
        : QEGLPlatformContext(format, share, display, eglApi)
 
63
    {
 
64
    }
 
65
 
 
66
    EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface)
 
67
    {
 
68
        QMinimalEglWindow *window = static_cast<QMinimalEglWindow *>(surface);
 
69
        QMinimalEglScreen *screen = static_cast<QMinimalEglScreen *>(window->screen());
 
70
        return screen->surface();
 
71
    }
 
72
};
 
73
 
 
74
QMinimalEglScreen::QMinimalEglScreen(EGLNativeDisplayType display)
 
75
    : m_depth(32)
 
76
    , m_format(QImage::Format_Invalid)
 
77
    , m_platformContext(0)
 
78
    , m_surface(0)
 
79
{
 
80
#ifdef QEGL_EXTRA_DEBUG
 
81
    qWarning("QEglScreen %p\n", this);
 
82
#endif
 
83
 
 
84
    EGLint major, minor;
 
85
 
 
86
    if (!eglBindAPI(EGL_OPENGL_ES_API)) {
 
87
        qWarning("Could not bind GL_ES API\n");
 
88
        qFatal("EGL error");
 
89
    }
 
90
 
 
91
    m_dpy = eglGetDisplay(display);
 
92
    if (m_dpy == EGL_NO_DISPLAY) {
 
93
        qWarning("Could not open egl display\n");
 
94
        qFatal("EGL error");
 
95
    }
 
96
    qWarning("Opened display %p\n", m_dpy);
 
97
 
 
98
    if (!eglInitialize(m_dpy, &major, &minor)) {
 
99
        qWarning("Could not initialize egl display\n");
 
100
        qFatal("EGL error");
 
101
    }
 
102
 
 
103
    qWarning("Initialized display %d %d\n", major, minor);
 
104
 
 
105
    int swapInterval = 1;
 
106
    QByteArray swapIntervalString = qgetenv("QT_QPA_EGLFS_SWAPINTERVAL");
 
107
    if (!swapIntervalString.isEmpty()) {
 
108
        bool ok;
 
109
        swapInterval = swapIntervalString.toInt(&ok);
 
110
        if (!ok)
 
111
            swapInterval = 1;
 
112
    }
 
113
    eglSwapInterval(m_dpy, swapInterval);
 
114
}
 
115
 
 
116
QMinimalEglScreen::~QMinimalEglScreen()
 
117
{
 
118
    if (m_surface)
 
119
        eglDestroySurface(m_dpy, m_surface);
 
120
 
 
121
    eglTerminate(m_dpy);
 
122
}
 
123
 
 
124
void QMinimalEglScreen::createAndSetPlatformContext() const {
 
125
    const_cast<QMinimalEglScreen *>(this)->createAndSetPlatformContext();
 
126
}
 
127
 
 
128
void QMinimalEglScreen::createAndSetPlatformContext()
 
129
{
 
130
    QSurfaceFormat platformFormat;
 
131
 
 
132
    QByteArray depthString = qgetenv("QT_QPA_EGLFS_DEPTH");
 
133
    if (depthString.toInt() == 16) {
 
134
        platformFormat.setDepthBufferSize(16);
 
135
        platformFormat.setRedBufferSize(5);
 
136
        platformFormat.setGreenBufferSize(6);
 
137
        platformFormat.setBlueBufferSize(5);
 
138
        m_depth = 16;
 
139
        m_format = QImage::Format_RGB16;
 
140
    } else {
 
141
        platformFormat.setDepthBufferSize(24);
 
142
        platformFormat.setStencilBufferSize(8);
 
143
        platformFormat.setRedBufferSize(8);
 
144
        platformFormat.setGreenBufferSize(8);
 
145
        platformFormat.setBlueBufferSize(8);
 
146
        m_depth = 32;
 
147
        m_format = QImage::Format_RGB32;
 
148
    }
 
149
 
 
150
    if (!qEnvironmentVariableIsEmpty("QT_QPA_EGLFS_MULTISAMPLE"))
 
151
        platformFormat.setSamples(4);
 
152
 
 
153
    EGLConfig config = q_configFromGLFormat(m_dpy, platformFormat);
 
154
 
 
155
    EGLNativeWindowType eglWindow = 0;
 
156
#ifdef Q_OPENKODE
 
157
    if (kdInitializeNV() == KD_ENOTINITIALIZED) {
 
158
        qFatal("Did not manage to initialize openkode");
 
159
    }
 
160
    KDWindow *window = kdCreateWindow(m_dpy,config,0);
 
161
 
 
162
    kdRealizeWindow(window,&eglWindow);
 
163
#endif
 
164
 
 
165
#ifdef QEGL_EXTRA_DEBUG
 
166
    q_printEglConfig(m_dpy, config);
 
167
#endif
 
168
 
 
169
    m_surface = eglCreateWindowSurface(m_dpy, config, eglWindow, NULL);
 
170
    if (m_surface == EGL_NO_SURFACE) {
 
171
        qWarning("Could not create the egl surface: error = 0x%x\n", eglGetError());
 
172
        eglTerminate(m_dpy);
 
173
        qFatal("EGL error");
 
174
    }
 
175
    //    qWarning("Created surface %dx%d\n", w, h);
 
176
 
 
177
    QEGLPlatformContext *platformContext = new QMinimalEglContext(platformFormat, 0, m_dpy);
 
178
    m_platformContext = platformContext;
 
179
 
 
180
    EGLint w,h;                    // screen size detection
 
181
    eglQuerySurface(m_dpy, m_surface, EGL_WIDTH, &w);
 
182
    eglQuerySurface(m_dpy, m_surface, EGL_HEIGHT, &h);
 
183
 
 
184
    m_geometry = QRect(0,0,w,h);
 
185
 
 
186
}
 
187
 
 
188
QRect QMinimalEglScreen::geometry() const
 
189
{
 
190
    if (m_geometry.isNull()) {
 
191
        createAndSetPlatformContext();
 
192
    }
 
193
    return m_geometry;
 
194
}
 
195
 
 
196
int QMinimalEglScreen::depth() const
 
197
{
 
198
    return m_depth;
 
199
}
 
200
 
 
201
QImage::Format QMinimalEglScreen::format() const
 
202
{
 
203
    if (m_format == QImage::Format_Invalid)
 
204
        createAndSetPlatformContext();
 
205
    return m_format;
 
206
}
 
207
QPlatformOpenGLContext *QMinimalEglScreen::platformContext() const
 
208
{
 
209
    if (!m_platformContext) {
 
210
        QMinimalEglScreen *that = const_cast<QMinimalEglScreen *>(this);
 
211
        that->createAndSetPlatformContext();
 
212
    }
 
213
    return m_platformContext;
 
214
}
 
215
 
 
216
QT_END_NAMESPACE