forked from carlonluca/pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omx_textureprovider.cpp
161 lines (140 loc) · 5.36 KB
/
omx_textureprovider.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Project: PiOmxTextures
* Author: Luca Carlon
* Date: 11.01.2012
*
* Copyright (c) 2012, 2013 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 <QQuickWindow>
#include <QOpenGLContext>
#include <QGuiApplication>
// Private headers.
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformnativeinterface.h>
#include <IL/OMX_Broadcom.h>
#include "lc_logging.h"
#include "omx_textureprovider.h"
#include "omx_globals.h"
/*------------------------------------------------------------------------------
| OMX_TextureData::OMX_TextureData
+-----------------------------------------------------------------------------*/
OMX_TextureData::OMX_TextureData() :
m_textureId(0),
m_textureData(NULL),
m_eglImage(NULL),
m_textureSize(QSize(0, 0))
{
// Do nothing.
}
/*------------------------------------------------------------------------------
| OMX_TextureData::OMX_TextureData
+-----------------------------------------------------------------------------*/
OMX_TextureData::OMX_TextureData(const OMX_TextureData& textureData) :
m_textureId(textureData.m_textureId),
m_textureData(textureData.m_textureData),
m_eglImage(textureData.m_eglImage),
m_textureSize(textureData.m_textureSize)
{
// Do nothing.
}
/*------------------------------------------------------------------------------
| OMX_TextureData::~OMX_TextureData
+-----------------------------------------------------------------------------*/
OMX_TextureData::~OMX_TextureData()
{
// Do not free.
}
/*------------------------------------------------------------------------------
| OMX_TextureData::freeData
+-----------------------------------------------------------------------------*/
void OMX_TextureData::freeData()
{
if (!m_textureData || !m_textureId || !m_eglImage)
log_warn("Double free of OMX texture data requested.");
EGLDisplay eglDisplay = get_egl_display();
// Destroy texture, EGL image and free the buffer.
log_debug("Freeing KHR image...");
if (m_eglImage && eglDestroyImageKHR(eglDisplay, m_eglImage) == EGL_SUCCESS) {
EGLint err = eglGetError();
LOG_ERROR(LOG_TAG, "Failed to destroy EGLImageKHR: %d.", err);
}
m_eglImage = NULL;
if (m_textureId) {
log_debug("Freeing texture...");
glDeleteTextures(1, &m_textureId);
m_textureId = 0;
}
if (m_textureData) {
log_debug("Freeing texture data...");
delete m_textureData;
m_textureData = NULL;
}
}
/*------------------------------------------------------------------------------
| OpenMAXILTextureLoader::instantiateTexture
+-----------------------------------------------------------------------------*/
OMX_TextureData* OMX_TextureProviderQQuickItem::instantiateTexture(QSize size)
{
EGLDisplay eglDisplay = get_egl_display();
EGLContext eglContext = get_egl_context();
EGLint attr[] = {EGL_GL_TEXTURE_LEVEL_KHR, 0, EGL_NONE};
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// It seems that only 4byte pixels is supported here.
GLubyte* pixel = new GLubyte[size.width()*size.height()*4];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width(), size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
EGLImageKHR eglImage = eglCreateImageKHR(
eglDisplay,
eglContext,
EGL_GL_TEXTURE_2D_KHR,
(EGLClientBuffer)textureId,
attr
);
EGLint eglErr = eglGetError();
if (eglErr != EGL_SUCCESS) {
LOG_ERROR(LOG_TAG, "Failed to create KHR image: %d.", eglErr);
return 0;
}
OMX_TextureData* textureData = new OMX_TextureData;
textureData->m_textureId = textureId;
textureData->m_textureData = pixel;
textureData->m_eglImage = eglImage;
textureData->m_textureSize = size;
return textureData;
}
/*------------------------------------------------------------------------------
| OpenMAXILTextureLoader::freeTexture
+-----------------------------------------------------------------------------*/
void OMX_TextureProviderQQuickItem::freeTexture(OMX_TextureData* textureData)
{
if (!textureData) {
log_warn("Trying to free a NULL texture data object.");
return;
}
textureData->freeData();
delete textureData;
}