[vlc-devel] [RFC PATCH 4/4] opengl: Split texture uploading to a separate function

Martin Storsjö martin at martin.st
Tue Mar 12 19:02:09 CET 2013


This should take care of repacking of data if we can't pass the
alignment to opengl.
---

It still doesn't make subtitle overlays look sensible for me on the
pandaboard, though.
---
 modules/video_output/opengl.c |  108 +++++++++++++++++++++++------------------
 1 file changed, 62 insertions(+), 46 deletions(-)

diff --git a/modules/video_output/opengl.c b/modules/video_output/opengl.c
index 2dc1e9c..6021b34 100644
--- a/modules/video_output/opengl.c
+++ b/modules/video_output/opengl.c
@@ -692,6 +692,58 @@ error:
     return NULL;
 }
 
+static void upload(int width, int height, int full_width, int full_height,
+                   int w_num, int w_den, int h_num, int h_den,
+                   int pitch, int pixel_pitch, int new_tex,
+                   const uint8_t *pixels, int tex_target,
+                   int tex_format, int tex_type)
+{
+#ifndef GL_UNPACK_ROW_LENGTH
+    if ( (pitch / pixel_pitch) != (unsigned int)
+         ( width * w_num / w_den ) )
+    {
+        uint8_t *new_plane = malloc( width * height * w_num * h_num / (h_den * w_den ) );
+        uint8_t *destination = new_plane;
+        const uint8_t *source = pixels;
+
+        for( unsigned height = 0; height < (height * h_num / h_den) ; height++ )
+        {
+            memcpy( destination, source, width * w_num / w_den );
+            source += pitch / pixel_pitch;
+            destination += width * w_num / w_den;
+        }
+        if (new_tex)
+            glTexImage2D( tex_target, 0, tex_format,
+                          width  * w_num / w_den,
+                          height * h_num / h_den,
+                          0, tex_format, tex_type, new_plane );
+        else
+            glTexSubImage2D( tex_target, 0,
+                             0, 0,
+                             width  * w_num / w_den,
+                             height * h_num / h_den,
+                             tex_format, tex_type, new_plane );
+        free( new_plane );
+    } else {
+#else
+        glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
+#endif
+        if (new_tex)
+            glTexImage2D(tex_target, 0, tex_format,
+                         full_width  * w_num / w_den,
+                         full_height * h_num / h_den,
+                         0, tex_format, tex_type, pixels);
+        else
+            glTexSubImage2D(tex_target, 0,
+                            0, 0,
+                            full_width  * w_num / w_den,
+                            full_height * h_num / h_den,
+                            tex_format, tex_type, pixels);
+#ifndef GL_UNPACK_ROW_LENGTH
+    }
+#endif
+}
+
 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
                                 picture_t *picture, subpicture_t *subpicture)
 {
@@ -706,38 +758,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
         }
         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
 
-#ifndef GL_UNPACK_ROW_LENGTH
-        if ( (picture->p[j].i_pitch / picture->p[j].i_pixel_pitch) != (unsigned int)
-             ( picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den ) )
-        {
-            uint8_t *new_plane = malloc( picture->format.i_visible_width * vgl->fmt.i_visible_height * vgl->chroma->p[j].w.num * vgl->chroma->p[j].h.num / (vgl->chroma->p[j].h.den * vgl->chroma->p[j].w.den ) );
-            uint8_t *destination = new_plane;
-            const uint8_t *source = picture->p[j].p_pixels;
-
-            for( unsigned height = 0; height < (vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den) ; height++ )
-            {
-                memcpy( destination, source, picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den );
-                source += picture->p[j].i_pitch / picture->p[j].i_pixel_pitch;
-                destination += picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
-            }
-            glTexSubImage2D( vgl->tex_target, 0,
-                             0, 0,
-                             picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
-                             vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
-                             vgl->tex_format, vgl->tex_type, new_plane );
-            free( new_plane );
-        } else {
-#else
-            glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch);
-#endif
-            glTexSubImage2D(vgl->tex_target, 0,
-                            0, 0,
-                            vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
-                            vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
-                            vgl->tex_format, vgl->tex_type, picture->p[j].p_pixels);
-#ifndef GL_UNPACK_ROW_LENGTH
-        }
-#endif
+        upload(picture->format.i_visible_width, picture->format.i_visible_height,
+               picture->format.i_width, picture->format.i_height,
+               vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
+               picture->p[j].i_pitch, picture->p[j].i_pixel_pitch, 0, picture->p[j].p_pixels, vgl->tex_target, vgl->tex_format, vgl->tex_type);
     }
 
     int         last_count = vgl->region_count;
@@ -794,13 +818,9 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
             if (glr->texture) {
                 glBindTexture(GL_TEXTURE_2D, glr->texture);
-                /* TODO set GL_UNPACK_ALIGNMENT */
-#ifdef GL_UNPACK_ROW_LENGTH
-                glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
-#endif
-                glTexSubImage2D(GL_TEXTURE_2D, 0,
-                                0, 0, glr->width, glr->height,
-                                glr->format, glr->type, &r->p_picture->p->p_pixels[pixels_offset]);
+                upload(glr->width, glr->height, glr->width, glr->height, 1, 1, 1, 1,
+                       r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
+                       &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
             } else {
                 glGenTextures(1, &glr->texture);
                 glBindTexture(GL_TEXTURE_2D, glr->texture);
@@ -812,13 +832,9 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-                /* TODO set GL_UNPACK_ALIGNMENT */
-#ifdef GL_UNPACK_ROW_LENGTH
-                glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
-#endif
-                glTexImage2D(GL_TEXTURE_2D, 0, glr->format,
-                             glr->width, glr->height, 0, glr->format, glr->type,
-                             &r->p_picture->p->p_pixels[pixels_offset]);
+                upload(glr->width, glr->height, glr->width, glr->height, 1, 1, 1, 1,
+                       r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
+                       &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
             }
         }
     }
-- 
1.7.10.4




More information about the vlc-devel mailing list