Skip to content

Commit 625e2e6

Browse files
committed
bugfix: immutable texture is used when creating textures, so glTexImage2D can not be used to change the texture size.
1 parent 65654aa commit 625e2e6

File tree

5 files changed

+53
-66
lines changed

5 files changed

+53
-66
lines changed

library/src/main/jni/cge/common/cgeGLFunctions.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,22 @@ void TextureObject::cleanup(bool deleteTexture)
268268

269269
bool TextureObject::resize(int w, int h, const void* buffer, GLenum format)
270270
{
271-
if (m_texture == 0 || m_size.width != w || m_size.height != h || buffer != nullptr)
271+
if (m_texture != 0 && m_size.width == w && m_size.height == h && buffer == nullptr)
272272
{
273-
if (w == 0 || h == 0)
273+
return false;
274+
}
275+
276+
if (w == 0 || h == 0)
277+
{
278+
assert(0 && (void*)"TextureObject::resize must not be 0!");
279+
return false;
280+
}
281+
282+
if (m_texture == 0 || m_size.width != w || m_size.height != h)
283+
{
284+
if (m_texture != 0)
274285
{
275-
assert(0 && "TextureObject::resize must not be 0!");
276-
return false;
286+
glDeleteTextures(1, &m_texture);
277287
}
278288

279289
int channel;
@@ -297,29 +307,17 @@ bool TextureObject::resize(int w, int h, const void* buffer, GLenum format)
297307
break;
298308
}
299309

300-
if (m_texture == 0)
301-
{
302-
m_texture = cgeGenTextureWithBuffer(buffer, w, h, format, GL_UNSIGNED_BYTE, channel);
303-
m_size.set(w, h);
304-
}
305-
else
306-
{
307-
glBindTexture(GL_TEXTURE_2D, m_texture);
308-
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
309-
if (m_size.width != w || m_size.height != h)
310-
{
311-
m_size.set(w, h);
312-
glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, buffer);
313-
}
314-
else
315-
{
316-
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, buffer);
317-
}
318-
}
319-
320-
return true;
310+
m_size.set(w, h);
311+
m_texture = cgeGenTextureWithBuffer(buffer, w, h, format, GL_UNSIGNED_BYTE, channel);
321312
}
322-
return false;
313+
else
314+
{
315+
glBindTexture(GL_TEXTURE_2D, m_texture);
316+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
317+
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, buffer);
318+
}
319+
320+
return true;
323321
}
324322

325323
//////////////

library/src/main/jni/cge/common/cgeGLFunctions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void cgeDisableGlobalGLContext();
5050
typedef void* (*CGEBufferLoadFun)(const char* sourceName, void** bufferData, GLint* w, GLint* h, CGEBufferFormat* fmt, void* arg);
5151
typedef bool (*CGEBufferUnloadFun)(void* arg1, void* arg2);
5252

53-
//加载纹理回调, 注, 为了保持接口简洁性, 回调返回的纹理单元将由调用者负责释放
54-
//返回的纹理不应该为 glDeleteTextures 无法处理的特殊纹理类型.
53+
// 加载纹理回调, 注, 为了保持接口简洁性, 回调返回的纹理单元将由调用者负责释放
54+
// 返回的纹理不应该为 glDeleteTextures 无法处理的特殊纹理类型.
5555
typedef GLuint (*CGETextureLoadFun)(const char* sourceName, GLint* w, GLint* h, void* arg);
5656

5757
// You can set a common function for loading textures

library/src/main/jni/cge/common/cgeShaderFunctions.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ class ProgramObject
224224
inline GLint _getUniform(GLuint programId, const char* name)
225225
{
226226
GLint uniform = glGetUniformLocation(programId, name);
227-
CGE_LOG_CODE(
228-
if (uniform < 0)
229-
CGE_LOG_ERROR("uniform name %s does not exist!\n", name););
227+
#ifdef DEBUG
228+
if (uniform < 0)
229+
CGE_LOG_ERROR("uniform name %s does not exist!\n", name);
230+
#endif
230231
return uniform;
231232
}
232233

library/src/main/jni/interface/cgeVideoPlayer.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ static const GLfloat s_colorConversion709[] = {
6969

7070
namespace CGE
7171
{
72-
CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P() :
73-
m_posAttribLocation(0), m_decodeHandler(nullptr), m_vertexBuffer(0)
72+
CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P()
7473
{
7574
m_program.bindAttribLocation(CGEImageFilterInterface::paramPositionIndexName, m_posAttribLocation);
7675

@@ -89,16 +88,16 @@ CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P() :
8988
m_texULoc = m_program.uniformLocation("textureU");
9089
m_texVLoc = m_program.uniformLocation("textureV");
9190

92-
glUniform1i(m_texYLoc, 1);
93-
glUniform1i(m_texULoc, 2);
94-
glUniform1i(m_texVLoc, 3);
95-
9691
if (m_texYLoc < 0 || m_texULoc < 0 || m_texVLoc < 0)
9792
{
9893
CGE_LOG_ERROR("Invalid YUV Texture Uniforms\n");
9994
}
100-
101-
memset(m_texYUV, 0, sizeof(m_texYUV));
95+
else
96+
{
97+
glUniform1i(m_texYLoc, 1);
98+
glUniform1i(m_texULoc, 2);
99+
glUniform1i(m_texVLoc, 3);
100+
}
102101

103102
m_rotLoc = m_program.uniformLocation("rotation");
104103
m_flipScaleLoc = m_program.uniformLocation("flipScale");
@@ -143,12 +142,6 @@ bool CGEVideoPlayerYUV420P::initWithDecodeHandler(CGEVideoDecodeHandler* handler
143142
m_linesize[2] = m_linesize[1] = m_linesize[0] / 2;
144143
m_videoHeight = m_decodeHandler->getHeight();
145144

146-
m_texYUV[0] = cgeGenTextureWithBuffer(nullptr, m_linesize[0], m_videoHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 1);
147-
148-
m_texYUV[1] = cgeGenTextureWithBuffer(nullptr, m_linesize[1], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 2);
149-
150-
m_texYUV[2] = cgeGenTextureWithBuffer(nullptr, m_linesize[2], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 3);
151-
152145
if (m_vertexBuffer == 0)
153146
m_vertexBuffer = cgeGenCommonQuadArrayBuffer();
154147

@@ -245,23 +238,18 @@ bool CGEVideoPlayerYUV420P::updateVideoFrame(const CGEVideoFrameBufferData* data
245238

246239
m_program.bind();
247240

248-
if (m_linesize[0] != framebuffer.linesize[0])
241+
if (m_texYUV[0] == 0 || m_linesize[0] != framebuffer.linesize[0])
249242
{
250243
m_linesize[0] = framebuffer.linesize[0];
251244
m_linesize[1] = framebuffer.linesize[1];
252245
m_linesize[2] = framebuffer.linesize[2];
253246

254-
glActiveTexture(GL_TEXTURE1);
255-
glBindTexture(GL_TEXTURE_2D, m_texYUV[0]);
256-
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[0], m_videoHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[0]);
257-
258-
glActiveTexture(GL_TEXTURE2);
259-
glBindTexture(GL_TEXTURE_2D, m_texYUV[1]);
260-
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[1], m_videoHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[1]);
247+
if (m_texYUV[0] != 0)
248+
glDeleteTextures(3, m_texYUV);
261249

262-
glActiveTexture(GL_TEXTURE3);
263-
glBindTexture(GL_TEXTURE_2D, m_texYUV[2]);
264-
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[2], m_videoHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[2]);
250+
m_texYUV[0] = cgeGenTextureWithBuffer(framebuffer.data[0], m_linesize[0], m_videoHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 1);
251+
m_texYUV[1] = cgeGenTextureWithBuffer(framebuffer.data[1], m_linesize[1], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 2);
252+
m_texYUV[2] = cgeGenTextureWithBuffer(framebuffer.data[2], m_linesize[2], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 3);
265253
}
266254
else
267255
{

library/src/main/jni/interface/cgeVideoPlayer.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CGEVideoPlayerYUV420P : public CGEVideoPlayerInterface
4848

4949
bool open(const char* filename, CGEVideoDecodeHandler::SamplingStyle s = CGEVideoDecodeHandler::ssFastBilinear);
5050

51-
//不同的初始化方式,不与 open 合用
51+
// 不同的初始化方式,不与 open 合用
5252
bool initWithDecodeHandler(CGEVideoDecodeHandler*);
5353

5454
void close();
@@ -81,15 +81,15 @@ class CGEVideoPlayerYUV420P : public CGEVideoPlayerInterface
8181

8282
protected:
8383
ProgramObject m_program;
84-
GLuint m_texYUV[3];
85-
GLint m_texYLoc, m_texULoc, m_texVLoc;
86-
GLuint m_posAttribLocation;
87-
GLuint m_rotLoc, m_flipScaleLoc;
88-
CGEVideoDecodeHandler* m_decodeHandler;
89-
90-
GLuint m_vertexBuffer;
91-
int m_videoWidth, m_videoHeight;
92-
int m_linesize[3];
84+
GLuint m_texYUV[3]{};
85+
GLint m_texYLoc = -1, m_texULoc = -1, m_texVLoc = -1;
86+
GLuint m_posAttribLocation{};
87+
GLuint m_rotLoc{}, m_flipScaleLoc{};
88+
CGEVideoDecodeHandler* m_decodeHandler{};
89+
90+
GLuint m_vertexBuffer{};
91+
int m_videoWidth{}, m_videoHeight{};
92+
int m_linesize[3]{};
9393
};
9494
} // namespace CGE
9595

0 commit comments

Comments
 (0)