summaryrefslogtreecommitdiffstats
path: root/src/plugins/graphicssystems/shivavg/shivavgwindowsurface.cpp
blob: bf1f942ea08691726d65c66fc2eae8373cf0e737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#define GL_GLEXT_PROTOTYPES
#include "shivavgwindowsurface.h"
#include <QtOpenVG/private/qpaintengine_vg_p.h>
#if defined(Q_WS_X11)
#include "private/qt_x11_p.h"
#include "qx11info_x11.h"
#include <GL/glx.h>

extern QX11Info *qt_x11Info(const QPaintDevice *pd);
#endif

// Define this to use framebuffer objects.
//#define QVG_USE_FBO 1

#include <vg/openvg.h>

QT_BEGIN_NAMESPACE

class QShivaContext
{
public:
    QShivaContext();
    ~QShivaContext();

    bool makeCurrent(ShivaVGWindowSurfacePrivate *surface);
    void doneCurrent();

    bool initialized;
    QSize currentSize;
    ShivaVGWindowSurfacePrivate *currentSurface;
};

Q_GLOBAL_STATIC(QShivaContext, shivaContext);

class ShivaVGWindowSurfacePrivate
{
public:
    ShivaVGWindowSurfacePrivate()
        : isCurrent(false)
        , needsResize(true)
        , engine(0)
#if defined(QVG_USE_FBO)
        , fbo(0)
        , texture(0)
#endif
#if defined(Q_WS_X11)
        , drawable(0)
        , context(0)
#endif
    {
    }
    ~ShivaVGWindowSurfacePrivate();

    void ensureContext(QWidget *widget);

    QSize size;
    bool isCurrent;
    bool needsResize;
    QVGPaintEngine *engine;
#if defined(QVG_USE_FBO)
    GLuint fbo;
    GLuint texture;
#endif
#if defined(Q_WS_X11)
    GLXDrawable drawable;
    GLXContext context;
#endif
};

QShivaContext::QShivaContext()
    : initialized(false)
    , currentSurface(0)
{
}

QShivaContext::~QShivaContext()
{
    if (initialized)
        vgDestroyContextSH();
}

bool QShivaContext::makeCurrent(ShivaVGWindowSurfacePrivate *surface)
{
    if (currentSurface)
        currentSurface->isCurrent = false;
    surface->isCurrent = true;
    currentSurface = surface;
    currentSize = surface->size;
#if defined(Q_WS_X11)
    glXMakeCurrent(X11->display, surface->drawable, surface->context);
#endif
    if (!initialized) {
        if (!vgCreateContextSH(currentSize.width(), currentSize.height())) {
            qWarning("vgCreateContextSH(%d, %d): could not create context", currentSize.width(), currentSize.height());
            return false;
        }
        initialized = true;
    } else {
        vgResizeSurfaceSH(currentSize.width(), currentSize.height());
    }
#if defined(QVG_USE_FBO)
    if (surface->fbo)
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, surface->fbo);
    else
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
#endif
    return true;
}

void QShivaContext::doneCurrent()
{
    if (currentSurface) {
        currentSurface->isCurrent = false;
        currentSurface = 0;
    }
#if defined(Q_WS_X11)
    glXMakeCurrent(X11->display, 0, 0);
#endif
}

ShivaVGWindowSurfacePrivate::~ShivaVGWindowSurfacePrivate()
{
#if defined(QVG_USE_FBO)
    if (fbo) {
        glDeleteTextures(1, &texture);
        glDeleteFramebuffersEXT(1, &fbo);
    }
#endif
}

void ShivaVGWindowSurfacePrivate::ensureContext(QWidget *widget)
{
#if defined(Q_WS_X11)
    Window win = widget->winId();
    if (win != drawable) {
        if (context)
            glXDestroyContext(X11->display, context);
        drawable = win;
    }
    if (context == 0) {
        const QX11Info *xinfo = qt_x11Info(widget);
        int spec[64];
        int i = 0;
        spec[i++] = GLX_DOUBLEBUFFER;
        spec[i++] = GLX_DEPTH_SIZE;
        spec[i++] = 1;
        spec[i++] = GLX_STENCIL_SIZE;
        spec[i++] = 1;
        spec[i++] = GLX_RGBA;
        spec[i++] = GLX_RED_SIZE;
        spec[i++] = 1;
        spec[i++] = GLX_GREEN_SIZE;
        spec[i++] = 1;
        spec[i++] = GLX_BLUE_SIZE;
        spec[i++] = 1;
        spec[i++] = GLX_SAMPLE_BUFFERS_ARB;
        spec[i++] = 1;
        spec[i++] = GLX_SAMPLES_ARB;
        spec[i++] = 4;
        spec[i] = XNone;
        XVisualInfo *visual = glXChooseVisual
            (xinfo->display(), xinfo->screen(), spec);
        context = glXCreateContext(X11->display, visual, 0, True);
        if (!context)
            qWarning("glXCreateContext: could not create GL context for VG rendering");
    }
#else
    Q_UNUSED(widget);
#endif
#if defined(QVG_USE_FBO)
    if (needsResize && fbo) {
#if defined(Q_WS_X11)
        glXMakeCurrent(X11->display, drawable, context);
#endif
        glDeleteTextures(1, &texture);
        glDeleteFramebuffersEXT(1, &fbo);
#if defined(Q_WS_X11)
        glXMakeCurrent(X11->display, 0, 0);
#endif
        fbo = 0;
        texture = 0;
    }
    if (!fbo) {
#if defined(Q_WS_X11)
        glXMakeCurrent(X11->display, drawable, context);
#endif
        glGenFramebuffersEXT(1, &fbo);
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size.width(), size.height(), 0,
                     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glFramebufferTexture2DEXT
            (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
             texture, 0);

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
#if defined(Q_WS_X11)
        glXMakeCurrent(X11->display, 0, 0);
#endif
    }
#endif
    needsResize = false;
}

ShivaVGWindowSurface::ShivaVGWindowSurface(QWidget *window)
    : QWindowSurface(window), d_ptr(new ShivaVGWindowSurfacePrivate)
{
}

ShivaVGWindowSurface::~ShivaVGWindowSurface()
{
    if (d_ptr->isCurrent) {
        shivaContext()->doneCurrent();
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    }
#if defined(Q_WS_X11)
    if (d_ptr->context)
        glXDestroyContext(X11->display, d_ptr->context);
#endif
    delete d_ptr;
}

QPaintDevice *ShivaVGWindowSurface::paintDevice()
{
    d_ptr->ensureContext(window());
    shivaContext()->makeCurrent(d_ptr);
    glClearDepth(0.0f);
    glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    return this;
}

void ShivaVGWindowSurface::flush(QWidget *widget, const QRegion &region, const QPoint &offset)
{
    Q_UNUSED(region);
    Q_UNUSED(offset);
    QWidget *parent = widget->internalWinId() ? widget : widget->nativeParentWidget();
    d_ptr->ensureContext(parent);
    QShivaContext *context = shivaContext();
    if (!d_ptr->isCurrent)
        context->makeCurrent(d_ptr);
#if defined(QVG_USE_FBO)
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    if (d_ptr->fbo) {
        static GLfloat const vertices[][2] = {
            {-1, -1}, {1, -1}, {1, 1}, {-1, 1}
        };
        static GLfloat const texCoords[][2] = {
            {0, 0}, {1, 0}, {1, 1}, {0, 1}
        };
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
        glBindTexture(GL_TEXTURE_2D, d_ptr->texture);
        glEnable(GL_TEXTURE_2D);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
#endif
#if defined(Q_WS_X11)
    glXSwapBuffers(X11->display, d_ptr->drawable);
#endif
    context->doneCurrent();
}

void ShivaVGWindowSurface::setGeometry(const QRect &rect)
{
    QWindowSurface::setGeometry(rect);
    d_ptr->needsResize = true;
    d_ptr->size = rect.size();
}

bool ShivaVGWindowSurface::scroll(const QRegion &area, int dx, int dy)
{
    return QWindowSurface::scroll(area, dx, dy);
}

void ShivaVGWindowSurface::beginPaint(const QRegion &region)
{
    // Nothing to do here.
    Q_UNUSED(region);
}

void ShivaVGWindowSurface::endPaint(const QRegion &region)
{
    // Nothing to do here.
    Q_UNUSED(region);
}

Q_GLOBAL_STATIC(QVGPaintEngine, sharedPaintEngine);

QPaintEngine *ShivaVGWindowSurface::paintEngine() const
{
    if (!d_ptr->engine)
        d_ptr->engine = sharedPaintEngine();
    return d_ptr->engine;
}

// We need to get access to QWidget::metric() from ShivaVGWindowSurface::metric,
// but it is not a friend of QWidget.  To get around this, we create a
// fake QX11PaintEngine class, which is a friend.
class QX11PaintEngine
{
public:
    static int metric(const QWidget *widget, QPaintDevice::PaintDeviceMetric met)
    {
        return widget->metric(met);
    }
};

int ShivaVGWindowSurface::metric(PaintDeviceMetric met) const
{
    return QX11PaintEngine::metric(window(), met);
}

QT_END_NAMESPACE