diff --git a/library/src/main/jni/cge/common/cgeGLFunctions.cpp b/library/src/main/jni/cge/common/cgeGLFunctions.cpp index fd4a53fa..094217a4 100644 --- a/library/src/main/jni/cge/common/cgeGLFunctions.cpp +++ b/library/src/main/jni/cge/common/cgeGLFunctions.cpp @@ -268,12 +268,22 @@ void TextureObject::cleanup(bool deleteTexture) bool TextureObject::resize(int w, int h, const void* buffer, GLenum format) { - if (m_texture == 0 || m_size.width != w || m_size.height != h || buffer != nullptr) + if (m_texture != 0 && m_size.width == w && m_size.height == h && buffer == nullptr) { - if (w == 0 || h == 0) + return false; + } + + if (w == 0 || h == 0) + { + assert(0 && (void*)"TextureObject::resize must not be 0!"); + return false; + } + + if (m_texture == 0 || m_size.width != w || m_size.height != h) + { + if (m_texture != 0) { - assert(0 && "TextureObject::resize must not be 0!"); - return false; + glDeleteTextures(1, &m_texture); } int channel; @@ -297,29 +307,17 @@ bool TextureObject::resize(int w, int h, const void* buffer, GLenum format) break; } - if (m_texture == 0) - { - m_texture = cgeGenTextureWithBuffer(buffer, w, h, format, GL_UNSIGNED_BYTE, channel); - m_size.set(w, h); - } - else - { - glBindTexture(GL_TEXTURE_2D, m_texture); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - if (m_size.width != w || m_size.height != h) - { - m_size.set(w, h); - glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, buffer); - } - else - { - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, buffer); - } - } - - return true; + m_size.set(w, h); + m_texture = cgeGenTextureWithBuffer(buffer, w, h, format, GL_UNSIGNED_BYTE, channel); } - return false; + else + { + glBindTexture(GL_TEXTURE_2D, m_texture); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, buffer); + } + + return true; } ////////////// diff --git a/library/src/main/jni/cge/common/cgeGLFunctions.h b/library/src/main/jni/cge/common/cgeGLFunctions.h index f317cdcd..4515e911 100644 --- a/library/src/main/jni/cge/common/cgeGLFunctions.h +++ b/library/src/main/jni/cge/common/cgeGLFunctions.h @@ -50,8 +50,8 @@ void cgeDisableGlobalGLContext(); typedef void* (*CGEBufferLoadFun)(const char* sourceName, void** bufferData, GLint* w, GLint* h, CGEBufferFormat* fmt, void* arg); typedef bool (*CGEBufferUnloadFun)(void* arg1, void* arg2); -//加载纹理回调, 注, 为了保持接口简洁性, 回调返回的纹理单元将由调用者负责释放 -//返回的纹理不应该为 glDeleteTextures 无法处理的特殊纹理类型. +// 加载纹理回调, 注, 为了保持接口简洁性, 回调返回的纹理单元将由调用者负责释放 +// 返回的纹理不应该为 glDeleteTextures 无法处理的特殊纹理类型. typedef GLuint (*CGETextureLoadFun)(const char* sourceName, GLint* w, GLint* h, void* arg); // You can set a common function for loading textures diff --git a/library/src/main/jni/cge/common/cgeShaderFunctions.h b/library/src/main/jni/cge/common/cgeShaderFunctions.h index 038dd201..4a8510ab 100644 --- a/library/src/main/jni/cge/common/cgeShaderFunctions.h +++ b/library/src/main/jni/cge/common/cgeShaderFunctions.h @@ -224,9 +224,10 @@ class ProgramObject inline GLint _getUniform(GLuint programId, const char* name) { GLint uniform = glGetUniformLocation(programId, name); - CGE_LOG_CODE( - if (uniform < 0) - CGE_LOG_ERROR("uniform name %s does not exist!\n", name);); +#ifdef DEBUG + if (uniform < 0) + CGE_LOG_ERROR("uniform name %s does not exist!\n", name); +#endif return uniform; } diff --git a/library/src/main/jni/interface/cgeVideoPlayer.cpp b/library/src/main/jni/interface/cgeVideoPlayer.cpp index 3319ea34..80f144b6 100644 --- a/library/src/main/jni/interface/cgeVideoPlayer.cpp +++ b/library/src/main/jni/interface/cgeVideoPlayer.cpp @@ -69,8 +69,7 @@ static const GLfloat s_colorConversion709[] = { namespace CGE { -CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P() : - m_posAttribLocation(0), m_decodeHandler(nullptr), m_vertexBuffer(0) +CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P() { m_program.bindAttribLocation(CGEImageFilterInterface::paramPositionIndexName, m_posAttribLocation); @@ -89,16 +88,16 @@ CGEVideoPlayerYUV420P::CGEVideoPlayerYUV420P() : m_texULoc = m_program.uniformLocation("textureU"); m_texVLoc = m_program.uniformLocation("textureV"); - glUniform1i(m_texYLoc, 1); - glUniform1i(m_texULoc, 2); - glUniform1i(m_texVLoc, 3); - if (m_texYLoc < 0 || m_texULoc < 0 || m_texVLoc < 0) { CGE_LOG_ERROR("Invalid YUV Texture Uniforms\n"); } - - memset(m_texYUV, 0, sizeof(m_texYUV)); + else + { + glUniform1i(m_texYLoc, 1); + glUniform1i(m_texULoc, 2); + glUniform1i(m_texVLoc, 3); + } m_rotLoc = m_program.uniformLocation("rotation"); m_flipScaleLoc = m_program.uniformLocation("flipScale"); @@ -143,12 +142,6 @@ bool CGEVideoPlayerYUV420P::initWithDecodeHandler(CGEVideoDecodeHandler* handler m_linesize[2] = m_linesize[1] = m_linesize[0] / 2; m_videoHeight = m_decodeHandler->getHeight(); - m_texYUV[0] = cgeGenTextureWithBuffer(nullptr, m_linesize[0], m_videoHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 1); - - m_texYUV[1] = cgeGenTextureWithBuffer(nullptr, m_linesize[1], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 2); - - m_texYUV[2] = cgeGenTextureWithBuffer(nullptr, m_linesize[2], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 3); - if (m_vertexBuffer == 0) m_vertexBuffer = cgeGenCommonQuadArrayBuffer(); @@ -245,23 +238,18 @@ bool CGEVideoPlayerYUV420P::updateVideoFrame(const CGEVideoFrameBufferData* data m_program.bind(); - if (m_linesize[0] != framebuffer.linesize[0]) + if (m_texYUV[0] == 0 || m_linesize[0] != framebuffer.linesize[0]) { m_linesize[0] = framebuffer.linesize[0]; m_linesize[1] = framebuffer.linesize[1]; m_linesize[2] = framebuffer.linesize[2]; - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, m_texYUV[0]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[0], m_videoHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[0]); - - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, m_texYUV[1]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[1], m_videoHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[1]); + if (m_texYUV[0] != 0) + glDeleteTextures(3, m_texYUV); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, m_texYUV[2]); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, m_linesize[2], m_videoHeight / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, framebuffer.data[2]); + m_texYUV[0] = cgeGenTextureWithBuffer(framebuffer.data[0], m_linesize[0], m_videoHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 1); + m_texYUV[1] = cgeGenTextureWithBuffer(framebuffer.data[1], m_linesize[1], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 2); + m_texYUV[2] = cgeGenTextureWithBuffer(framebuffer.data[2], m_linesize[2], m_videoHeight / 2, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1, 3); } else { diff --git a/library/src/main/jni/interface/cgeVideoPlayer.h b/library/src/main/jni/interface/cgeVideoPlayer.h index 9a8c20ef..a9cd6cb3 100644 --- a/library/src/main/jni/interface/cgeVideoPlayer.h +++ b/library/src/main/jni/interface/cgeVideoPlayer.h @@ -48,7 +48,7 @@ class CGEVideoPlayerYUV420P : public CGEVideoPlayerInterface bool open(const char* filename, CGEVideoDecodeHandler::SamplingStyle s = CGEVideoDecodeHandler::ssFastBilinear); - //不同的初始化方式,不与 open 合用 + // 不同的初始化方式,不与 open 合用 bool initWithDecodeHandler(CGEVideoDecodeHandler*); void close(); @@ -81,15 +81,15 @@ class CGEVideoPlayerYUV420P : public CGEVideoPlayerInterface protected: ProgramObject m_program; - GLuint m_texYUV[3]; - GLint m_texYLoc, m_texULoc, m_texVLoc; - GLuint m_posAttribLocation; - GLuint m_rotLoc, m_flipScaleLoc; - CGEVideoDecodeHandler* m_decodeHandler; - - GLuint m_vertexBuffer; - int m_videoWidth, m_videoHeight; - int m_linesize[3]; + GLuint m_texYUV[3]{}; + GLint m_texYLoc = -1, m_texULoc = -1, m_texVLoc = -1; + GLuint m_posAttribLocation{}; + GLuint m_rotLoc{}, m_flipScaleLoc{}; + CGEVideoDecodeHandler* m_decodeHandler{}; + + GLuint m_vertexBuffer{}; + int m_videoWidth{}, m_videoHeight{}; + int m_linesize[3]{}; }; } // namespace CGE