1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickcontext2dtexture_p.h"
43 #include "qquickcontext2dtile_p.h"
44 #include "qquickcanvasitem_p.h"
45 #include <private/qquickitem_p.h>
46 #include <QtQuick/private/qsgtexture_p.h>
47 #include "qquickcontext2dcommandbuffer_p.h"
48 #include <QOpenGLPaintDevice>
50 #include <QOpenGLFramebufferObject>
51 #include <QOpenGLFramebufferObjectFormat>
52 #include <QtCore/QThread>
56 #define QT_MINIMUM_FBO_SIZE 64
58 static inline int qt_next_power_of_two(int v)
70 struct GLAcquireContext {
71 GLAcquireContext(QOpenGLContext *c, QSurface *s):ctx(c) {
78 qWarning() << "Unable to create GL context";
79 else if (!ctx->makeCurrent(s))
80 qWarning() << "Can't make current GL context";
90 QQuickContext2DTexture::QQuickContext2DTexture()
93 , m_dirtyCanvas(false)
94 , m_canvasWindowChanged(false)
95 , m_dirtyTexture(false)
97 , m_tiledCanvas(false)
102 QQuickContext2DTexture::~QQuickContext2DTexture()
107 QSize QQuickContext2DTexture::textureSize() const
109 return m_canvasWindow.size();
112 void QQuickContext2DTexture::markDirtyTexture()
114 m_dirtyTexture = true;
116 emit textureChanged();
119 bool QQuickContext2DTexture::setCanvasSize(const QSize &size)
121 if (m_canvasSize != size) {
123 m_dirtyCanvas = true;
129 bool QQuickContext2DTexture::setTileSize(const QSize &size)
131 if (m_tileSize != size) {
133 m_dirtyCanvas = true;
139 void QQuickContext2DTexture::setSmooth(bool smooth)
144 void QQuickContext2DTexture::setItem(QQuickCanvasItem* item)
147 m_context = (QQuickContext2D*)item->rawContext(); // FIXME
148 m_state = m_context->state;
151 bool QQuickContext2DTexture::setCanvasWindow(const QRect& r)
153 if (m_canvasWindow != r) {
155 m_canvasWindowChanged = true;
161 bool QQuickContext2DTexture::setDirtyRect(const QRect &r)
163 bool doDirty = false;
165 foreach (QQuickContext2DTile* t, m_tiles) {
166 bool dirty = t->rect().intersected(r).isValid();
172 doDirty = m_canvasWindow.intersected(r).isValid();
177 void QQuickContext2DTexture::canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth)
180 if (ts.width() > canvasSize.width())
181 ts.setWidth(canvasSize.width());
183 if (ts.height() > canvasSize.height())
184 ts.setHeight(canvasSize.height());
186 setCanvasSize(canvasSize);
188 setCanvasWindow(canvasWindow);
190 if (canvasSize == canvasWindow.size()) {
191 m_tiledCanvas = false;
192 m_dirtyCanvas = false;
194 m_tiledCanvas = true;
197 if (dirtyRect.isValid())
198 setDirtyRect(dirtyRect);
203 void QQuickContext2DTexture::paintWithoutTiles(QQuickContext2DCommandBuffer *ccb)
205 if (!ccb || ccb->isEmpty())
208 QPaintDevice* device = beginPainting();
217 p.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
218 | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
220 p.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
221 | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, false);
222 p.setCompositionMode(QPainter::CompositionMode_SourceOver);
224 ccb->replay(&p, m_state);
229 bool QQuickContext2DTexture::canvasDestroyed()
234 void QQuickContext2DTexture::paint(QQuickContext2DCommandBuffer *ccb)
236 if (canvasDestroyed()) {
241 GLAcquireContext currentContext(m_context->glContext(), m_context->surface());
243 if (!m_tiledCanvas) {
244 paintWithoutTiles(ccb);
249 QRect tiledRegion = createTiles(m_canvasWindow.intersected(QRect(QPoint(0, 0), m_canvasSize)));
250 if (!tiledRegion.isEmpty()) {
252 foreach (QQuickContext2DTile* tile, m_tiles) {
254 if (dirtyRect.isEmpty())
255 dirtyRect = tile->rect();
257 dirtyRect |= tile->rect();
261 if (beginPainting()) {
262 QQuickContext2D::State oldState = m_state;
263 foreach (QQuickContext2DTile* tile, m_tiles) {
265 ccb->replay(tile->createPainter(m_smooth), oldState);
266 tile->drawFinished();
267 tile->markDirty(false);
279 QRect QQuickContext2DTexture::tiledRect(const QRectF& window, const QSize& tileSize)
281 if (window.isEmpty())
284 const int tw = tileSize.width();
285 const int th = tileSize.height();
286 const int h1 = window.left() / tw;
287 const int v1 = window.top() / th;
289 const int htiles = ((window.right() - h1 * tw) + tw - 1)/tw;
290 const int vtiles = ((window.bottom() - v1 * th) + th - 1)/th;
292 return QRect(h1 * tw, v1 * th, htiles * tw, vtiles * th);
295 QRect QQuickContext2DTexture::createTiles(const QRect& window)
297 QList<QQuickContext2DTile*> oldTiles = m_tiles;
300 if (window.isEmpty()) {
301 m_dirtyCanvas = false;
305 QRect r = tiledRect(window, adjustedTileSize(m_tileSize));
307 const int tw = m_tileSize.width();
308 const int th = m_tileSize.height();
309 const int h1 = window.left() / tw;
310 const int v1 = window.top() / th;
313 const int htiles = r.width() / tw;
314 const int vtiles = r.height() / th;
316 for (int yy = 0; yy < vtiles; ++yy) {
317 for (int xx = 0; xx < htiles; ++xx) {
321 QQuickContext2DTile* tile = 0;
323 QPoint pos(ht * tw, vt * th);
324 QRect rect(pos, m_tileSize);
326 for (int i = 0; i < oldTiles.size(); i++) {
327 if (oldTiles[i]->rect() == rect) {
328 tile = oldTiles.takeAt(i);
337 m_tiles.append(tile);
341 qDeleteAll(oldTiles);
343 m_dirtyCanvas = false;
347 void QQuickContext2DTexture::clearTiles()
353 QSize QQuickContext2DTexture::adjustedTileSize(const QSize &ts)
358 static inline QSize npotAdjustedSize(const QSize &size)
360 static bool checked = false;
361 static bool npotSupported = false;
364 npotSupported = QOpenGLContext::currentContext()->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures);
369 return QSize(qMax(QT_MINIMUM_FBO_SIZE, size.width()),
370 qMax(QT_MINIMUM_FBO_SIZE, size.height()));
373 return QSize(qMax(QT_MINIMUM_FBO_SIZE, qt_next_power_of_two(size.width())),
374 qMax(QT_MINIMUM_FBO_SIZE, qt_next_power_of_two(size.height())));
377 QQuickContext2DFBOTexture::QQuickContext2DFBOTexture()
378 : QQuickContext2DTexture()
380 , m_multisampledFbo(0)
385 QQuickContext2DFBOTexture::~QQuickContext2DFBOTexture()
387 if (m_multisampledFbo)
388 m_multisampledFbo->release();
393 delete m_multisampledFbo;
394 delete m_paint_device;
397 QSize QQuickContext2DFBOTexture::adjustedTileSize(const QSize &ts)
399 return npotAdjustedSize(ts);
402 void QQuickContext2DFBOTexture::bind()
404 glBindTexture(GL_TEXTURE_2D, textureId());
408 QRectF QQuickContext2DFBOTexture::normalizedTextureSubRect() const
412 , qreal(m_canvasWindow.width()) / m_fboSize.width()
413 , qreal(m_canvasWindow.height()) / m_fboSize.height());
417 int QQuickContext2DFBOTexture::textureId() const
419 return m_fbo? m_fbo->texture() : 0;
423 bool QQuickContext2DFBOTexture::updateTexture()
425 bool textureUpdated = m_dirtyTexture;
426 m_dirtyTexture = false;
427 return textureUpdated;
430 QQuickContext2DTile* QQuickContext2DFBOTexture::createTile() const
432 return new QQuickContext2DFBOTile();
435 bool QQuickContext2DFBOTexture::doMultisampling() const
437 static bool extensionsChecked = false;
438 static bool multisamplingSupported = false;
440 if (!extensionsChecked) {
441 QList<QByteArray> extensions = QByteArray((const char *)glGetString(GL_EXTENSIONS)).split(' ');
442 multisamplingSupported = extensions.contains("GL_EXT_framebuffer_multisample")
443 && extensions.contains("GL_EXT_framebuffer_blit");
444 extensionsChecked = true;
447 return multisamplingSupported && m_smooth;
450 void QQuickContext2DFBOTexture::grabImage(const QRectF& rf)
452 Q_ASSERT(rf.isValid());
455 m_context->setGrabbedImage(QImage());
461 GLAcquireContext ctx(m_context->glContext(), m_context->surface());
462 grabbed = m_fbo->toImage().mirrored().copy(rf.toRect());
465 m_context->setGrabbedImage(grabbed);
468 void QQuickContext2DFBOTexture::compositeTile(QQuickContext2DTile* tile)
470 QQuickContext2DFBOTile* t = static_cast<QQuickContext2DFBOTile*>(tile);
471 QRect target = t->rect().intersected(m_canvasWindow);
472 if (target.isValid()) {
473 QRect source = target;
475 source.moveTo(source.topLeft() - t->rect().topLeft());
476 target.moveTo(target.topLeft() - m_canvasWindow.topLeft());
478 QOpenGLFramebufferObject::blitFramebuffer(m_fbo, target, t->fbo(), source);
482 QQuickCanvasItem::RenderTarget QQuickContext2DFBOTexture::renderTarget() const
484 return QQuickCanvasItem::FramebufferObject;
487 QPaintDevice* QQuickContext2DFBOTexture::beginPainting()
489 QQuickContext2DTexture::beginPainting();
491 if (m_canvasWindow.size().isEmpty()) {
493 delete m_multisampledFbo;
494 delete m_paint_device;
496 m_multisampledFbo = 0;
499 } else if (!m_fbo || m_canvasWindowChanged) {
501 delete m_multisampledFbo;
502 delete m_paint_device;
505 m_fboSize = npotAdjustedSize(m_canvasWindow.size());
506 m_canvasWindowChanged = false;
508 if (doMultisampling()) {
510 QOpenGLFramebufferObjectFormat format;
511 format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
512 format.setSamples(8);
513 m_multisampledFbo = new QOpenGLFramebufferObject(m_fboSize, format);
516 QOpenGLFramebufferObjectFormat format;
517 format.setAttachment(QOpenGLFramebufferObject::NoAttachment);
518 m_fbo = new QOpenGLFramebufferObject(m_fboSize, format);
521 QOpenGLFramebufferObjectFormat format;
522 format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
524 m_fbo = new QOpenGLFramebufferObject(m_fboSize, format);
528 if (doMultisampling())
529 m_multisampledFbo->bind();
534 if (!m_paint_device) {
535 QOpenGLPaintDevice *gl_device = new QOpenGLPaintDevice(m_fbo->size());
536 gl_device->setPaintFlipped(true);
537 gl_device->setSize(m_fbo->size());
538 m_paint_device = gl_device;
541 return m_paint_device;
544 void QQuickContext2DFBOTexture::endPainting()
546 QQuickContext2DTexture::endPainting();
547 if (m_multisampledFbo)
548 QOpenGLFramebufferObject::blitFramebuffer(m_fbo, m_multisampledFbo);
551 QQuickContext2DImageTexture::QQuickContext2DImageTexture()
552 : QQuickContext2DTexture()
557 QQuickContext2DImageTexture::~QQuickContext2DImageTexture()
559 if (m_texture && m_texture->thread() != QThread::currentThread())
560 m_texture->deleteLater();
565 int QQuickContext2DImageTexture::textureId() const
567 return imageTexture()->textureId();
570 QQuickCanvasItem::RenderTarget QQuickContext2DImageTexture::renderTarget() const
572 return QQuickCanvasItem::Image;
575 void QQuickContext2DImageTexture::bind()
577 imageTexture()->bind();
580 bool QQuickContext2DImageTexture::updateTexture()
582 bool textureUpdated = m_dirtyTexture;
583 if (m_dirtyTexture) {
584 imageTexture()->setImage(m_image);
585 m_dirtyTexture = false;
587 return textureUpdated;
590 QQuickContext2DTile* QQuickContext2DImageTexture::createTile() const
592 return new QQuickContext2DImageTile();
595 void QQuickContext2DImageTexture::grabImage(const QRectF& rf)
597 Q_ASSERT(rf.isValid());
599 QImage grabbed = m_image.copy(rf.toRect());
600 m_context->setGrabbedImage(grabbed);
603 QSGPlainTexture *QQuickContext2DImageTexture::imageTexture() const
606 QQuickContext2DImageTexture *that = const_cast<QQuickContext2DImageTexture *>(this);
607 that->m_texture = new QSGPlainTexture;
608 that->m_texture->setOwnsTexture(true);
609 that->m_texture->setHasMipmaps(false);
614 QPaintDevice* QQuickContext2DImageTexture::beginPainting()
616 QQuickContext2DTexture::beginPainting();
618 if (m_canvasWindow.size().isEmpty())
621 if (m_canvasWindowChanged) {
622 m_image = QImage(m_canvasWindow.size(), QImage::Format_ARGB32_Premultiplied);
623 m_image.fill(0x00000000);
624 m_canvasWindowChanged = false;
630 void QQuickContext2DImageTexture::compositeTile(QQuickContext2DTile* tile)
632 Q_ASSERT(!tile->dirty());
633 QQuickContext2DImageTile* t = static_cast<QQuickContext2DImageTile*>(tile);
634 QRect target = t->rect().intersected(m_canvasWindow);
635 if (target.isValid()) {
636 QRect source = target;
637 source.moveTo(source.topLeft() - t->rect().topLeft());
638 target.moveTo(target.topLeft() - m_canvasWindow.topLeft());
640 m_painter.begin(&m_image);
641 m_painter.setCompositionMode(QPainter::CompositionMode_Source);
642 m_painter.drawImage(target, t->image(), source);