source: trunk/src/opengl/qwindowsurface_gl.cpp@ 636

Last change on this file since 636 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 27.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui/QApplication>
43#include <QtGui/QColormap>
44#include <QtGui/QDesktopWidget>
45#include <QtGui/QPaintDevice>
46#include <QtGui/QWidget>
47
48#include <qglframebufferobject.h>
49#include <qglpixelbuffer.h>
50#include <qcolormap.h>
51#include <qdesktopwidget.h>
52#include <private/qwidget_p.h>
53#include "qdebug.h"
54
55#ifdef Q_WS_X11
56#include <private/qt_x11_p.h>
57#include <qx11info_x11.h>
58
59#ifndef QT_OPENGL_ES
60#include <GL/glx.h>
61#include <X11/Xlib.h>
62#endif
63#endif //Q_WS_X11
64
65#include <private/qglextensions_p.h>
66#include <private/qwindowsurface_gl_p.h>
67
68#include <private/qgl_p.h>
69
70#include <private/qglpixelbuffer_p.h>
71#include <private/qgraphicssystem_gl_p.h>
72
73#include <private/qpaintengineex_opengl2_p.h>
74#include <private/qpixmapdata_gl_p.h>
75
76#ifndef QT_OPENGL_ES_2
77#include <private/qpaintengine_opengl_p.h>
78#endif
79
80#ifndef GLX_ARB_multisample
81#define GLX_SAMPLE_BUFFERS_ARB 100000
82#define GLX_SAMPLES_ARB 100001
83#endif
84
85#ifdef QT_OPENGL_ES_1_CL
86#include "qgl_cl_p.h"
87#endif
88
89#ifdef QT_OPENGL_ES
90#include <private/qegl_p.h>
91#endif
92
93QT_BEGIN_NAMESPACE
94
95//
96// QGLGraphicsSystem
97//
98#ifdef Q_WS_WIN
99extern Q_GUI_EXPORT bool qt_win_owndc_required;
100#endif
101QGLGraphicsSystem::QGLGraphicsSystem()
102 : QGraphicsSystem()
103{
104#if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
105 // only override the system defaults if the user hasn't already
106 // picked a visual
107 if (X11->visual == 0 && X11->visual_id == -1 && X11->visual_class == -1) {
108 // find a double buffered, RGBA visual that supports OpenGL
109 // and set that as the default visual for windows in Qt
110 int i = 0;
111 int spec[16];
112 spec[i++] = GLX_RGBA;
113 spec[i++] = GLX_DOUBLEBUFFER;
114
115 if (!qgetenv("QT_GL_SWAPBUFFER_PRESERVE").isNull()) {
116 spec[i++] = GLX_DEPTH_SIZE;
117 spec[i++] = 8;
118 spec[i++] = GLX_STENCIL_SIZE;
119 spec[i++] = 8;
120 spec[i++] = GLX_SAMPLE_BUFFERS_ARB;
121 spec[i++] = 1;
122 spec[i++] = GLX_SAMPLES_ARB;
123 spec[i++] = 4;
124 }
125
126 spec[i++] = XNone;
127
128 XVisualInfo *vi = glXChooseVisual(X11->display, X11->defaultScreen, spec);
129 if (vi) {
130 X11->visual_id = vi->visualid;
131 X11->visual_class = vi->c_class;
132
133 QGLFormat format;
134 int res;
135 glXGetConfig(X11->display, vi, GLX_LEVEL, &res);
136 format.setPlane(res);
137 glXGetConfig(X11->display, vi, GLX_DOUBLEBUFFER, &res);
138 format.setDoubleBuffer(res);
139 glXGetConfig(X11->display, vi, GLX_DEPTH_SIZE, &res);
140 format.setDepth(res);
141 if (format.depth())
142 format.setDepthBufferSize(res);
143 glXGetConfig(X11->display, vi, GLX_RGBA, &res);
144 format.setRgba(res);
145 glXGetConfig(X11->display, vi, GLX_RED_SIZE, &res);
146 format.setRedBufferSize(res);
147 glXGetConfig(X11->display, vi, GLX_GREEN_SIZE, &res);
148 format.setGreenBufferSize(res);
149 glXGetConfig(X11->display, vi, GLX_BLUE_SIZE, &res);
150 format.setBlueBufferSize(res);
151 glXGetConfig(X11->display, vi, GLX_ALPHA_SIZE, &res);
152 format.setAlpha(res);
153 if (format.alpha())
154 format.setAlphaBufferSize(res);
155 glXGetConfig(X11->display, vi, GLX_ACCUM_RED_SIZE, &res);
156 format.setAccum(res);
157 if (format.accum())
158 format.setAccumBufferSize(res);
159 glXGetConfig(X11->display, vi, GLX_STENCIL_SIZE, &res);
160 format.setStencil(res);
161 if (format.stencil())
162 format.setStencilBufferSize(res);
163 glXGetConfig(X11->display, vi, GLX_STEREO, &res);
164 format.setStereo(res);
165 glXGetConfig(X11->display, vi, GLX_SAMPLE_BUFFERS_ARB, &res);
166 format.setSampleBuffers(res);
167 if (format.sampleBuffers()) {
168 glXGetConfig(X11->display, vi, GLX_SAMPLES_ARB, &res);
169 format.setSamples(res);
170 }
171
172 QGLWindowSurface::surfaceFormat = format;
173 XFree(vi);
174
175 printf("using visual class %x, id %x\n", X11->visual_class, X11->visual_id);
176 }
177 }
178#elif defined(Q_WS_WIN)
179 QGLWindowSurface::surfaceFormat.setDoubleBuffer(true);
180
181 qt_win_owndc_required = true;
182#endif
183}
184
185//
186// QGLWindowSurface
187//
188
189class QGLGlobalShareWidget
190{
191public:
192 QGLGlobalShareWidget() : widget(0), initializing(false) {}
193
194 QGLWidget *shareWidget() {
195 if (!initializing && !widget && !cleanedUp) {
196 initializing = true;
197 widget = new QGLWidget;
198 // We dont need this internal widget to appear in QApplication::topLevelWidgets()
199 if (QWidgetPrivate::allWidgets)
200 QWidgetPrivate::allWidgets->remove(widget);
201 initializing = false;
202 }
203 return widget;
204 }
205
206 void cleanup() {
207 QGLWidget *w = widget;
208 cleanedUp = true;
209 widget = 0;
210 delete w;
211 }
212
213 static bool cleanedUp;
214
215private:
216 QGLWidget *widget;
217 bool initializing;
218};
219
220bool QGLGlobalShareWidget::cleanedUp = false;
221
222static void qt_cleanup_gl_share_widget();
223Q_GLOBAL_STATIC_WITH_INITIALIZER(QGLGlobalShareWidget, _qt_gl_share_widget,
224 {
225 qAddPostRoutine(qt_cleanup_gl_share_widget);
226 })
227
228static void qt_cleanup_gl_share_widget()
229{
230 _qt_gl_share_widget()->cleanup();
231}
232
233QGLWidget* qt_gl_share_widget()
234{
235 if (QGLGlobalShareWidget::cleanedUp)
236 return 0;
237 return _qt_gl_share_widget()->shareWidget();
238}
239
240
241struct QGLWindowSurfacePrivate
242{
243 QGLFramebufferObject *fbo;
244 QGLPixelBuffer *pb;
245 GLuint tex_id;
246 GLuint pb_tex_id;
247
248 int tried_fbo : 1;
249 int tried_pb : 1;
250 int destructive_swap_buffers : 1;
251 int geometry_updated : 1;
252
253 QGLContext *ctx;
254
255 QList<QGLContext **> contexts;
256
257 QRegion paintedRegion;
258 QSize size;
259
260 QList<QImage> buffers;
261 QGLWindowSurfaceGLPaintDevice glDevice;
262 QGLWindowSurface* q_ptr;
263};
264
265QGLFormat QGLWindowSurface::surfaceFormat;
266
267void QGLWindowSurfaceGLPaintDevice::endPaint()
268{
269 glFlush();
270 QGLPaintDevice::endPaint();
271}
272
273QSize QGLWindowSurfaceGLPaintDevice::size() const
274{
275 return d->size;
276}
277
278QGLContext* QGLWindowSurfaceGLPaintDevice::context() const
279{
280 return d->ctx;
281}
282
283
284int QGLWindowSurfaceGLPaintDevice::metric(PaintDeviceMetric m) const
285{
286 return qt_paint_device_metric(d->q_ptr->window(), m);
287}
288
289QPaintEngine *QGLWindowSurfaceGLPaintDevice::paintEngine() const
290{
291 return qt_qgl_paint_engine();
292}
293
294QGLWindowSurface::QGLWindowSurface(QWidget *window)
295 : QWindowSurface(window), d_ptr(new QGLWindowSurfacePrivate)
296{
297 Q_ASSERT(window->isTopLevel());
298 QGLExtensions::init();
299 d_ptr->pb = 0;
300 d_ptr->fbo = 0;
301 d_ptr->ctx = 0;
302#if defined (QT_OPENGL_ES_2)
303 d_ptr->tried_fbo = true;
304 d_ptr->tried_pb = true;
305#else
306 d_ptr->tried_fbo = false;
307 d_ptr->tried_pb = false;
308#endif
309 d_ptr->destructive_swap_buffers = qgetenv("QT_GL_SWAPBUFFER_PRESERVE").isNull();
310 d_ptr->glDevice.d = d_ptr;
311 d_ptr->q_ptr = this;
312 d_ptr->geometry_updated = false;
313}
314
315QGLWindowSurface::~QGLWindowSurface()
316{
317 if (d_ptr->ctx)
318 glDeleteTextures(1, &d_ptr->tex_id);
319 foreach(QGLContext **ctx, d_ptr->contexts) {
320 delete *ctx;
321 *ctx = 0;
322 }
323
324 delete d_ptr->pb;
325 delete d_ptr->fbo;
326 delete d_ptr;
327}
328
329void QGLWindowSurface::deleted(QObject *object)
330{
331 // Make sure that the fbo is destroyed before destroying its context.
332 delete d_ptr->fbo;
333 d_ptr->fbo = 0;
334
335 QWidget *widget = qobject_cast<QWidget *>(object);
336 if (widget) {
337 QWidgetPrivate *widgetPrivate = widget->d_func();
338 if (widgetPrivate->extraData()) {
339 union { QGLContext **ctxPtr; void **voidPtr; };
340 voidPtr = &widgetPrivate->extraData()->glContext;
341 int index = d_ptr->contexts.indexOf(ctxPtr);
342 if (index != -1) {
343 delete *ctxPtr;
344 *ctxPtr = 0;
345 d_ptr->contexts.removeAt(index);
346 }
347 }
348 }
349}
350
351void QGLWindowSurface::hijackWindow(QWidget *widget)
352{
353 QWidgetPrivate *widgetPrivate = widget->d_func();
354 widgetPrivate->createExtra();
355 if (widgetPrivate->extraData()->glContext)
356 return;
357
358 QGLContext *ctx = new QGLContext(surfaceFormat, widget);
359 ctx->create(qt_gl_share_widget()->context());
360
361#if defined(Q_WS_X11) && defined(QT_OPENGL_ES)
362 // Create the EGL surface to draw into. QGLContext::chooseContext()
363 // does not do this for X11/EGL, but does do it for other platforms.
364 // This probably belongs in qgl_x11egl.cpp.
365 QGLContextPrivate *ctxpriv = ctx->d_func();
366 ctxpriv->eglSurface = ctxpriv->eglContext->createSurface(widget);
367 if (ctxpriv->eglSurface == EGL_NO_SURFACE) {
368 qWarning() << "hijackWindow() could not create EGL surface";
369 }
370 qDebug("QGLWindowSurface - using EGLConfig %d", reinterpret_cast<int>(ctxpriv->eglContext->config()));
371#endif
372
373 widgetPrivate->extraData()->glContext = ctx;
374
375 union { QGLContext **ctxPtr; void **voidPtr; };
376
377 connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(deleted(QObject*)));
378
379 voidPtr = &widgetPrivate->extraData()->glContext;
380 d_ptr->contexts << ctxPtr;
381 qDebug() << "hijackWindow() context created for" << widget << d_ptr->contexts.size();
382}
383
384QGLContext *QGLWindowSurface::context() const
385{
386 return d_ptr->ctx;
387}
388
389QPaintDevice *QGLWindowSurface::paintDevice()
390{
391 updateGeometry();
392
393 if (d_ptr->pb)
394 return d_ptr->pb;
395
396 if (d_ptr->ctx)
397 return &d_ptr->glDevice;
398
399 QGLContext *ctx = reinterpret_cast<QGLContext *>(window()->d_func()->extraData()->glContext);
400 ctx->makeCurrent();
401 return d_ptr->fbo;
402}
403
404static void drawTexture(const QRectF &rect, GLuint tex_id, const QSize &texSize, const QRectF &src = QRectF());
405
406void QGLWindowSurface::beginPaint(const QRegion &)
407{
408}
409
410void QGLWindowSurface::endPaint(const QRegion &rgn)
411{
412 if (context())
413 d_ptr->paintedRegion |= rgn;
414
415 d_ptr->buffers.clear();
416}
417
418void QGLWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
419{
420 if (context() && widget != window()) {
421 qWarning("No native child widget support in GL window surface without FBOs or pixel buffers");
422 return;
423 }
424
425 //### Find out why d_ptr->geometry_updated isn't always false.
426 // flush() should not be called when d_ptr->geometry_updated is true. It assumes that either
427 // d_ptr->fbo or d_ptr->pb is allocated and has the correct size.
428 if (d_ptr->geometry_updated)
429 return;
430
431 QWidget *parent = widget->internalWinId() ? widget : widget->nativeParentWidget();
432 Q_ASSERT(parent);
433
434 if (!geometry().isValid())
435 return;
436
437 // Needed to support native child-widgets...
438 hijackWindow(parent);
439
440 QRect br = rgn.boundingRect().translated(offset);
441 br = br.intersected(window()->rect());
442 QPoint wOffset = qt_qwidget_data(parent)->wrect.topLeft();
443 QRect rect = br.translated(-offset - wOffset);
444
445 const GLenum target = GL_TEXTURE_2D;
446 Q_UNUSED(target);
447
448 if (context()) {
449 context()->makeCurrent();
450
451 if (context()->format().doubleBuffer()) {
452#if !defined(QT_OPENGL_ES_2)
453 if (d_ptr->destructive_swap_buffers) {
454 glBindTexture(target, d_ptr->tex_id);
455
456 QVector<QRect> rects = d_ptr->paintedRegion.rects();
457 for (int i = 0; i < rects.size(); ++i) {
458 QRect br = rects.at(i);
459 if (br.isEmpty())
460 continue;
461
462 const uint bottom = window()->height() - (br.y() + br.height());
463 glCopyTexSubImage2D(target, 0, br.x(), bottom, br.x(), bottom, br.width(), br.height());
464 }
465
466 glBindTexture(target, 0);
467
468 QRegion dirtyRegion = QRegion(window()->rect()) - d_ptr->paintedRegion;
469
470 if (!dirtyRegion.isEmpty()) {
471 glMatrixMode(GL_MODELVIEW);
472 glLoadIdentity();
473
474 glMatrixMode(GL_PROJECTION);
475 glLoadIdentity();
476#ifndef QT_OPENGL_ES
477 glOrtho(0, window()->width(), window()->height(), 0, -999999, 999999);
478#else
479 glOrthof(0, window()->width(), window()->height(), 0, -999999, 999999);
480#endif
481 glViewport(0, 0, window()->width(), window()->height());
482
483 QVector<QRect> rects = dirtyRegion.rects();
484 glColor4f(1, 1, 1, 1);
485 for (int i = 0; i < rects.size(); ++i) {
486 QRect rect = rects.at(i);
487 if (rect.isEmpty())
488 continue;
489
490 drawTexture(rect, d_ptr->tex_id, window()->size(), rect);
491 }
492 }
493 }
494#endif
495 d_ptr->paintedRegion = QRegion();
496 context()->swapBuffers();
497 } else {
498 glFlush();
499 }
500
501 return;
502 }
503
504 QGLContext *previous_ctx = const_cast<QGLContext *>(QGLContext::currentContext());
505 QGLContext *ctx = reinterpret_cast<QGLContext *>(parent->d_func()->extraData()->glContext);
506
507 // QPainter::end() should have unbound the fbo, otherwise something is very wrong...
508 Q_ASSERT(!d_ptr->fbo || !d_ptr->fbo->isBound());
509
510 if (ctx != previous_ctx) {
511 ctx->makeCurrent();
512 }
513
514 QSize size = widget->rect().size();
515 if (d_ptr->destructive_swap_buffers && ctx->format().doubleBuffer()) {
516 rect = parent->rect();
517 br = rect.translated(wOffset + offset);
518 size = parent->size();
519 }
520
521 glDisable(GL_SCISSOR_TEST);
522
523 if (d_ptr->fbo && (QGLExtensions::glExtensions & QGLExtensions::FramebufferBlit)) {
524 const int h = d_ptr->fbo->height();
525
526 const int sx0 = br.left();
527 const int sx1 = br.left() + br.width();
528 const int sy0 = h - (br.top() + br.height());
529 const int sy1 = h - br.top();
530
531 const int tx0 = rect.left();
532 const int tx1 = rect.left() + rect.width();
533 const int ty0 = parent->height() - (rect.top() + rect.height());
534 const int ty1 = parent->height() - rect.top();
535
536 if (window() == parent || d_ptr->fbo->format().samples() <= 1) {
537 // glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
538 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, d_ptr->fbo->handle());
539
540 glBlitFramebufferEXT(sx0, sy0, sx1, sy1,
541 tx0, ty0, tx1, ty1,
542 GL_COLOR_BUFFER_BIT,
543 GL_NEAREST);
544
545 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
546 } else {
547 // can't do sub-region blits with multisample FBOs
548 QGLFramebufferObject *temp = qgl_fbo_pool()->acquire(d_ptr->fbo->size(), QGLFramebufferObjectFormat());
549
550 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, temp->handle());
551 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, d_ptr->fbo->handle());
552
553 glBlitFramebufferEXT(0, 0, d_ptr->fbo->width(), d_ptr->fbo->height(),
554 0, 0, d_ptr->fbo->width(), d_ptr->fbo->height(),
555 GL_COLOR_BUFFER_BIT,
556 GL_NEAREST);
557
558 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, temp->handle());
559 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
560
561 glBlitFramebufferEXT(sx0, sy0, sx1, sy1,
562 tx0, ty0, tx1, ty1,
563 GL_COLOR_BUFFER_BIT,
564 GL_NEAREST);
565
566 glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
567
568 qgl_fbo_pool()->release(temp);
569 }
570 }
571#if !defined(QT_OPENGL_ES_2)
572 else {
573 GLuint texture;
574 if (d_ptr->fbo) {
575 texture = d_ptr->fbo->texture();
576 } else {
577 d_ptr->pb->makeCurrent();
578 glBindTexture(target, d_ptr->pb_tex_id);
579 const uint bottom = window()->height() - (br.y() + br.height());
580 glCopyTexSubImage2D(target, 0, br.x(), bottom, br.x(), bottom, br.width(), br.height());
581 texture = d_ptr->pb_tex_id;
582 glBindTexture(target, 0);
583 }
584
585 glDisable(GL_DEPTH_TEST);
586
587 if (d_ptr->fbo) {
588 d_ptr->fbo->release();
589 } else {
590 ctx->makeCurrent();
591 }
592
593 glMatrixMode(GL_MODELVIEW);
594 glLoadIdentity();
595
596 glMatrixMode(GL_PROJECTION);
597 glLoadIdentity();
598#ifndef QT_OPENGL_ES
599 glOrtho(0, size.width(), size.height(), 0, -999999, 999999);
600#else
601 glOrthof(0, size.width(), size.height(), 0, -999999, 999999);
602#endif
603 glViewport(0, 0, size.width(), size.height());
604
605 glColor4f(1, 1, 1, 1);
606 drawTexture(rect, texture, window()->size(), br);
607
608 if (d_ptr->fbo)
609 d_ptr->fbo->bind();
610 }
611#else
612 // OpenGL/ES 2.0 version of the fbo blit.
613 else if (d_ptr->fbo) {
614 Q_UNUSED(target);
615
616 GLuint texture = d_ptr->fbo->texture();
617
618 glDisable(GL_DEPTH_TEST);
619
620 if (d_ptr->fbo->isBound())
621 d_ptr->fbo->release();
622
623 glViewport(0, 0, size.width(), size.height());
624
625 QGLShaderProgram *blitProgram =
626 QGLEngineSharedShaders::shadersForContext(ctx)->blitProgram();
627 blitProgram->bind();
628 blitProgram->setUniformValue("imageTexture", 0 /*QT_IMAGE_TEXTURE_UNIT*/);
629
630 // The shader manager's blit program does not multiply the
631 // vertices by the pmv matrix, so we need to do the effect
632 // of the orthographic projection here ourselves.
633 QRectF r;
634 qreal w = size.width() ? size.width() : 1.0f;
635 qreal h = size.height() ? size.height() : 1.0f;
636 r.setLeft((rect.left() / w) * 2.0f - 1.0f);
637 if (rect.right() == (size.width() - 1))
638 r.setRight(1.0f);
639 else
640 r.setRight((rect.right() / w) * 2.0f - 1.0f);
641 r.setBottom((rect.top() / h) * 2.0f - 1.0f);
642 if (rect.bottom() == (size.height() - 1))
643 r.setTop(1.0f);
644 else
645 r.setTop((rect.bottom() / w) * 2.0f - 1.0f);
646
647 drawTexture(r, texture, window()->size(), br);
648 }
649#endif
650
651 if (ctx->format().doubleBuffer())
652 ctx->swapBuffers();
653 else
654 glFlush();
655}
656
657
658void QGLWindowSurface::setGeometry(const QRect &rect)
659{
660 QWindowSurface::setGeometry(rect);
661 d_ptr->geometry_updated = true;
662}
663
664
665void QGLWindowSurface::updateGeometry() {
666 if (!d_ptr->geometry_updated)
667 return;
668 d_ptr->geometry_updated = false;
669
670
671 QRect rect = geometry();
672 hijackWindow(window());
673 QGLContext *ctx = reinterpret_cast<QGLContext *>(window()->d_func()->extraData()->glContext);
674
675#ifdef Q_WS_MAC
676 ctx->updatePaintDevice();
677#endif
678
679 const GLenum target = GL_TEXTURE_2D;
680
681 if (rect.width() <= 0 || rect.height() <= 0)
682 return;
683
684 if (d_ptr->size == rect.size())
685 return;
686
687 d_ptr->size = rect.size();
688
689 if (d_ptr->ctx) {
690#ifndef QT_OPENGL_ES_2
691 if (d_ptr->destructive_swap_buffers) {
692 glBindTexture(target, d_ptr->tex_id);
693 glTexImage2D(target, 0, GL_RGBA, rect.width(), rect.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
694 glBindTexture(target, 0);
695 }
696#endif
697 return;
698 }
699
700 if (d_ptr->destructive_swap_buffers
701 && (QGLExtensions::glExtensions & QGLExtensions::FramebufferObject)
702 && (d_ptr->fbo || !d_ptr->tried_fbo)
703 && qt_gl_preferGL2Engine())
704 {
705 d_ptr->tried_fbo = true;
706 ctx->d_ptr->internal_context = true;
707 ctx->makeCurrent();
708 delete d_ptr->fbo;
709
710 QGLFramebufferObjectFormat format;
711 format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
712 format.setInternalTextureFormat(GLenum(GL_RGBA));
713 format.setTextureTarget(target);
714
715 if (QGLExtensions::glExtensions & QGLExtensions::FramebufferBlit)
716 format.setSamples(8);
717
718 d_ptr->fbo = new QGLFramebufferObject(rect.size(), format);
719
720 if (d_ptr->fbo->isValid()) {
721 qDebug() << "Created Window Surface FBO" << rect.size()
722 << "with samples" << d_ptr->fbo->format().samples();
723 return;
724 } else {
725 qDebug() << "QGLWindowSurface: Failed to create valid FBO, falling back";
726 delete d_ptr->fbo;
727 d_ptr->fbo = 0;
728 }
729 }
730
731#if !defined(QT_OPENGL_ES_2)
732 if (d_ptr->destructive_swap_buffers && (d_ptr->pb || !d_ptr->tried_pb)) {
733 d_ptr->tried_pb = true;
734
735 if (d_ptr->pb) {
736 d_ptr->pb->makeCurrent();
737 glDeleteTextures(1, &d_ptr->pb_tex_id);
738 }
739
740 delete d_ptr->pb;
741
742 d_ptr->pb = new QGLPixelBuffer(rect.width(), rect.height(),
743 QGLFormat(QGL::SampleBuffers | QGL::StencilBuffer | QGL::DepthBuffer),
744 qt_gl_share_widget());
745
746 if (d_ptr->pb->isValid()) {
747 qDebug() << "Created Window Surface Pixelbuffer, Sample buffers:" << d_ptr->pb->format().sampleBuffers();
748 d_ptr->pb->makeCurrent();
749
750 glGenTextures(1, &d_ptr->pb_tex_id);
751 glBindTexture(target, d_ptr->pb_tex_id);
752 glTexImage2D(target, 0, GL_RGBA, rect.width(), rect.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
753
754 glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
755 glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
756 glBindTexture(target, 0);
757
758 glMatrixMode(GL_PROJECTION);
759 glLoadIdentity();
760 glOrtho(0, d_ptr->pb->width(), d_ptr->pb->height(), 0, -999999, 999999);
761
762 d_ptr->pb->d_ptr->qctx->d_func()->internal_context = true;
763 return;
764 } else {
765 qDebug() << "QGLWindowSurface: Failed to create valid pixelbuffer, falling back";
766 delete d_ptr->pb;
767 d_ptr->pb = 0;
768 }
769 }
770#endif // !defined(QT_OPENGL_ES_2)
771
772 ctx->makeCurrent();
773
774#ifndef QT_OPENGL_ES_2
775 if (d_ptr->destructive_swap_buffers) {
776 glGenTextures(1, &d_ptr->tex_id);
777 glBindTexture(target, d_ptr->tex_id);
778 glTexImage2D(target, 0, GL_RGBA, rect.width(), rect.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
779
780 glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
781 glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
782 glBindTexture(target, 0);
783 }
784#endif
785
786 qDebug() << "QGLWindowSurface: Using plain widget as window surface" << this;;
787 d_ptr->ctx = ctx;
788 d_ptr->ctx->d_ptr->internal_context = true;
789}
790
791bool QGLWindowSurface::scroll(const QRegion &area, int dx, int dy)
792{
793 // this code randomly fails currently for unknown reasons
794 return false;
795
796 if (!d_ptr->pb)
797 return false;
798
799 d_ptr->pb->makeCurrent();
800
801 QRect br = area.boundingRect();
802
803#if 0
804 // ## workaround driver issue (scrolling by these deltas is unbearably slow for some reason)
805 // ## maybe we should use glCopyTexSubImage insteadk
806 if (dx == 1 || dx == -1 || dy == 1 || dy == -1 || dy == 2)
807 return false;
808
809 glRasterPos2i(br.x() + dx, br.y() + br.height() + dy);
810 glCopyPixels(br.x(), d_ptr->pb->height() - (br.y() + br.height()), br.width(), br.height(), GL_COLOR);
811 return true;
812#endif
813
814 const GLenum target = GL_TEXTURE_2D;
815
816 glBindTexture(target, d_ptr->tex_id);
817 glCopyTexImage2D(target, 0, GL_RGBA, br.x(), d_ptr->pb->height() - (br.y() + br.height()), br.width(), br.height(), 0);
818 glBindTexture(target, 0);
819
820 drawTexture(br.translated(dx, dy), d_ptr->tex_id, window()->size());
821
822 return true;
823}
824
825static void drawTexture(const QRectF &rect, GLuint tex_id, const QSize &texSize, const QRectF &br)
826{
827 const GLenum target = GL_TEXTURE_2D;
828 QRectF src = br.isEmpty()
829 ? QRectF(QPointF(), texSize)
830 : QRectF(QPointF(br.x(), texSize.height() - br.bottom()), br.size());
831
832 if (target == GL_TEXTURE_2D) {
833 qreal width = texSize.width();
834 qreal height = texSize.height();
835
836 src.setLeft(src.left() / width);
837 src.setRight(src.right() / width);
838 src.setTop(src.top() / height);
839 src.setBottom(src.bottom() / height);
840 }
841
842 const q_vertexType tx1 = f2vt(src.left());
843 const q_vertexType tx2 = f2vt(src.right());
844 const q_vertexType ty1 = f2vt(src.top());
845 const q_vertexType ty2 = f2vt(src.bottom());
846
847 q_vertexType texCoordArray[4*2] = {
848 tx1, ty2, tx2, ty2, tx2, ty1, tx1, ty1
849 };
850
851 q_vertexType vertexArray[4*2];
852 extern void qt_add_rect_to_array(const QRectF &r, q_vertexType *array); // qpaintengine_opengl.cpp
853 qt_add_rect_to_array(rect, vertexArray);
854
855#if !defined(QT_OPENGL_ES_2)
856 glVertexPointer(2, q_vertexTypeEnum, 0, vertexArray);
857 glTexCoordPointer(2, q_vertexTypeEnum, 0, texCoordArray);
858
859 glBindTexture(target, tex_id);
860 glEnable(target);
861
862 glEnableClientState(GL_VERTEX_ARRAY);
863 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
864 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
865 glDisableClientState(GL_VERTEX_ARRAY);
866 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
867
868 glDisable(target);
869 glBindTexture(target, 0);
870#else
871 glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexArray);
872 glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, texCoordArray);
873
874 glBindTexture(target, tex_id);
875
876 glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
877 glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
878 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
879 glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
880 glDisableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
881
882 glBindTexture(target, 0);
883#endif
884}
885
886QImage *QGLWindowSurface::buffer(const QWidget *widget)
887{
888 QImage image;
889
890 if (d_ptr->pb)
891 image = d_ptr->pb->toImage();
892 else if (d_ptr->fbo)
893 image = d_ptr->fbo->toImage();
894
895 if (image.isNull())
896 return 0;
897
898 QRect rect = widget->rect();
899 rect.translate(widget->mapTo(widget->window(), QPoint()));
900
901 QImage subImage = image.copy(rect);
902 d_ptr->buffers << subImage;
903 return &d_ptr->buffers.last();
904}
905
906
907
908QT_END_NAMESPACE
909
Note: See TracBrowser for help on using the repository browser.