aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scenegraph/openvg/qsgopenvgpublicnodes.cpp
blob: e58652f942b71f5375a4856493220a317cb4bdc6 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qsgopenvgpublicnodes.h"
#include "qsgopenvghelpers.h"
#include <VG/openvg.h>
#include <QtGui/QPixmap>

QT_BEGIN_NAMESPACE

QSGOpenVGRectangleNode::QSGOpenVGRectangleNode()
{
    // Set Dummy material and geometry to avoid asserts
    setMaterial((QSGMaterial*)1);
    setGeometry((QSGGeometry*)1);

    m_rectPath = vgCreatePath(VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_F, 1, 0, 0, 0,
                              VG_PATH_CAPABILITY_APPEND_TO);
    m_rectPaint = vgCreatePaint();
}

QSGOpenVGRectangleNode::~QSGOpenVGRectangleNode()
{
    vgDestroyPaint(m_rectPaint);
    vgDestroyPath(m_rectPath);
}

void QSGOpenVGRectangleNode::setRect(const QRectF &rect)
{
    m_rect = rect;
    m_pathDirty = true;
    markDirty(DirtyMaterial);
}

QRectF QSGOpenVGRectangleNode::rect() const
{
    return m_rect;
}

void QSGOpenVGRectangleNode::setColor(const QColor &color)
{
    m_color = color;
    m_paintDirty = true;
    markDirty(DirtyMaterial);
}

QColor QSGOpenVGRectangleNode::color() const
{
    return m_color;
}

void QSGOpenVGRectangleNode::setTransform(const QOpenVGMatrix &transform)
{
    // if there transform matrix is not affine, regenerate the path
    if (transform.isAffine())
        m_pathDirty = true;
    markDirty(DirtyGeometry);

    QSGOpenVGRenderable::setTransform(transform);
}

void QSGOpenVGRectangleNode::render()
{
    // Set Transform
    if (transform().isAffine()) {
        // Use current transform matrix
        vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
        vgLoadMatrix(transform().constData());
    } else {
        // map the path's to handle the perspective matrix
        vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
        vgLoadIdentity();
    }

    if (m_pathDirty) {
        vgClearPath(m_rectPath, VG_PATH_CAPABILITY_APPEND_TO);

        if (transform().isAffine()) {
        // Create command list
        static const VGubyte rectCommands[] = {
            VG_MOVE_TO_ABS,
            VG_HLINE_TO_REL,
            VG_VLINE_TO_REL,
            VG_HLINE_TO_REL,
            VG_CLOSE_PATH
        };

        // Create command data
        QVector<VGfloat> coordinates(5);
        coordinates[0] = m_rect.x();
        coordinates[1] = m_rect.y();
        coordinates[2] = m_rect.width();
        coordinates[3] = m_rect.height();
        coordinates[4] = -m_rect.width();

        vgAppendPathData(m_rectPath, 5, rectCommands, coordinates.constData());

        } else {
            // Pre-transform path
            static const VGubyte rectCommands[] = {
                VG_MOVE_TO_ABS,
                VG_LINE_TO_ABS,
                VG_LINE_TO_ABS,
                VG_LINE_TO_ABS,
                VG_CLOSE_PATH
            };

            QVector<VGfloat> coordinates(8);
            const QPointF topLeft = transform().map(m_rect.topLeft());
            const QPointF topRight = transform().map(m_rect.topRight());
            const QPointF bottomLeft = transform().map(m_rect.bottomLeft());
            const QPointF bottomRight = transform().map(m_rect.bottomRight());
            coordinates[0] = bottomLeft.x();
            coordinates[1] = bottomLeft.y();
            coordinates[2] = bottomRight.x();
            coordinates[3] = bottomRight.y();
            coordinates[4] = topRight.x();
            coordinates[5] = topRight.y();
            coordinates[6] = topLeft.x();
            coordinates[7] = topLeft.y();

            vgAppendPathData(m_rectPath, 5, rectCommands, coordinates.constData());
        }

        m_pathDirty = false;
    }

    if (m_paintDirty) {
        vgSetPaint(m_rectPaint, VG_FILL_PATH);
        vgSetParameteri(m_rectPaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
        vgSetParameterfv(m_rectPaint, VG_PAINT_COLOR, 4, QSGOpenVGHelpers::qColorToVGColor(m_color).constData());

        m_paintDirty = false;
    }

    vgSetPaint(m_rectPaint, VG_FILL_PATH);
    vgDrawPath(m_rectPath, VG_FILL_PATH);

}

QSGOpenVGImageNode::QSGOpenVGImageNode() : m_texture(nullptr), m_owns(false)
{
    // Set Dummy material and geometry to avoid asserts
    setMaterial((QSGMaterial*)1);
    setGeometry((QSGGeometry*)1);

}

QSGOpenVGImageNode::~QSGOpenVGImageNode()
{
    if (m_owns)
        delete m_texture;
}

void QSGOpenVGImageNode::setRect(const QRectF &rect)
{
    m_rect = rect;
    markDirty(DirtyMaterial);
}

QRectF QSGOpenVGImageNode::rect() const
{
    return m_rect;
}

void QSGOpenVGImageNode::setSourceRect(const QRectF &r)
{
    m_sourceRect = r;
}

QRectF QSGOpenVGImageNode::sourceRect() const
{
    return m_sourceRect;
}

void QSGOpenVGImageNode::setTexture(QSGTexture *texture)
{
    if (m_owns)
        delete m_texture;
    m_texture = texture;
    markDirty(DirtyMaterial);
}

QSGTexture *QSGOpenVGImageNode::texture() const
{
    return m_texture;
}

void QSGOpenVGImageNode::setFiltering(QSGTexture::Filtering filtering)
{
    m_filtering = filtering;
    markDirty(DirtyMaterial);
}

QSGTexture::Filtering QSGOpenVGImageNode::filtering() const
{
    return m_filtering;
}

void QSGOpenVGImageNode::setMipmapFiltering(QSGTexture::Filtering)
{
}

QSGTexture::Filtering QSGOpenVGImageNode::mipmapFiltering() const
{
    return QSGTexture::None;
}

void QSGOpenVGImageNode::setTextureCoordinatesTransform(QSGImageNode::TextureCoordinatesTransformMode transformNode)
{
    if (m_transformMode == transformNode)
        return;
    m_transformMode = transformNode;
    markDirty(DirtyGeometry);
}

QSGImageNode::TextureCoordinatesTransformMode QSGOpenVGImageNode::textureCoordinatesTransform() const
{
    return m_transformMode;
}

void QSGOpenVGImageNode::setOwnsTexture(bool owns)
{
    m_owns = owns;
}

bool QSGOpenVGImageNode::ownsTexture() const
{
    return m_owns;
}

void QSGOpenVGImageNode::render()
{
    if (!m_texture) {
        return;
    }

    // Set Draw Mode
    if (opacity() < 1.0) {
        //Transparent
        vgSetPaint(opacityPaint(), VG_FILL_PATH);
        vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_MULTIPLY);
    } else {
        vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
    }

    // Set Transform
    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    vgLoadMatrix(transform().constData());

    VGImage image = static_cast<VGImage>(m_texture->textureId());

    //Apply the TextureCoordinateTransform Flag
    if (m_transformMode != QSGImageNode::NoTransform) {
        float translateX = 0.0f;
        float translateY = 0.0f;
        float scaleX = 1.0f;
        float scaleY = 1.0f;

        if (m_transformMode & QSGImageNode::MirrorHorizontally) {
            translateX = m_rect.width();
            scaleX = -1.0;
        }

        if (m_transformMode & QSGImageNode::MirrorVertically) {
            translateY = m_rect.height();
            scaleY = -1.0;
        }

        vgTranslate(translateX, translateY);
        vgScale(scaleX, scaleY);
    }

    // If the the source rect is the same as the target rect
    if (m_sourceRect == m_rect) {
        vgDrawImage(image);
    } else {
        // Scale
        float scaleX = m_rect.width() / m_sourceRect.width();
        float scaleY = m_rect.height() / m_sourceRect.height();
        vgScale(scaleX, scaleY);
        VGImage subImage = vgChildImage(image, m_sourceRect.x(), m_sourceRect.y(), m_sourceRect.width(), m_sourceRect.height());
        vgDrawImage(subImage);
        vgDestroyImage(subImage);
    }

}

QSGOpenVGNinePatchNode::QSGOpenVGNinePatchNode() : m_texture(nullptr)
{
    // Set Dummy material and geometry to avoid asserts
    setMaterial((QSGMaterial*)1);
    setGeometry((QSGGeometry*)1);

}

QSGOpenVGNinePatchNode::~QSGOpenVGNinePatchNode()
{
    delete m_texture;
}

void QSGOpenVGNinePatchNode::setTexture(QSGTexture *texture)
{
    delete m_texture;
    m_texture = texture;
    markDirty(DirtyMaterial);
}

void QSGOpenVGNinePatchNode::setBounds(const QRectF &bounds)
{
    if (m_bounds == bounds)
        return;
    m_bounds = bounds;
    markDirty(DirtyGeometry);
}

void QSGOpenVGNinePatchNode::setDevicePixelRatio(qreal ratio)
{
    if (m_pixelRatio == ratio)
        return;
    m_pixelRatio = ratio;
    markDirty(DirtyGeometry);
}

void QSGOpenVGNinePatchNode::setPadding(qreal left, qreal top, qreal right, qreal bottom)
{
    QMarginsF margins(left, top, right, bottom);
    if (m_margins == margins)
        return;
    m_margins = margins;
    markDirty(DirtyGeometry);
}

void QSGOpenVGNinePatchNode::update()
{

}

void QSGOpenVGNinePatchNode::render()
{
    if (!m_texture)
        return;

    // Set Draw Mode
    if (opacity() < 1.0) {
        //Transparent
        vgSetPaint(opacityPaint(), VG_FILL_PATH);
        vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_MULTIPLY);
    } else {
        vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
    }

    // Set Transform
    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    vgLoadMatrix(transform().constData());

    VGImage image = static_cast<VGImage>(m_texture->textureId());

    //Draw borderImage
    QSGOpenVGHelpers::qDrawBorderImage(image, m_texture->textureSize(), m_bounds, m_bounds.marginsRemoved(m_margins), QRectF(0, 0, 1, 1));
}

QRectF QSGOpenVGNinePatchNode::bounds() const
{
    return m_bounds;
}


QT_END_NAMESPACE