1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include <private/qtquickglobal_p.h>
41 #include "qquickitemgrabresult.h"
42
43 #include "qquickrendercontrol.h"
44 #include "qquickwindow.h"
45 #include "qquickitem.h"
46 #if QT_CONFIG(quick_shadereffect)
47 #include "qquickshadereffectsource_p.h"
48 #endif
49
50 #include <QtQml/QQmlEngine>
51 #include <QtQml/QQmlInfo>
52
53 #include <private/qquickpixmapcache_p.h>
54 #include <private/qquickitem_p.h>
55 #include <private/qsgcontext_p.h>
56 #include <private/qsgadaptationlayer_p.h>
57
58 QT_BEGIN_NAMESPACE
59
60 const QEvent::Type Event_Grab_Completed = static_cast<QEvent::Type>(QEvent::User + 1);
61
62 class QQuickItemGrabResultPrivate : public QObjectPrivate
63 {
64 public:
QQuickItemGrabResultPrivate()65 QQuickItemGrabResultPrivate()
66 : cacheEntry(nullptr)
67 , qmlEngine(nullptr)
68 , texture(nullptr)
69 {
70 }
71
~QQuickItemGrabResultPrivate()72 ~QQuickItemGrabResultPrivate()
73 {
74 delete cacheEntry;
75 }
76
ensureImageInCache() const77 void ensureImageInCache() const {
78 if (url.isEmpty() && !image.isNull()) {
79 url.setScheme(QQuickPixmap::itemGrabberScheme);
80 url.setPath(QVariant::fromValue(item.data()).toString());
81 static uint counter = 0;
82 url.setFragment(QString::number(++counter));
83 cacheEntry = new QQuickPixmap(url, image);
84 }
85 }
86
87 static QQuickItemGrabResult *create(QQuickItem *item, const QSize &size);
88
89 QImage image;
90
91 mutable QUrl url;
92 mutable QQuickPixmap *cacheEntry;
93
94 QQmlEngine *qmlEngine;
95 QJSValue callback;
96
97 QPointer<QQuickItem> item;
98 QPointer<QQuickWindow> window;
99 QSGLayer *texture;
100 QSizeF itemSize;
101 QSize textureSize;
102 };
103
104 /*!
105 * \qmlproperty url QtQuick::ItemGrabResult::url
106 *
107 * This property holds a URL which can be used in conjunction with
108 * URL based image consumers, such as the QtQuick::Image type.
109 *
110 * The URL is valid while there is a reference in QML or JavaScript
111 * to the ItemGrabResult or while the image the URL references is
112 * actively used.
113 *
114 * The URL does not represent a valid file or location to read it from, it
115 * is primarily a key to access images through Qt Quick's image-based types.
116 */
117
118 /*!
119 * \property QQuickItemGrabResult::url
120 *
121 * This property holds a URL which can be used in conjunction with
122 * URL based image consumers, such as the QtQuick::Image type.
123 *
124 * The URL is valid until the QQuickItemGrabResult object is deleted.
125 *
126 * The URL does not represent a valid file or location to read it from, it
127 * is primarily a key to access images through Qt Quick's image-based types.
128 */
129
130 /*!
131 * \qmlproperty variant QtQuick::ItemGrabResult::image
132 *
133 * This property holds the pixel results from a grab in the
134 * form of a QImage.
135 */
136
137 /*!
138 * \property QQuickItemGrabResult::image
139 *
140 * This property holds the pixel results from a grab.
141 *
142 * If the grab is not yet complete or if it failed,
143 * a null image is returned (\c {image.isNull()} will return \c true).
144 */
145
146 /*!
147 \class QQuickItemGrabResult
148 \inmodule QtQuick
149 \brief The QQuickItemGrabResult contains the result from QQuickItem::grabToImage().
150
151 \sa QQuickItem::grabToImage()
152 */
153
154 /*!
155 * \fn void QQuickItemGrabResult::ready()
156 *
157 * This signal is emitted when the grab has completed.
158 */
159
160 /*!
161 * \qmltype ItemGrabResult
162 * \instantiates QQuickItemGrabResult
163 * \inherits QtObject
164 * \inqmlmodule QtQuick
165 * \ingroup qtquick-visual
166 * \brief Contains the results from a call to Item::grabToImage().
167 *
168 * The ItemGrabResult is a small container used to encapsulate
169 * the results from Item::grabToImage().
170 *
171 * \sa Item::grabToImage()
172 */
173
QQuickItemGrabResult(QObject * parent)174 QQuickItemGrabResult::QQuickItemGrabResult(QObject *parent)
175 : QObject(*new QQuickItemGrabResultPrivate, parent)
176 {
177 }
178
179 /*!
180 * \qmlmethod bool QtQuick::ItemGrabResult::saveToFile(fileName)
181 *
182 * Saves the grab result as an image to \a fileName. Returns true
183 * if successful; otherwise returns false.
184 */
185
186 /*!
187 * Saves the grab result as an image to \a fileName. Returns true
188 * if successful; otherwise returns false.
189 *
190 * \note In Qt versions prior to 5.9, this function is marked as non-\c{const}.
191 */
saveToFile(const QString & fileName) const192 bool QQuickItemGrabResult::saveToFile(const QString &fileName) const
193 {
194 Q_D(const QQuickItemGrabResult);
195 return d->image.save(fileName);
196 }
197
198 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
199 #if QT_DEPRECATED_SINCE(5, 15)
200 /*!
201 * \overload
202 * \internal
203 */
saveToFile(const QString & fileName)204 bool QQuickItemGrabResult::saveToFile(const QString &fileName)
205 {
206 return qAsConst(*this).saveToFile(fileName);
207 }
208 #endif
209 #endif // < Qt 6
210
url() const211 QUrl QQuickItemGrabResult::url() const
212 {
213 Q_D(const QQuickItemGrabResult);
214 d->ensureImageInCache();
215 return d->url;
216 }
217
image() const218 QImage QQuickItemGrabResult::image() const
219 {
220 Q_D(const QQuickItemGrabResult);
221 return d->image;
222 }
223
224 /*!
225 * \internal
226 */
event(QEvent * e)227 bool QQuickItemGrabResult::event(QEvent *e)
228 {
229 Q_D(QQuickItemGrabResult);
230 if (e->type() == Event_Grab_Completed) {
231 // JS callback
232 if (d->qmlEngine && d->callback.isCallable()) {
233 d->callback.call(QJSValueList() << d->qmlEngine->newQObject(this));
234 deleteLater();
235 } else {
236 Q_EMIT ready();
237 }
238 return true;
239 }
240 return QObject::event(e);
241 }
242
setup()243 void QQuickItemGrabResult::setup()
244 {
245 Q_D(QQuickItemGrabResult);
246 if (!d->item) {
247 disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing, this, &QQuickItemGrabResult::setup);
248 disconnect(d->window.data(), &QQuickWindow::afterRendering, this, &QQuickItemGrabResult::render);
249 QCoreApplication::postEvent(this, new QEvent(Event_Grab_Completed));
250 return;
251 }
252
253 QSGRenderContext *rc = QQuickWindowPrivate::get(d->window.data())->context;
254 d->texture = rc->sceneGraphContext()->createLayer(rc);
255 d->texture->setItem(QQuickItemPrivate::get(d->item)->itemNode());
256 d->itemSize = QSizeF(d->item->width(), d->item->height());
257 }
258
render()259 void QQuickItemGrabResult::render()
260 {
261 Q_D(QQuickItemGrabResult);
262 if (!d->texture)
263 return;
264
265 d->texture->setRect(QRectF(0, d->itemSize.height(), d->itemSize.width(), -d->itemSize.height()));
266 const QSize minSize = QQuickWindowPrivate::get(d->window.data())->context->sceneGraphContext()->minimumFBOSize();
267 d->texture->setSize(QSize(qMax(minSize.width(), d->textureSize.width()),
268 qMax(minSize.height(), d->textureSize.height())));
269 d->texture->scheduleUpdate();
270 d->texture->updateTexture();
271 d->image = d->texture->toImage();
272
273 delete d->texture;
274 d->texture = nullptr;
275
276 disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing, this, &QQuickItemGrabResult::setup);
277 disconnect(d->window.data(), &QQuickWindow::afterRendering, this, &QQuickItemGrabResult::render);
278 QCoreApplication::postEvent(this, new QEvent(Event_Grab_Completed));
279 }
280
create(QQuickItem * item,const QSize & targetSize)281 QQuickItemGrabResult *QQuickItemGrabResultPrivate::create(QQuickItem *item, const QSize &targetSize)
282 {
283 QSize size = targetSize;
284 if (size.isEmpty())
285 size = QSize(item->width(), item->height());
286
287 if (size.width() < 1 || size.height() < 1) {
288 qmlWarning(item) << "grabToImage: item has invalid dimensions";
289 return nullptr;
290 }
291
292 if (!item->window()) {
293 qmlWarning(item) << "grabToImage: item is not attached to a window";
294 return nullptr;
295 }
296
297 QWindow *effectiveWindow = item->window();
298 if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(item->window()))
299 effectiveWindow = renderWindow;
300
301 if (!effectiveWindow->isVisible()) {
302 qmlWarning(item) << "grabToImage: item's window is not visible";
303 return nullptr;
304 }
305
306 QQuickItemGrabResult *result = new QQuickItemGrabResult();
307 QQuickItemGrabResultPrivate *d = result->d_func();
308 d->item = item;
309 d->window = item->window();
310 d->textureSize = size;
311
312 QQuickItemPrivate::get(item)->refFromEffectItem(false);
313
314 // trigger sync & render
315 item->window()->update();
316
317 return result;
318 }
319
320 /*!
321 * Grabs the item into an in-memory image.
322 *
323 * The grab happens asynchronously and the signal QQuickItemGrabResult::ready()
324 * is emitted when the grab has been completed.
325 *
326 * Use \a targetSize to specify the size of the target image. By default, the
327 * result will have the same size as item.
328 *
329 * If the grab could not be initiated, the function returns \c null.
330 *
331 * \note This function will render the item to an offscreen surface and
332 * copy that surface from the GPU's memory into the CPU's memory, which can
333 * be quite costly. For "live" preview, use \l {QtQuick::Item::layer.enabled} {layers}
334 * or ShaderEffectSource.
335 *
336 * \sa QQuickWindow::grabWindow()
337 */
grabToImage(const QSize & targetSize)338 QSharedPointer<QQuickItemGrabResult> QQuickItem::grabToImage(const QSize &targetSize)
339 {
340 QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(this, targetSize);
341 if (!result)
342 return QSharedPointer<QQuickItemGrabResult>();
343
344 connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection);
345 connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection);
346
347 return QSharedPointer<QQuickItemGrabResult>(result);
348 }
349
350 /*!
351 * \qmlmethod bool QtQuick::Item::grabToImage(callback, targetSize)
352 *
353 * Grabs the item into an in-memory image.
354 *
355 * The grab happens asynchronously and the JavaScript function \a callback is
356 * invoked when the grab is completed. The callback takes one argument, which
357 * is the result of the grab operation; an \l ItemGrabResult object.
358 *
359 * Use \a targetSize to specify the size of the target image. By default, the result
360 * will have the same size as the item.
361 *
362 * If the grab could not be initiated, the function returns \c false.
363 *
364 * The following snippet shows how to grab an item and store the results to
365 * a file.
366 *
367 * \snippet qml/itemGrab.qml grab-source
368 * \snippet qml/itemGrab.qml grab-to-file
369 *
370 * The following snippet shows how to grab an item and use the results in
371 * another image element.
372 *
373 * \snippet qml/itemGrab.qml grab-image-target
374 * \snippet qml/itemGrab.qml grab-to-cache
375 *
376 * \note This function will render the item to an offscreen surface and
377 * copy that surface from the GPU's memory into the CPU's memory, which can
378 * be quite costly. For "live" preview, use \l {QtQuick::Item::layer.enabled} {layers}
379 * or ShaderEffectSource.
380 */
381
382 /*!
383 * \internal
384 * Only visible from QML.
385 */
grabToImage(const QJSValue & callback,const QSize & targetSize)386 bool QQuickItem::grabToImage(const QJSValue &callback, const QSize &targetSize)
387 {
388 QQmlEngine *engine = qmlEngine(this);
389 if (!engine) {
390 qmlWarning(this) << "grabToImage: item has no QML engine";
391 return false;
392 }
393
394 if (!callback.isCallable()) {
395 qmlWarning(this) << "grabToImage: 'callback' is not a function";
396 return false;
397 }
398
399 QSize size = targetSize;
400 if (size.isEmpty())
401 size = QSize(width(), height());
402
403 if (size.width() < 1 || size.height() < 1) {
404 qmlWarning(this) << "grabToImage: item has invalid dimensions";
405 return false;
406 }
407
408 if (!window()) {
409 qmlWarning(this) << "grabToImage: item is not attached to a window";
410 return false;
411 }
412
413 QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(this, size);
414 if (!result)
415 return false;
416
417 connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection);
418 connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection);
419
420 QQuickItemGrabResultPrivate *d = result->d_func();
421 d->qmlEngine = engine;
422 d->callback = callback;
423 return true;
424 }
425
426 QT_END_NAMESPACE
427
428 #include "moc_qquickitemgrabresult.cpp"
429