1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "iconrenderer.h"
#include "../editor3d/selectionboxgeometry.h"
#include "../editor3d/generalhelper.h"
#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlproperty.h>
#include <QtQml/qqmlcontext.h>
#include <QtQuick/qquickview.h>
#include <QtQuick/qquickitem.h>
#include <QtGui/qsurfaceformat.h>
#include <QtGui/qimage.h>
#include <QtGui/qguiapplication.h>
#include <QtCore/qtimer.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#ifdef QUICK3D_MODULE
#include <QtQuick3D/private/qquick3dnode_p.h>
#include <QtQuick3D/private/qquick3dviewport_p.h>
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QtGui/private/qrhi_p.h>
#include <QtQuick/private/qquickrendercontrol_p.h>
#include <QtQuick/private/qquickrendertarget_p.h>
#endif
#include <private/qquickdesignersupportitems_p.h>
IconRenderer::IconRenderer(int size, const QString &filePath, const QString &source)
: QObject(nullptr)
, m_size(size)
, m_filePath(filePath)
, m_source(source)
{
}
void IconRenderer::setupRender()
{
DesignerSupport::activateDesignerMode();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
DesignerSupport::activateDesignerWindowManager();
#endif
QQmlEngine *engine = nullptr;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto view = new QQuickView;
engine = view->engine();
m_window = view;
QSurfaceFormat surfaceFormat = view->requestedFormat();
surfaceFormat.setVersion(4, 1);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
view->setFormat(surfaceFormat);
DesignerSupport::createOpenGLContext(view);
#else
engine = new QQmlEngine;
m_renderControl = new QQuickRenderControl;
m_window = new QQuickWindow(m_renderControl);
m_window->setDefaultAlphaBuffer(true);
m_window->setColor(Qt::transparent);
m_renderControl->initialize();
#endif
QQmlComponent component(engine);
component.loadUrl(QUrl::fromLocalFile(m_source));
QObject *iconItem = component.create();
if (iconItem) {
#ifdef QUICK3D_MODULE
if (auto scene = qobject_cast<QQuick3DNode *>(iconItem)) {
qmlRegisterType<QmlDesigner::Internal::SelectionBoxGeometry>("SelectionBoxGeometry", 1, 0, "SelectionBoxGeometry");
QQmlComponent component(engine);
component.loadUrl(QUrl("qrc:/qtquickplugin/mockfiles/IconRenderer3D.qml"));
m_containerItem = qobject_cast<QQuickItem *>(component.create());
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
DesignerSupport::setRootItem(view, m_containerItem);
#else
m_window->contentItem()->setSize(m_containerItem->size());
m_window->setGeometry(0, 0, m_containerItem->width(), m_containerItem->height());
m_containerItem->setParentItem(m_window->contentItem());
#endif
auto helper = new QmlDesigner::Internal::GeneralHelper();
engine->rootContext()->setContextProperty("_generalHelper", helper);
m_contentItem = QQmlProperty::read(m_containerItem, "view3D").value<QQuickItem *>();
auto view3D = qobject_cast<QQuick3DViewport *>(m_contentItem);
view3D->setImportScene(scene);
m_is3D = true;
} else
#endif
if (auto scene = qobject_cast<QQuickItem *>(iconItem)) {
m_contentItem = scene;
m_containerItem = new QQuickItem();
m_containerItem->setSize(QSizeF(1024, 1024));
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
DesignerSupport::setRootItem(view, m_containerItem);
#else
m_window->contentItem()->setSize(m_containerItem->size());
m_window->setGeometry(0, 0, m_containerItem->width(), m_containerItem->height());
m_containerItem->setParentItem(m_window->contentItem());
#endif
m_contentItem->setParentItem(m_containerItem);
}
if (m_containerItem && m_contentItem) {
resizeContent(m_size);
if (!initRhi())
QTimer::singleShot(0, qGuiApp, &QGuiApplication::quit);
QTimer::singleShot(0, this, &IconRenderer::createIcon);
} else {
QTimer::singleShot(0, qGuiApp, &QGuiApplication::quit);
}
} else {
QTimer::singleShot(0, qGuiApp, &QGuiApplication::quit);
}
}
void IconRenderer::createIcon()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
m_designerSupport.refFromEffectItem(m_containerItem, false);
#endif
QQuickDesignerSupportItems::disableNativeTextRendering(m_containerItem);
#ifdef QUICK3D_MODULE
if (m_is3D) {
// Render once to make sure scene is up to date before we set up the selection box
render({});
QMetaObject::invokeMethod(m_containerItem, "setSceneToBox");
int tries = 0;
while (tries < 10) {
++tries;
render({});
QMetaObject::invokeMethod(m_containerItem, "fitAndHideBox");
}
}
#endif
QFileInfo fi(m_filePath);
// Render regular size image
render(fi.absoluteFilePath());
// Render @2x image
resizeContent(m_size * 2);
if (!initRhi())
QTimer::singleShot(1000, qGuiApp, &QGuiApplication::quit);
QString saveFile;
saveFile = fi.absolutePath() + '/' + fi.completeBaseName() + "@2x";
if (!fi.suffix().isEmpty())
saveFile += '.' + fi.suffix();
fi.absoluteDir().mkpath(".");
render(saveFile);
// Allow little time for file operations to finish
QTimer::singleShot(1000, qGuiApp, &QGuiApplication::quit);
}
void IconRenderer::render(const QString &fileName)
{
std::function<void (QQuickItem *)> updateNodesRecursive;
updateNodesRecursive = [&updateNodesRecursive](QQuickItem *item) {
const auto childItems = item->childItems();
for (QQuickItem *childItem : childItems)
updateNodesRecursive(childItem);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
DesignerSupport::updateDirtyNode(item);
#else
if (item->flags() & QQuickItem::ItemHasContents)
item->update();
#endif
};
updateNodesRecursive(m_containerItem);
QRect rect(QPoint(), m_contentItem->size().toSize());
QImage renderImage;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
renderImage = m_designerSupport.renderImageForItem(m_containerItem, rect, rect.size());
#else
m_renderControl->polishItems();
m_renderControl->beginFrame();
m_renderControl->sync();
m_renderControl->render();
bool readCompleted = false;
QRhiReadbackResult readResult;
readResult.completed = [&] {
readCompleted = true;
QImage wrapperImage(reinterpret_cast<const uchar *>(readResult.data.constData()),
readResult.pixelSize.width(), readResult.pixelSize.height(),
QImage::Format_RGBA8888_Premultiplied);
if (m_rhi->isYUpInFramebuffer())
renderImage = wrapperImage.mirrored().copy(0, 0, rect.width(), rect.height());
else
renderImage = wrapperImage.copy(0, 0, rect.width(), rect.height());
};
QRhiResourceUpdateBatch *readbackBatch = m_rhi->nextResourceUpdateBatch();
readbackBatch->readBackTexture(m_texture, &readResult);
QQuickRenderControlPrivate *rd = QQuickRenderControlPrivate::get(m_renderControl);
rd->cb->resourceUpdate(readbackBatch);
m_renderControl->endFrame();
#endif
if (!fileName.isEmpty()) {
QFileInfo fi(fileName);
if (fi.suffix().isEmpty())
renderImage.save(fileName, "PNG");
else
renderImage.save(fileName);
}
}
void IconRenderer::resizeContent(int dimensions)
{
QSizeF size(dimensions, dimensions);
m_contentItem->setSize(size);
if (m_contentItem->width() > m_containerItem->width())
m_containerItem->setWidth(m_contentItem->width());
if (m_contentItem->height() > m_containerItem->height())
m_containerItem->setHeight(m_contentItem->height());
}
bool IconRenderer::initRhi()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (!m_rhi) {
QQuickRenderControlPrivate *rd = QQuickRenderControlPrivate::get(m_renderControl);
m_rhi = rd->rhi;
if (!m_rhi) {
qWarning() << __FUNCTION__ << "Rhi is null";
return false;
}
}
// Don't bother deleting old ones as iconrender is a single shot executable
m_texTarget = nullptr;
m_rpDesc = nullptr;
m_buffer = nullptr;
m_texture = nullptr;
const QSize size = m_containerItem->size().toSize();
m_texture = m_rhi->newTexture(QRhiTexture::RGBA8, size, 1,
QRhiTexture::RenderTarget | QRhiTexture::UsedAsTransferSource);
if (!m_texture->create()) {
qWarning() << __FUNCTION__ << "QRhiTexture creation failed";
return false;
}
m_buffer = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, size, 1);
if (!m_buffer->create()) {
qWarning() << __FUNCTION__ << "Depth/stencil buffer creation failed";
return false;
}
QRhiTextureRenderTargetDescription rtDesc {QRhiColorAttachment(m_texture)};
rtDesc.setDepthStencilBuffer(m_buffer);
m_texTarget = m_rhi->newTextureRenderTarget(rtDesc);
m_rpDesc = m_texTarget->newCompatibleRenderPassDescriptor();
m_texTarget->setRenderPassDescriptor(m_rpDesc);
if (!m_texTarget->create()) {
qWarning() << __FUNCTION__ << "Texture render target creation failed";
return false;
}
// redirect Qt Quick rendering into our texture
m_window->setRenderTarget(QQuickRenderTarget::fromRhiRenderTarget(m_texTarget));
#endif
return true;
}
|