qquickcontext2dtexture.cpp 19.49 KiB
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.1, 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.
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
** $QT_END_LICENSE$
****************************************************************************/
#include "qquickcontext2dtexture_p.h"
#include "qquickcontext2dtile_p.h"
#include "qquickcanvasitem_p.h"
#include <private/qquickitem_p.h>
#include <QtQuick/private/qsgtexture_p.h>
#include "qquickcontext2dcommandbuffer_p.h"
#include <QOpenGLPaintDevice>
#include <QOpenGLFramebufferObject>
#include <QOpenGLFramebufferObjectFormat>
#include <QtCore/QThread>
QT_BEGIN_NAMESPACE
#define QT_MINIMUM_FBO_SIZE 64
static inline int qt_next_power_of_two(int v)
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    ++v;
    return v;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
Q_GLOBAL_STATIC(QThread, globalCanvasThreadRenderInstance) QQuickContext2DTexture::QQuickContext2DTexture() : m_context(0) , m_item(0) , m_dirtyCanvas(false) , m_canvasWindowChanged(false) , m_dirtyTexture(false) , m_threadRendering(false) , m_smooth(false) , m_tiledCanvas(false) , m_doGrabImage(false) , m_painting(false) { } QQuickContext2DTexture::~QQuickContext2DTexture() { clearTiles(); } QSize QQuickContext2DTexture::textureSize() const { return m_canvasWindow.size(); } void QQuickContext2DTexture::markDirtyTexture() { const bool inGrab = m_doGrabImage; m_dirtyTexture = true; updateTexture(); if (!inGrab) emit textureChanged(); } bool QQuickContext2DTexture::setCanvasSize(const QSize &size) { if (m_canvasSize != size) { m_canvasSize = size; m_dirtyCanvas = true; return true; } return false; } bool QQuickContext2DTexture::setTileSize(const QSize &size) { if (m_tileSize != size) { m_tileSize = size; m_dirtyCanvas = true; return true; } return false; } void QQuickContext2DTexture::setSmooth(bool smooth) { m_smooth = smooth; } void QQuickContext2DTexture::setItem(QQuickCanvasItem* item) { m_item = item; m_context = (QQuickContext2D*)item->rawContext(); // FIXME m_state = m_context->state; }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
bool QQuickContext2DTexture::setCanvasWindow(const QRect& r) { if (m_canvasWindow != r) { m_canvasWindow = r; m_canvasWindowChanged = true; return true; } return false; } bool QQuickContext2DTexture::setDirtyRect(const QRect &r) { bool doDirty = false; if (m_tiledCanvas) { foreach (QQuickContext2DTile* t, m_tiles) { bool dirty = t->rect().intersected(r).isValid(); t->markDirty(dirty); if (dirty) doDirty = true; } } else { doDirty = m_canvasWindow.intersected(r).isValid(); } return doDirty; } void QQuickContext2DTexture::canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth) { lock(); QSize ts = tileSize; if (ts.width() > canvasSize.width()) ts.setWidth(canvasSize.width()); if (ts.height() > canvasSize.height()) ts.setHeight(canvasSize.height()); setCanvasSize(canvasSize); setTileSize(ts); setCanvasWindow(canvasWindow); if (canvasSize == canvasWindow.size()) { m_tiledCanvas = false; m_dirtyCanvas = false; } else { m_tiledCanvas = true; } if (dirtyRect.isValid()) setDirtyRect(dirtyRect); setSmooth(smooth); unlock(); } void QQuickContext2DTexture::paintWithoutTiles() { QQuickContext2DCommandBuffer* ccb = m_context->nextBuffer(); if (!ccb || ccb->isEmpty()) return; QPaintDevice* device = beginPainting(); if (!device) { delete ccb; endPainting(); return; }
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
QPainter p; p.begin(device); if (m_smooth) p.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform); else p.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, false); p.setCompositionMode(QPainter::CompositionMode_SourceOver); ccb->replay(&p, m_state); ccb->clear(); delete ccb; endPainting(); markDirtyTexture(); } bool QQuickContext2DTexture::canvasDestroyed() { bool noCanvas = false; lock(); noCanvas = m_item == 0; unlock(); return noCanvas; } void QQuickContext2DTexture::paint() { if (canvasDestroyed()) return; if (m_threadRendering && QThread::currentThread() != globalCanvasThreadRenderInstance()) { Q_ASSERT(thread() == globalCanvasThreadRenderInstance()); QMetaObject::invokeMethod(this, "paint", Qt::QueuedConnection); return; } if (!m_tiledCanvas) { paintWithoutTiles(); } else { lock(); QRect tiledRegion = createTiles(m_canvasWindow.intersected(QRect(QPoint(0, 0), m_canvasSize))); unlock(); if (!tiledRegion.isEmpty()) { if (m_threadRendering && !m_doGrabImage) { QRect dirtyRect; lock(); foreach (QQuickContext2DTile* tile, m_tiles) { if (tile->dirty()) { if (dirtyRect.isEmpty()) dirtyRect = tile->rect(); else dirtyRect |= tile->rect(); } } unlock(); } if (beginPainting()) { QQuickContext2D::State oldState = m_state; QQuickContext2DCommandBuffer* ccb = m_context->nextBuffer(); if (!ccb || ccb->isEmpty()) { endPainting(); delete ccb; return; }