OpenCoverage

qquickshadereffectmesh.cpp

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquickshadereffectmesh.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
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 "qquickshadereffectmesh_p.h"-
41#include <QtQuick/qsggeometry.h>-
42#include "qquickshadereffect_p.h"-
43#include "qquickscalegrid_p_p.h"-
44#include "qquickborderimage_p_p.h"-
45#include <QtQuick/private/qsgbasicinternalimagenode_p.h>-
46-
47QT_BEGIN_NAMESPACE-
48-
49static const char qt_position_attribute_name[] = "qt_Vertex";-
50static const char qt_texcoord_attribute_name[] = "qt_MultiTexCoord0";-
51-
52const char *qtPositionAttributeName()-
53{-
54 return qt_position_attribute_name;
executed 238 times by 5 tests: return qt_position_attribute_name;
Executed by:
  • tst_examples
  • tst_qquickborderimage
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
238
55}-
56-
57const char *qtTexCoordAttributeName()-
58{-
59 return qt_texcoord_attribute_name;
executed 246 times by 5 tests: return qt_texcoord_attribute_name;
Executed by:
  • tst_examples
  • tst_qquickborderimage
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
246
60}-
61-
62QQuickShaderEffectMesh::QQuickShaderEffectMesh(QObject *parent)-
63 : QObject(parent)-
64{-
65}
executed 110 times by 5 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickborderimage
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
110
66-
67/*!-
68 \qmltype GridMesh-
69 \instantiates QQuickGridMesh-
70 \inqmlmodule QtQuick-
71 \since 5.0-
72 \ingroup qtquick-effects-
73 \brief Defines a mesh with vertices arranged in a grid.-
74-
75 GridMesh defines a rectangular mesh consisting of vertices arranged in an-
76 evenly spaced grid. It is used to generate \l{QSGGeometry}{geometry}.-
77 The grid resolution is specified with the \l resolution property.-
78*/-
79-
80QQuickGridMesh::QQuickGridMesh(QObject *parent)-
81 : QQuickShaderEffectMesh(parent)-
82 , m_resolution(1, 1)-
83{-
84}
executed 108 times by 5 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickborderimage
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
108
85-
86bool QQuickGridMesh::validateAttributes(const QVector<QByteArray> &attributes, int *posIndex)-
87{-
88 const int attrCount = attributes.count();-
89 int positionIndex = attributes.indexOf(qtPositionAttributeName());-
90 int texCoordIndex = attributes.indexOf(qtTexCoordAttributeName());-
91-
92 switch (attrCount) {-
93 case 0:
never executed: case 0:
0
94 m_log = QLatin1String("Error: No attributes specified.");-
95 return false;
never executed: return false;
0
96 case 1:
never executed: case 1:
0
97 if (positionIndex != 0) {
positionIndex != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
98 m_log = QLatin1String("Error: Missing \'") + QLatin1String(qtPositionAttributeName())-
99 + QLatin1String("\' attribute.\n");-
100 return false;
never executed: return false;
0
101 }-
102 break;
never executed: break;
0
103 case 2:
executed 62 times by 4 tests: case 2:
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
104 if (positionIndex == -1 || texCoordIndex == -1) {
positionIndex == -1Description
TRUEnever evaluated
FALSEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
texCoordIndex == -1Description
TRUEnever evaluated
FALSEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
0-62
105 m_log.clear();-
106 if (positionIndex == -1) {
positionIndex == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
107 m_log = QLatin1String("Error: Missing \'") + QLatin1String(qtPositionAttributeName())-
108 + QLatin1String("\' attribute.\n");-
109 }
never executed: end of block
0
110 if (texCoordIndex == -1) {
texCoordIndex == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
111 m_log += QLatin1String("Error: Missing \'") + QLatin1String(qtTexCoordAttributeName())-
112 + QLatin1String("\' attribute.\n");-
113 }
never executed: end of block
0
114 return false;
never executed: return false;
0
115 }-
116 break;
executed 62 times by 4 tests: break;
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
117 default:
never executed: default:
0
118 m_log = QLatin1String("Error: Too many attributes specified.");-
119 return false;
never executed: return false;
0
120 }-
121-
122 if (posIndex)
posIndexDescription
TRUEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEnever evaluated
0-62
123 *posIndex = positionIndex;
executed 62 times by 4 tests: *posIndex = positionIndex;
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
124-
125 return true;
executed 62 times by 4 tests: return true;
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
126}-
127-
128QSGGeometry *QQuickGridMesh::updateGeometry(QSGGeometry *geometry, int attrCount, int posIndex,-
129 const QRectF &srcRect, const QRectF &dstRect)-
130{-
131 int vmesh = m_resolution.height();-
132 int hmesh = m_resolution.width();-
133-
134 if (!geometry) {
!geometryDescription
TRUEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEnever evaluated
0-62
135 Q_ASSERT(attrCount == 1 || attrCount == 2);-
136 geometry = new QSGGeometry(attrCount == 1-
137 ? QSGGeometry::defaultAttributes_Point2D()-
138 : QSGGeometry::defaultAttributes_TexturedPoint2D(),-
139 (vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2),-
140 QSGGeometry::UnsignedShortType);-
141-
142 } else {
executed 62 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
143 geometry->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));-
144 }
never executed: end of block
0
145-
146 QSGGeometry::Point2D *vdata = static_cast<QSGGeometry::Point2D *>(geometry->vertexData());-
147-
148 for (int iy = 0; iy <= vmesh; ++iy) {
iy <= vmeshDescription
TRUEevaluated 124 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62-124
149 float fy = iy / float(vmesh);-
150 float y = float(dstRect.top()) + fy * float(dstRect.height());-
151 float ty = float(srcRect.top()) + fy * float(srcRect.height());-
152 for (int ix = 0; ix <= hmesh; ++ix) {
ix <= hmeshDescription
TRUEevaluated 248 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 124 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
124-248
153 float fx = ix / float(hmesh);-
154 for (int ia = 0; ia < attrCount; ++ia) {
ia < attrCountDescription
TRUEevaluated 496 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 248 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
248-496
155 if (ia == posIndex) {
ia == posIndexDescription
TRUEevaluated 248 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 248 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
248
156 vdata->x = float(dstRect.left()) + fx * float(dstRect.width());-
157 vdata->y = y;-
158 ++vdata;-
159 } else {
executed 248 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
248
160 vdata->x = float(srcRect.left()) + fx * float(srcRect.width());-
161 vdata->y = ty;-
162 ++vdata;-
163 }
executed 248 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
248
164 }-
165 }
executed 248 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
248
166 }
executed 124 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
124
167-
168 quint16 *indices = (quint16 *)geometry->indexDataAsUShort();-
169 int i = 0;-
170 for (int iy = 0; iy < vmesh; ++iy) {
iy < vmeshDescription
TRUEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
171 *(indices++) = i + hmesh + 1;-
172 for (int ix = 0; ix <= hmesh; ++ix, ++i) {
ix <= hmeshDescription
TRUEevaluated 124 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
FALSEevaluated 62 times by 4 tests
Evaluated by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62-124
173 *(indices++) = i + hmesh + 1;-
174 *(indices++) = i;-
175 }
executed 124 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
124
176 *(indices++) = i - 1;-
177 }
executed 62 times by 4 tests: end of block
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
178-
179 return geometry;
executed 62 times by 4 tests: return geometry;
Executed by:
  • tst_examples
  • tst_qquickitemlayer
  • tst_qquickshadereffect
  • tst_scenegraph
62
180}-
181-
182/*!-
183 \qmlproperty size QtQuick::GridMesh::resolution-
184-
185 This property holds the grid resolution. The resolution's width and height-
186 specify the number of cells or spacings between vertices horizontally and-
187 vertically respectively. The minimum and default is 1x1, which corresponds-
188 to four vertices in total, one in each corner.-
189 For non-linear vertex transformations, you probably want to set the-
190 resolution higher.-
191-
192 \table-
193 \row-
194 \li \image declarative-gridmesh.png-
195 \li \qml-
196 import QtQuick 2.0-
197-
198 ShaderEffect {-
199 width: 200-
200 height: 200-
201 mesh: GridMesh {-
202 resolution: Qt.size(20, 20)-
203 }-
204 property variant source: Image {-
205 source: "qt-logo.png"-
206 sourceSize { width: 200; height: 200 }-
207 }-
208 vertexShader: "-
209 uniform highp mat4 qt_Matrix;-
210 attribute highp vec4 qt_Vertex;-
211 attribute highp vec2 qt_MultiTexCoord0;-
212 varying highp vec2 qt_TexCoord0;-
213 uniform highp float width;-
214 void main() {-
215 highp vec4 pos = qt_Vertex;-
216 highp float d = .5 * smoothstep(0., 1., qt_MultiTexCoord0.y);-
217 pos.x = width * mix(d, 1.0 - d, qt_MultiTexCoord0.x);-
218 gl_Position = qt_Matrix * pos;-
219 qt_TexCoord0 = qt_MultiTexCoord0;-
220 }"-
221 }-
222 \endqml-
223 \endtable-
224*/-
225-
226void QQuickGridMesh::setResolution(const QSize &res)-
227{-
228 if (res == m_resolution)
res == m_resolutionDescription
TRUEnever evaluated
FALSEnever evaluated
0
229 return;
never executed: return;
0
230 if (res.width() < 1 || res.height() < 1) {
res.width() < 1Description
TRUEnever evaluated
FALSEnever evaluated
res.height() < 1Description
TRUEnever evaluated
FALSEnever evaluated
0
231 return;
never executed: return;
0
232 }-
233 m_resolution = res;-
234 emit resolutionChanged();-
235 emit geometryChanged();-
236}
never executed: end of block
0
237-
238QSize QQuickGridMesh::resolution() const-
239{-
240 return m_resolution;
never executed: return m_resolution;
0
241}-
242-
243/*!-
244 \qmltype BorderImageMesh-
245 \instantiates QQuickBorderImageMesh-
246 \inqmlmodule QtQuick-
247 \since 5.8-
248 \ingroup qtquick-effects-
249 \brief Defines a mesh with vertices arranged like those of a BorderImage.-
250-
251 BorderImageMesh provides BorderImage-like capabilities to a ShaderEffect-
252 without the need for a potentially costly ShaderEffectSource.-
253-
254 The following are functionally equivalent:-
255 \qml-
256 BorderImage {-
257 id: borderImage-
258 border {-
259 left: 10-
260 right: 10-
261 top: 10-
262 bottom: 10-
263 }-
264 source: "myImage.png"-
265 visible: false-
266 }-
267 ShaderEffectSource {-
268 id: effectSource-
269 sourceItem: borderImage-
270 visible: false-
271 }-
272 ShaderEffect {-
273 property var source: effectSource-
274 ...-
275 }-
276 \endqml-
277-
278 \qml-
279 Image {-
280 id: image-
281 source: "myImage.png"-
282 visible: false-
283 }-
284 ShaderEffect {-
285 property var source: image-
286 mesh: BorderImageMesh {-
287 border {-
288 left: 10-
289 right: 10-
290 top: 10-
291 bottom: 10-
292 }-
293 size: image.sourceSize-
294 }-
295 ...-
296 }-
297 \endqml-
298-
299 But the BorderImageMesh version can typically be better optimized.-
300*/-
301QQuickBorderImageMesh::QQuickBorderImageMesh(QObject *parent)-
302 : QQuickShaderEffectMesh(parent), m_border(new QQuickScaleGrid(this)),-
303 m_horizontalTileMode(QQuickBorderImageMesh::Stretch),-
304 m_verticalTileMode(QQuickBorderImageMesh::Stretch)-
305{-
306}
executed 2 times by 1 test: end of block
Executed by:
  • tst_qquickborderimage
2
307-
308bool QQuickBorderImageMesh::validateAttributes(const QVector<QByteArray> &attributes, int *posIndex)-
309{-
310 Q_UNUSED(attributes);-
311 Q_UNUSED(posIndex);-
312 return true;
executed 2 times by 1 test: return true;
Executed by:
  • tst_qquickborderimage
2
313}-
314-
315QSGGeometry *QQuickBorderImageMesh::updateGeometry(QSGGeometry *geometry, int attrCount, int posIndex,-
316 const QRectF &srcRect, const QRectF &rect)-
317{-
318 Q_UNUSED(attrCount);-
319 Q_UNUSED(posIndex);-
320-
321 QRectF innerSourceRect;-
322 QRectF targetRect;-
323 QRectF innerTargetRect;-
324 QRectF subSourceRect;-
325-
326 QQuickBorderImagePrivate::calculateRects(m_border, m_size, rect.size(), m_horizontalTileMode, m_verticalTileMode,-
327 1, &targetRect, &innerTargetRect, &innerSourceRect, &subSourceRect);-
328-
329 QRectF sourceRect = srcRect;-
330 QRectF modifiedInnerSourceRect(sourceRect.x() + innerSourceRect.x() * sourceRect.width(),-
331 sourceRect.y() + innerSourceRect.y() * sourceRect.height(),-
332 innerSourceRect.width() * sourceRect.width(),-
333 innerSourceRect.height() * sourceRect.height());-
334-
335 geometry = QSGBasicInternalImageNode::updateGeometry(targetRect, innerTargetRect, sourceRect,-
336 modifiedInnerSourceRect, subSourceRect, geometry);-
337-
338 return geometry;
executed 2 times by 1 test: return geometry;
Executed by:
  • tst_qquickborderimage
2
339}-
340-
341/*!-
342 \qmlpropertygroup QtQuick::BorderImageMesh::border-
343 \qmlproperty int QtQuick::BorderImageMesh::border.left-
344 \qmlproperty int QtQuick::BorderImageMesh::border.right-
345 \qmlproperty int QtQuick::BorderImageMesh::border.top-
346 \qmlproperty int QtQuick::BorderImageMesh::border.bottom-
347-
348 The 4 border lines (2 horizontal and 2 vertical) break the image into 9 sections,-
349 as shown below:-
350-
351 \image declarative-scalegrid.png-
352-
353 Each border line (left, right, top, and bottom) specifies an offset in pixels-
354 from the respective edge of the mesh. By default, each border line has-
355 a value of 0.-
356-
357 For example, the following definition sets the bottom line 10 pixels up from-
358 the bottom of the mesh:-
359-
360 \qml-
361 BorderImageMesh {-
362 border.bottom: 10-
363 // ...-
364 }-
365 \endqml-
366*/-
367QQuickScaleGrid *QQuickBorderImageMesh::border() const-
368{-
369 return m_border;
executed 2 times by 1 test: return m_border;
Executed by:
  • tst_qquickborderimage
2
370}-
371-
372/*!-
373 \qmlproperty size QtQuick::BorderImageMesh::size-
374-
375 The base size of the mesh. This generally corresponds to the \l {Image::}{sourceSize}-
376 of the image being used by the ShaderEffect.-
377*/-
378QSize QQuickBorderImageMesh::size() const-
379{-
380 return m_size;
never executed: return m_size;
0
381}-
382-
383void QQuickBorderImageMesh::setSize(const QSize &size)-
384{-
385 if (size == m_size)
size == m_sizeDescription
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_qquickborderimage
0-4
386 return;
never executed: return;
0
387 m_size = size;-
388 Q_EMIT sizeChanged();-
389 Q_EMIT geometryChanged();-
390}
executed 4 times by 1 test: end of block
Executed by:
  • tst_qquickborderimage
4
391-
392/*!-
393 \qmlproperty enumeration QtQuick::BorderImageMesh::horizontalTileMode-
394 \qmlproperty enumeration QtQuick::BorderImageMesh::verticalTileMode-
395-
396 This property describes how to repeat or stretch the middle parts of an image.-
397-
398 \list-
399 \li BorderImage.Stretch - Scales the image to fit to the available area.-
400 \li BorderImage.Repeat - Tile the image until there is no more space. May crop the last image.-
401 \li BorderImage.Round - Like Repeat, but scales the images down to ensure that the last image is not cropped.-
402 \endlist-
403-
404 The default tile mode for each property is BorderImage.Stretch.-
405*/-
406-
407QQuickBorderImageMesh::TileMode QQuickBorderImageMesh::horizontalTileMode() const-
408{-
409 return m_horizontalTileMode;
never executed: return m_horizontalTileMode;
0
410}-
411-
412void QQuickBorderImageMesh::setHorizontalTileMode(TileMode t)-
413{-
414 if (t == m_horizontalTileMode)
t == m_horizontalTileModeDescription
TRUEnever evaluated
FALSEnever evaluated
0
415 return;
never executed: return;
0
416 m_horizontalTileMode = t;-
417 Q_EMIT horizontalTileModeChanged();-
418 Q_EMIT geometryChanged();-
419}
never executed: end of block
0
420-
421QQuickBorderImageMesh::TileMode QQuickBorderImageMesh::verticalTileMode() const-
422{-
423 return m_verticalTileMode;
never executed: return m_verticalTileMode;
0
424}-
425-
426void QQuickBorderImageMesh::setVerticalTileMode(TileMode t)-
427{-
428 if (t == m_verticalTileMode)
t == m_verticalTileModeDescription
TRUEnever evaluated
FALSEnever evaluated
0
429 return;
never executed: return;
0
430-
431 m_verticalTileMode = t;-
432 Q_EMIT verticalTileModeChanged();-
433 Q_EMIT geometryChanged();-
434}
never executed: end of block
0
435-
436QT_END_NAMESPACE-
437-
438#include "moc_qquickshadereffectmesh_p.cpp"-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.0