[vlc-commits] opengl: converter: refactor pf_gen_textures

Thomas Guillem git at videolan.org
Thu Feb 2 09:52:48 CET 2017


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Jan 31 15:46:23 2017 +0100| [7f80b1a4ba9c5462a339d3011d14dc2d5e39a287] | committer: Thomas Guillem

opengl: converter: refactor pf_gen_textures

This function doesn't mean to be handled differently by converters, we can put
the code back in the vout_helper. Add pf_allocate_texture that can be overridden
to allocate data for a texture.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=7f80b1a4ba9c5462a339d3011d14dc2d5e39a287
---

 modules/video_output/opengl/converter_android.c | 30 ------------
 modules/video_output/opengl/converters.c        | 43 +++--------------
 modules/video_output/opengl/internal.h          | 33 +++++--------
 modules/video_output/opengl/vout_helper.c       | 63 +++++++++++++++++++++----
 4 files changed, 73 insertions(+), 96 deletions(-)

diff --git a/modules/video_output/opengl/converter_android.c b/modules/video_output/opengl/converter_android.c
index 405a7b5..0f034c4 100644
--- a/modules/video_output/opengl/converter_android.c
+++ b/modules/video_output/opengl/converter_android.c
@@ -42,34 +42,6 @@ struct priv
 };
 
 static int
-tc_anop_gen_textures(const opengl_tex_converter_t *tc,
-                     const GLsizei *tex_width, const GLsizei *tex_height,
-                     GLuint *textures)
-{
-    (void) tex_width; (void) tex_height;
-
-    glActiveTexture(GL_TEXTURE0);
-    glClientActiveTexture(GL_TEXTURE0);
-
-    glGenTextures(1, textures);
-    glBindTexture(tc->tex_target, textures[0]);
-
-    glTexParameteri(tc->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-    glTexParameteri(tc->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-    glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-    glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-    return VLC_SUCCESS;
-}
-
-static void
-tc_anop_del_textures(const opengl_tex_converter_t *tc, GLuint *textures)
-{
-    (void) tc;
-    glDeleteTextures(1, textures);
-    textures[0] = 0;
-}
-
-static int
 pool_lock_pic(picture_t *p_pic)
 {
     picture_sys_t *p_picsys = p_pic->p_sys;
@@ -226,8 +198,6 @@ opengl_tex_converter_anop_init(const video_format_t *fmt,
     priv->stex = NULL;
     priv->transform_mtx = NULL;
 
-    tc->pf_gen_textures   = tc_anop_gen_textures;
-    tc->pf_del_textures   = tc_anop_del_textures;
     tc->pf_get_pool       = tc_anop_get_pool;
     tc->pf_update         = tc_anop_update;
     tc->pf_fetch_locations = tc_anop_fetch_locations;
diff --git a/modules/video_output/opengl/converters.c b/modules/video_output/opengl/converters.c
index 0c96be9..286b759 100644
--- a/modules/video_output/opengl/converters.c
+++ b/modules/video_output/opengl/converters.c
@@ -317,46 +317,18 @@ error:
 #endif /* VLCGL_HAS_PBO */
 
 static int
-tc_common_gen_textures(const opengl_tex_converter_t *tc,
-                       const GLsizei *tex_width, const GLsizei *tex_height,
-                       GLuint *textures)
+tc_common_allocate_texture(const opengl_tex_converter_t *tc, GLuint texture,
+                           unsigned tex_idx, const GLsizei tex_width,
+                           const GLsizei tex_height)
 {
+    (void) texture; (void) tex_idx;
     struct priv *priv = tc->priv;
 
-    glGenTextures(tc->desc->plane_count, textures);
-
-    for (unsigned i = 0; i < tc->desc->plane_count; i++)
-    {
-        glActiveTexture(GL_TEXTURE0 + i);
-        glClientActiveTexture(GL_TEXTURE0 + i);
-        glBindTexture(tc->tex_target, textures[i]);
-
-#if !defined(USE_OPENGL_ES2)
-        /* Set the texture parameters */
-        glTexParameterf(tc->tex_target, GL_TEXTURE_PRIORITY, 1.0);
-        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-#endif
-
-        glTexParameteri(tc->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-        glTexParameteri(tc->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-        glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-        glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
-        /* Call glTexImage2D only once, and use glTexSubImage2D later */
-        glTexImage2D(tc->tex_target, 0, priv->tex_internal,
-                     tex_width[i], tex_height[i], 0, priv->tex_format,
-                     priv->tex_type, NULL);
-    }
+    glTexImage2D(tc->tex_target, 0, priv->tex_internal, tex_width, tex_height,
+                 0, priv->tex_format, priv->tex_type, NULL);
     return VLC_SUCCESS;
 }
 
-static void
-tc_common_del_textures(const opengl_tex_converter_t *tc, GLuint *textures)
-{
-    glDeleteTextures(tc->desc->plane_count, textures);
-    memset(textures, 0, tc->desc->plane_count * sizeof(GLuint));
-}
-
 static int
 upload_plane(const opengl_tex_converter_t *tc,
              unsigned width, unsigned height,
@@ -474,10 +446,9 @@ common_init(opengl_tex_converter_t *tc, size_t priv_size, vlc_fourcc_t chroma,
     tc->desc    = vlc_fourcc_GetChromaDescription(chroma);
     assert(tc->desc != NULL);
 
-    tc->pf_gen_textures = tc_common_gen_textures;
-    tc->pf_del_textures = tc_common_del_textures;
     tc->pf_update       = tc_common_update;
     tc->pf_release      = tc_common_release;
+    tc->pf_allocate_texture = tc_common_allocate_texture;
 
 #ifdef VLCGL_HAS_PBO
     const bool supports_pbo = tc->api->BufferStorage
diff --git a/modules/video_output/opengl/internal.h b/modules/video_output/opengl/internal.h
index 22ee015..7cb6e27 100644
--- a/modules/video_output/opengl/internal.h
+++ b/modules/video_output/opengl/internal.h
@@ -180,31 +180,22 @@ struct opengl_tex_converter_t
     void *priv;
 
     /*
-     * Callback to generate and prepare textures
+     * Callback to allocate data for a bound texture
      *
-     * This function pointer cannot be NULL. The number of textures to generate
-     * is specified by desc->plane_count.
+     * This function pointer can be NULL. Software converters should call
+     * glTexImage2D() to allocate texture data (it will be deallocated by the
+     * caller when calling glDeleteTextures()).
      *
      * \param fc OpenGL tex converter
-     * \param tex_width array of tex width (one per plane)
-     * \param tex_height array of tex height (one per plane)
-     * \param textures array of textures to generate (one per plane)
+     * \param texture bound texture
+     * \param tex_idx texture index
+     * \param tex_width texture width
+     * \param tex_height texture height
      * \return VLC_SUCCESS or a VLC error
      */
-    int (*pf_gen_textures)(const opengl_tex_converter_t *fc,
-                           const GLsizei *tex_width, const GLsizei *tex_height,
-                           GLuint *textures);
-
-    /*
-     * Callback to delete textures generated by pf_gen_textures()
-     *
-     * This function pointer cannot be NULL.
-     *
-     * \param fc OpenGL tex converter
-     * \param textures array of textures to delete (one per plane)
-     */
-    void (*pf_del_textures)(const opengl_tex_converter_t *fc,
-                            GLuint *textures);
+    int (*pf_allocate_texture)(const opengl_tex_converter_t *fc, GLuint texture,
+                               unsigned tex_idx, const GLsizei tex_width,
+                               const GLsizei tex_height);
 
     /*
      * Callback to allocate a picture pool
@@ -215,7 +206,7 @@ struct opengl_tex_converter_t
      * \param fc OpenGL tex converter
      * \param fmt video format
      * \param requested_count number of pictures to allocate
-     * \param textures textures generated by pf_gen_textures()
+     * \param textures textures generated (and allocated by pf_allocate_texture)
      * \return the picture pool or NULL in case of error
      */
     picture_pool_t *(*pf_get_pool)(const opengl_tex_converter_t *fc,
diff --git a/modules/video_output/opengl/vout_helper.c b/modules/video_output/opengl/vout_helper.c
index aaecb1e..78648a1 100644
--- a/modules/video_output/opengl/vout_helper.c
+++ b/modules/video_output/opengl/vout_helper.c
@@ -385,6 +385,54 @@ static GLuint BuildVertexShader(vout_display_opengl_t *vgl, unsigned plane_count
     return shader;
 }
 
+static int
+GenTextures(const opengl_tex_converter_t *tc,
+            const GLsizei *tex_width, const GLsizei *tex_height,
+            GLuint *textures)
+{
+    glGenTextures(tc->desc->plane_count, textures);
+
+    for (unsigned i = 0; i < tc->desc->plane_count; i++)
+    {
+        glActiveTexture(GL_TEXTURE0 + i);
+        glClientActiveTexture(GL_TEXTURE0 + i);
+        glBindTexture(tc->tex_target, textures[i]);
+
+#if !defined(USE_OPENGL_ES2)
+        /* Set the texture parameters */
+        glTexParameterf(tc->tex_target, GL_TEXTURE_PRIORITY, 1.0);
+        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+#endif
+
+        glTexParameteri(tc->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(tc->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+        glTexParameteri(tc->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+        if (tc->pf_allocate_texture != NULL)
+        {
+            int ret = tc->pf_allocate_texture(tc, textures[i], i, tex_width[i],
+                                              tex_height[i]);
+            if (ret != VLC_SUCCESS)
+            {
+                if (i > 0)
+                {
+                    glDeleteTextures(i, textures);
+                    memset(textures, 0, tc->desc->plane_count * sizeof(GLuint));
+                }
+                return ret;
+            }
+        }
+    }
+    return VLC_SUCCESS;
+}
+
+static void
+DelTextures(const opengl_tex_converter_t *tc, GLuint *textures)
+{
+    glDeleteTextures(tc->desc->plane_count, textures);
+    memset(textures, 0, tc->desc->plane_count * sizeof(GLuint));
+}
+
 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
                                                const vlc_fourcc_t **subpicture_chromas,
                                                vlc_gl_t *gl,
@@ -532,7 +580,6 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
             {
                 assert(tex_conv.chroma != 0 && tex_conv.tex_target != 0 &&
                        tex_conv.fragment_shader != 0 &&
-                       tex_conv.pf_gen_textures != NULL &&
                        tex_conv.pf_update != NULL &&
                        tex_conv.pf_fetch_locations != NULL &&
                        tex_conv.pf_prepare_shader != NULL &&
@@ -759,13 +806,13 @@ void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
     glFlush();
 
     opengl_tex_converter_t *tc = &vgl->prgm->tc;
-    tc->pf_del_textures(tc, vgl->texture);
+    DelTextures(tc, vgl->texture);
 
     tc = &vgl->sub_prgm->tc;
     for (int i = 0; i < vgl->region_count; i++)
     {
         if (vgl->region[i].texture)
-            tc->pf_del_textures(tc, &vgl->region[i].texture);
+            DelTextures(tc, &vgl->region[i].texture);
     }
     free(vgl->region);
 
@@ -864,8 +911,7 @@ picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned
 
     /* Allocates our textures */
     opengl_tex_converter_t *tc = &vgl->prgm->tc;
-    int ret = tc->pf_gen_textures(tc, vgl->tex_width, vgl->tex_height,
-                                  vgl->texture);
+    int ret = GenTextures(tc, vgl->tex_width, vgl->tex_height, vgl->texture);
     if (ret != VLC_SUCCESS)
         return NULL;
 
@@ -904,7 +950,7 @@ picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned
     return vgl->pool;
 
 error:
-    tc->pf_del_textures(tc, vgl->texture);
+    DelTextures(tc, vgl->texture);
     return NULL;
 }
 
@@ -978,8 +1024,7 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
             {
                 /* Could not recycle a previous texture, generate a new one. */
                 GLsizei tex_width = glr->width, tex_height = glr->height;
-                ret = tc->pf_gen_textures(tc, &tex_width, &tex_height,
-                                          &glr->texture);
+                ret = GenTextures(tc, &tex_width, &tex_height, &glr->texture);
                 if (ret != VLC_SUCCESS)
                     continue;
             }
@@ -990,7 +1035,7 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
     }
     for (int i = 0; i < last_count; i++) {
         if (last[i].texture)
-            tc->pf_del_textures(tc, &last[i].texture);
+            DelTextures(tc, &last[i].texture);
     }
     free(last);
 



More information about the vlc-commits mailing list