forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx_imageelement.cpp
139 lines (124 loc) · 5.14 KB
/
omx_imageelement.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
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
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 12.16.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 <QSGNode>
// Private headers.
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformnativeinterface.h>
#include "omx_imageelement.h"
#include "omx_texture.h"
#include "lc_logging.h"
#include "openmaxiltextureloader.h"
/*------------------------------------------------------------------------------
| OMX_ImageElement::OMX_ImageElement
+-----------------------------------------------------------------------------*/
OMX_ImageElement::OMX_ImageElement(QQuickItem *parent)
: QQuickItem(parent),
m_source(""),
m_texture(0)
{
setFlag(ItemHasContents, true);
}
/*------------------------------------------------------------------------------
| OMX_ImageElement::~OMX_ImageElement
+-----------------------------------------------------------------------------*/
OMX_ImageElement::~OMX_ImageElement()
{
}
/*------------------------------------------------------------------------------
| OMX_ImageElement::updatePaintNode
+-----------------------------------------------------------------------------*/
QSGNode* OMX_ImageElement::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);
// Load and set the texture. The first time it is always needed to do this.
updateTexture();
// TODO: Who is freeing this?
QSGOpaqueTextureMaterial* material = new QSGOpaqueTextureMaterial;
material->setTexture(new OMX_SGTexture(m_texture, QSize(1920, 1080)));
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
}
else {
node = static_cast<QSGGeometryNode *>(oldNode);
geometry = node->geometry();
geometry->allocate(4);
// Update texture in the node if needed.
QSGOpaqueTextureMaterial* material = (QSGOpaqueTextureMaterial*)node->material();
if (m_texture != (GLuint)material->texture()->textureId()) {
updateTexture();
// TODO: Does setTextureId frees the prev texture?
OMX_SGTexture* mySGTexture = (OMX_SGTexture*)material->texture();
mySGTexture->setTexture(m_texture, QSize(1920, 1080));
}
}
// 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_ImageElement::updateTexture
+-----------------------------------------------------------------------------*/
inline
void OMX_ImageElement::updateTexture()
{
// Load the image.
QPlatformNativeInterface* nativeInterface =
QGuiApplicationPrivate::platformIntegration()->nativeInterface();
Q_ASSERT(nativeInterface);
EGLDisplay eglDisplay = nativeInterface->nativeResourceForIntegration("egldisplay");
EGLContext eglContext = nativeInterface->nativeResourceForContext(
"eglcontext",
QOpenGLContext::currentContext()
);
// TODO: Who is freeing the loader?
OpenMAXILTextureLoader* loader = OpenMAXILTextureLoader::intance();
if (!QFile(m_source).exists())
return;
for (int i = 0; i < 1000; i++) {
if (!loader->loadTextureFromImage(m_source, eglDisplay, eglContext, m_texture)) {
LOG_ERROR(LOG_TAG, "Failed to load image.");
return;
}
glDeleteTextures(1, &m_texture);
}
loader->freeInstance();
LOG_INFORMATION(LOG_TAG, "Image successfully loaded.");
}