4#include <private/qsgadaptationlayer_p.h>
6#include <private/qquickitem_p.h>
7#include <private/qquickcanvascontext_p.h>
8#include <private/qquickcontext2d_p.h>
9#include <private/qquickcontext2dtexture_p.h>
10#include <private/qsgadaptationlayer_p.h>
12#include <QtQuick/private/qquickpixmapcache_p.h>
13#include <QtGui/QGuiApplication>
17#include <private/qqmlengine_p.h>
18#include <QtCore/QBuffer>
19#include <QtCore/qdatetime.h>
21#include <private/qv4value_p.h>
22#include <private/qv4functionobject_p.h>
23#include <private/qv4scopedvalue_p.h>
24#include <private/qv4jscall_p.h>
25#include <private/qv4qobjectwrapper_p.h>
26#include <private/qjsvalue_p.h>
59 return m_pixmap->
width();
61 return m_image.
width();
81 if (m_image.
isNull() && m_pixmap)
82 m_image = m_pixmap->
image();
88QMutex QQuickContext2DRenderThread::renderThreadsMutex;
94 m_eventLoopQuitHack =
new QObject;
102 renderThreadsMutex.
lock();
103 renderThreads.
remove(m_engine);
104 renderThreadsMutex.
unlock();
113 renderThreadsMutex.
lock();
120 renderThreadsMutex.
unlock();
154 , hasCanvasSize(
false)
156 , hasCanvasWindow(
false)
268 if (
d->textureProvider)
280 return d_func()->available;
300 return d_func()->contextType;
311 qmlWarning(
this) <<
"Canvas already initialized with a different context type";
355 return d->canvasSize;
361 if (
d->canvasSize !=
size) {
362 d->hasCanvasSize =
true;
363 d->canvasSize =
size;
397 if (
d->tileSize !=
size) {
398 d->hasTileSize =
true;
426 return d->canvasWindow;
432 if (
d->canvasWindow !=
rect) {
433 d->canvasWindow =
rect;
435 d->hasCanvasWindow =
true;
461 return d->renderTarget;
467 if (
d->renderTarget !=
target) {
469 qmlWarning(
this) <<
"Canvas:renderTarget not changeble once context is active.";
503 return d_func()->renderStrategy;
509 if (
d->renderStrategy != strategy) {
511 qmlWarning(
this) <<
"Canvas:renderStrategy not changeable once context is active.";
514 d->renderStrategy = strategy;
521 return d_func()->context;
524bool QQuickCanvasItem::isPaintConnected()
529void QQuickCanvasItem::sceneGraphInitialized()
537 if (!
d->contextType.isNull())
539 else if (isPaintConnected())
552 if (!
d->hasCanvasSize &&
d->canvasSize != newSize) {
553 d->canvasSize = newSize;
557 if (!
d->hasTileSize &&
d->tileSize != newSize) {
558 d->tileSize = newSize.
toSize();
564 if (!
d->hasCanvasWindow &&
d->canvasWindow !=
rect) {
565 d->canvasWindow =
rect;
569 if (
d->available && newSize != oldGeometry.
size()) {
570 if (
isVisible() || (
d->extra.isAllocated() &&
d->extra->effectRefCount > 0))
581 d->context =
nullptr;
584 if (
d->textureProvider) {
586 d->textureProvider =
nullptr;
588 if (
d->nodeTexture) {
590 d->nodeTexture =
nullptr;
596 switch (
event->type()) {
605void QQuickCanvasItem::invalidateSceneGraph()
609 d->context->deleteLater();
610 d->context =
nullptr;
612 delete d->textureProvider;
613 d->textureProvider =
nullptr;
614 delete d->nodeTexture;
615 d->nodeTexture =
nullptr;
622void QQuickCanvasItem::schedulePolish()
649 if (
value.window==
nullptr)
672 d->context->prepare(
d->canvasSize.toSize(),
d->tileSize,
d->canvasWindow.toRect(),
d->dirtyRect.toRect(),
d->smooth,
antialiasing());
674 if (
d->animationCallbacks.size() > 0 &&
isVisible()) {
676 d->animationCallbacks.clear();
691 if (
d->dirtyRect.isValid()) {
692 if (
d->hasTileSize &&
d->hasCanvasWindow)
693 emit paint(tiledRect(
d->canvasWindow.intersected(
d->dirtyRect.toAlignedRect()),
d->tileSize));
712 if (!
d->context ||
d->canvasWindow.size().isEmpty()) {
713 if (
d->textureProvider) {
714 d->textureProvider->tex =
nullptr;
715 d->textureProvider->fireTextureChanged();
735 d->context->prepare(
d->canvasSize.toSize(),
d->tileSize,
d->canvasWindow.toRect(),
d->dirtyRect.toRect(),
d->smooth,
antialiasing());
745 d->nodeTexture =
nullptr;
746 if (
d->textureProvider) {
747 d->textureProvider->tex =
nullptr;
748 d->textureProvider->fireTextureChanged();
759 if (
d->textureProvider) {
760 d->textureProvider->tex =
d->nodeTexture;
761 d->textureProvider->fireTextureChanged();
782 if (!
w || !
w->isSceneGraphInitialized()
784 qWarning(
"QQuickCanvasItem::textureProvider: can only be queried on the rendering thread of an exposed window");
788 if (!
d->textureProvider)
790 d->textureProvider->tex =
d->nodeTexture;
791 return d->textureProvider;
820 qmlWarning(
this) <<
"getContext should be called with a string naming the required context type";
826 qmlWarning(
this) <<
"Unable to use getContext() at this time, please wait for available: true";
833 if (
d->context !=
nullptr) {
835 args->setReturnValue(
d->context->v4value());
839 qmlWarning(
this) <<
"Canvas already initialized with a different context type";
844 if (createContext(contextId))
845 args->setReturnValue(
d->context->v4value());
862 qmlWarning(
this) <<
"requestAnimationFrame should be called with an animation callback function";
890 if (!
v->isInteger()) {
891 qmlWarning(
this) <<
"cancelRequestAnimationFrame should be called with an animation callback id";
896 d_func()->animationCallbacks.
remove(
v->integerValue());
928 d->dirtyRect |=
rect;
933void QQuickCanvasItem::checkAnimationCallbacks()
935 if (d_func()->animationCallbacks.size() > 0 &&
isVisible())
964 QUrl fullPathUrl =
d->baseUrl.resolved(
url);
965 if (!
d->pixmaps.contains(fullPathUrl)) {
968 return d->pixmaps.value(fullPathUrl);
999 QUrl fullPathUrl =
d->baseUrl.resolved(
url);
1000 if (!
d->pixmaps.contains(fullPathUrl)) {
1004 d->pixmaps.insert(fullPathUrl, canvasPix);
1011 if (
pix->isLoading())
1029 d->pixmaps.remove(
d->baseUrl.resolved(
url));
1042 QUrl fullPathUrl =
d->baseUrl.resolved(
url);
1043 return d->pixmaps.contains(fullPathUrl)
1044 &&
d->pixmaps.value(fullPathUrl)->pixmap()->isError();
1056 QUrl fullPathUrl =
d->baseUrl.resolved(
url);
1057 return d->pixmaps.contains(fullPathUrl)
1058 &&
d->pixmaps.value(fullPathUrl)->pixmap()->isLoading();
1069 QUrl fullPathUrl =
d->baseUrl.resolved(
url);
1070 return d->pixmaps.contains(fullPathUrl)
1071 &&
d->pixmaps.value(fullPathUrl)->pixmap()->isReady();
1099 if (!
mime.startsWith(imagePrefix))
1130 if (!
image.isNull()) {
1146void QQuickCanvasItem::delayedCreate()
1150 if (!
d->context && !
d->contextType.isNull())
1151 createContext(
d->contextType);
1156bool QQuickCanvasItem::createContext(
const QString &contextType)
1180 d->context->init(
this,
args);
1194 const int h1 =
window.left() / tw;
1197 const int htiles = ((
window.right() - h1 * tw) + tw - 1)/tw;
1198 const int vtiles = ((
window.bottom() -
v1 * th) + th - 1)/th;
1200 return QRect(h1 * tw,
v1 * th, htiles * tw, vtiles * th);
1222#include "moc_qquickcanvasitem_p.cpp"
\inmodule QtCore \reentrant
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
QByteArray toBase64(Base64Options options=Base64Encoding) const
static void postEvent(QObject *receiver, QEvent *event, int priority=Qt::NormalEventPriority)
static qint64 currentMSecsSinceEpoch() noexcept
bool remove(const Key &key)
Removes the item that has the key from the hash.
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
T value(const Key &key) const noexcept
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
int width() const
Returns the width of the image.
bool save(const QString &fileName, const char *format=nullptr, int quality=-1) const
Saves the image to the file with the given fileName, using the given image file format and quality fa...
bool isNull() const
Returns true if it is a null image, otherwise returns false.
int height() const
Returns the height of the image.
QV4::ExecutionEngine * handle() const
static QJSValue fromReturnedValue(QV4::ReturnedValue d)
The QJSValue class acts as a container for Qt/JavaScript data types.
constexpr qsizetype size() const noexcept
void remove(qsizetype i, qsizetype n=1)
const_iterator cend() const
const_iterator cbegin() const
void unlock() noexcept
Unlocks the mutex.
void lock() noexcept
Locks the mutex.
void moveToThread(QThread *thread)
Changes the thread affinity for this object and its children.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
QThread * thread() const
Returns the thread in which the object lives.
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
bool load(const QString &fileName, const char *format=nullptr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the file with the given fileName.
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
QUrl baseUrl() const
Returns the base url of the component, or the containing component if none is set.
The QQmlEngine class provides an environment for instantiating QML components.
static QQmlContext * contextForObject(const QObject *)
Returns the QQmlContext for the object, or nullptr if no context has been set.
QQmlRefPointer< T > & adopt(T *)
Takes ownership of other.
QQuickCanvasTextureProvider * textureProvider
QQuickCanvasContext * context
QHash< QUrl, QQmlRefPointer< QQuickCanvasPixmap > > pixmaps
QQuickCanvasItemPrivate()
~QQuickCanvasItemPrivate()
QSGInternalImageNode * node
QQuickCanvasItem::RenderTarget renderTarget
QMap< int, QV4::PersistentValue > animationCallbacks
QQuickCanvasItem::RenderStrategy renderStrategy
void setTileSize(const QSize &)
void renderStrategyChanged()
void renderTargetChanged()
void setCanvasWindow(const QRectF &rect)
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
Q_INVOKABLE QString toDataURL(const QString &type=QLatin1String("image/png")) const
\qmlmethod string QtQuick::Canvas::toDataURL(string mimeType)
bool isTextureProvider() const override
Returns true if this item is a texture provider.
void paint(const QRect ®ion)
bool isImageLoaded(const QUrl &url) const
\qmlmethod QtQuick::Canvas::isImageLoaded(url image) Returns true if the image is successfully loaded...
void setCanvasSize(const QSizeF &)
void setRenderStrategy(RenderStrategy strategy)
RenderStrategy renderStrategy
QQmlRefPointer< QQuickCanvasPixmap > loadedPixmap(const QUrl &url, QSizeF sourceSize=QSizeF())
void updatePolish() override
This function should perform any layout as required for this item.
QQuickCanvasContext * rawContext() const
Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV4Function *args)
\qmlmethod QtQuick::Canvas::cancelRequestAnimationFrame(int handle)
QSGTextureProvider * textureProvider() const override
Returns the texture provider for an item.
void setContextType(const QString &contextType)
bool isAvailable() const
\qmlproperty bool QtQuick::Canvas::available
Q_INVOKABLE bool save(const QString &filename, const QSizeF &imageSize=QSizeF()) const
\qmlmethod bool QtQuick::Canvas::save(string filename, size imageSize = undefined)
Q_INVOKABLE void getContext(QQmlV4Function *args)
\qmlmethod object QtQuick::Canvas::getContext(string contextId, ... args)
void unloadImage(const QUrl &url)
\qmlmethod QtQuick::Canvas::unloadImage(url image)
bool isImageError(const QUrl &url) const
\qmlmethod QtQuick::Canvas::isImageError(url image)
void contextTypeChanged()
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
QQuickCanvasItem(QQuickItem *parent=nullptr)
\qmltype Canvas \instantiates QQuickCanvasItem \inqmlmodule QtQuick
QImage toImage(const QRectF &rect=QRectF()) const
QSGNode * updatePaintNode(QSGNode *, UpdatePaintNodeData *) override
Called on the render thread when it is time to sync the state of the item with the scene graph.
void canvasWindowChanged()
void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &) override
Called when change occurs for this item.
void releaseResources() override
This function is called when an item should release graphics resources which are not already managed ...
bool isImageLoading(const QUrl &url) const
\qmlmethod QtQuick::Canvas::isImageLoading(url image) Returns true if the image is currently loading.
RenderTarget renderTarget
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
Q_INVOKABLE void markDirty(const QRectF &dirtyRect=QRectF())
\qmlmethod QtQuick::Canvas::markDirty(rect area)
void setRenderTarget(RenderTarget target)
void loadImage(const QUrl &url, QSizeF sourceSize=QSizeF())
\qmlsignal QtQuick::Canvas::imageLoaded()
Q_INVOKABLE void requestPaint()
\qmlmethod QtQuick::Canvas::requestPaint()
Q_INVOKABLE void requestAnimationFrame(QQmlV4Function *args)
\qmlmethod int QtQuick::Canvas::requestAnimationFrame(callback)
QQuickCanvasPixmap(const QImage &image)
QSGTexture * texture() const override
Returns a pointer to the texture object.
void fireTextureChanged()
QQuickContext2DRenderThread(QQmlEngine *eng)
static QQuickContext2DRenderThread * instance(QQmlEngine *engine)
~QQuickContext2DRenderThread()
quint32 implicitAntialiasing
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
bool event(QEvent *) override
\reimp
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
void componentComplete() override
\reimp Derived classes should call the base class method before adding their own actions to perform a...
virtual QSGTextureProvider * textureProvider() const
Returns the texture provider for an item.
QQuickWindow * window() const
Returns the window in which this item is rendered.
qreal width
This property holds the width of this item.
virtual void itemChange(ItemChange, const ItemChangeData &)
Called when change occurs for this item.
bool antialiasing
\qmlproperty bool QtQuick::Item::antialiasing
qreal height
This property holds the height of this item.
virtual void updatePolish()
This function should perform any layout as required for this item.
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
void update()
Schedules a call to updatePaintNode() for this item.
void polish()
Schedules a polish event for this item.
virtual bool isTextureProvider() const
Returns true if this item is a texture provider.
static QQuickWindowPrivate * get(QQuickWindow *c)
QSGRenderContext * context
static void schedule(QQuickWindow *window, QObject *object)
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore\reentrant
constexpr QPointF topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
\inmodule QtCore\reentrant
virtual QSGInternalImageNode * createInternalImageNode(QSGRenderContext *renderContext)=0
virtual void setTexture(QSGTexture *texture)=0
virtual void setTargetRect(const QRectF &rect)=0
virtual void setFiltering(QSGTexture::Filtering filtering)=0
virtual void setInnerTargetRect(const QRectF &rect)=0
\group qtquick-scenegraph-nodes \title Qt Quick Scene Graph Node classes
QSGContext * sceneGraphContext() const
The QSGTextureProvider class encapsulates texture based entities in QML.
void textureChanged()
This signal is emitted when the texture changes.
constexpr QSize toSize() const noexcept
Returns an integer based copy of this size.
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
\macro QT_RESTRICTED_CAST_FROM_ASCII
int compare(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
void start(Priority=InheritPriority)
static QThread * currentThread()
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
QSet< QString >::iterator it
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QT_BEGIN_NAMESPACE const int tileSize
GLsizei const GLfloat * v
[13]
GLuint64 GLenum void * handle
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei imageSize
QQmlEngine * qmlEngine(const QObject *obj)
#define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments)
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
static const char * mimeToType(const QString &mime)
static QT_BEGIN_NAMESPACE qreal dpr(const QWindow *w)
QLatin1StringView QLatin1String
#define QStringLiteral(str)
QUrl url("example.com")
[constructor-url-reference]
application x qt windows mime
[2]
QItemEditorFactory * factory
static constexpr ReturnedValue null()
static ReturnedValue wrap(ExecutionEngine *engine, QObject *object)
static Value fromUInt32(uint i)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent