forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomx_texturedelement.cpp
103 lines (93 loc) · 3.89 KB
/
omx_texturedelement.cpp
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
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 12.18.2012
*
* Copyright (c) 2012 Luca Carlon. All rights reserved.
*
* This file is part of PiOmxTextures.
*
* PiOmxTextures is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PiOmxTextures is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PiOmxTextures. If not, see <http://www.gnu.org/licenses/>.
*/
/*------------------------------------------------------------------------------
| includes
+-----------------------------------------------------------------------------*/
#include <QSGOpaqueTextureMaterial>
#include <QQuickWindow>
#include "omx_texturedelement.h"
/*------------------------------------------------------------------------------
| OMX_TextureElement::OMX_TexturedElement
+-----------------------------------------------------------------------------*/
OMX_TexturedElement::OMX_TexturedElement(QQuickItem* parent) :
QQuickItem(parent),
m_texture(0)
{
// Do nothing.
}
OMX_TexturedElement::~OMX_TexturedElement()
{
// TODO: Do I have to dealloc something?
}
/*------------------------------------------------------------------------------
| OMX_TexturedElement::updatePaintNode
+-----------------------------------------------------------------------------*/
QSGNode* OMX_TexturedElement::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
{
QSGGeometryNode* node = 0;
QSGGeometry* geometry = 0;
if (!oldNode) {
// Create the node.
node = new QSGGeometryNode;
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
geometry->setDrawingMode(GL_TRIANGLE_STRIP);
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
// TODO: Who is freeing this?
// TODO: Who is freeing the texture?
QSGOpaqueTextureMaterial* material = new QSGOpaqueTextureMaterial;
QSGTexture* mySGTexture = window()->createTextureFromId(m_texture, QSize(1920, 1080));
material->setTexture(mySGTexture);
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
}
else {
node = static_cast<QSGGeometryNode*>(oldNode);
geometry = node->geometry();
geometry->allocate(4);
updateTexture(node);
}
// Create the vertices and map to texture.
QRectF bounds = boundingRect();
QSGGeometry::TexturedPoint2D* vertices = geometry->vertexDataAsTexturedPoint2D();
vertices[0].set(bounds.x(), bounds.y() + bounds.height(), 0.0f, 1.0f);
vertices[1].set(bounds.x() + bounds.width(), bounds.y() + bounds.height(), 1.0f, 1.0f);
vertices[2].set(bounds.x(), bounds.y(), 0.0f, 0.0f);
vertices[3].set(bounds.x() + bounds.width(), bounds.y(), 1.0f, 0.0f);
return node;
}
/*------------------------------------------------------------------------------
| OMX_TexturedElement::updateTexture
+-----------------------------------------------------------------------------*/
inline
void OMX_TexturedElement::updateTexture(QSGGeometryNode*& node)
{
// Update texture in the node if needed.
QSGOpaqueTextureMaterial* material = (QSGOpaqueTextureMaterial*)node->material();
if (m_texture != (GLuint)material->texture()->textureId()) {
// TODO: Does setTextureId frees the prev texture?
// TODO: The size must be determined and passed from outside.
QSGTexture* mySGTexture = window()->createTextureFromId(m_texture, QSize(1920, 1080));
material->setTexture(mySGTexture);
}
}