]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
Clean up.
[vlc] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL and OpenGL ES output common code
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * Copyright (C) 2009 Laurent Aimar
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Eric Petit <titer@m0k.org>
10  *          Cedric Cocquebert <cedric.cocquebert@supelec.fr>
11  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_picture_pool.h>
33 #include <vlc_subpicture.h>
34 #include <vlc_opengl.h>
35
36 #include "opengl.h"
37 // Define USE_OPENGL_ES to the GL ES Version you want to select
38
39 #if !defined (__APPLE__)
40 # if USE_OPENGL_ES == 2
41 #  include <GLES2/gl2ext.h>
42 # elif USE_OPENGL_ES == 1
43 #  include <GLES/glext.h>
44 # else
45 #   include <GL/glext.h>
46 # endif
47 #else
48 # if USE_OPENGL_ES == 2
49 #  include <OpenGLES/ES2/gl.h>
50 # elif USE_OPENGL_ES == 1
51 #  include <OpenGLES/ES1/gl.h>
52 # else
53 #  define MACOS_OPENGL
54 #  include <OpenGL/glext.h>
55 # endif
56 #endif
57
58 /* RV16 */
59 #ifndef GL_UNSIGNED_SHORT_5_6_5
60 # define GL_UNSIGNED_SHORT_5_6_5 0x8363
61 #endif
62 #ifndef GL_CLAMP_TO_EDGE
63 # define GL_CLAMP_TO_EDGE 0x812F
64 #endif
65
66 #if USE_OPENGL_ES
67 #   define VLCGL_TEXTURE_COUNT 1
68 #   define VLCGL_PICTURE_MAX 1
69 #elif defined(MACOS_OPENGL)
70 #   define VLCGL_TEXTURE_COUNT 2
71 #   define VLCGL_PICTURE_MAX 2
72 #else
73 #   define VLCGL_TEXTURE_COUNT 1
74 #   define VLCGL_PICTURE_MAX 128
75 #endif
76
77 static const vlc_fourcc_t gl_subpicture_chromas[] = {
78     VLC_CODEC_RGBA,
79     0
80 };
81
82 typedef struct {
83     GLuint   texture;
84     unsigned format;
85     unsigned type;
86     unsigned width;
87     unsigned height;
88
89     float    alpha;
90
91     float    top;
92     float    left;
93     float    bottom;
94     float    right;
95 } gl_region_t;
96
97 struct vout_display_opengl_t {
98     vlc_gl_t   *gl;
99
100     video_format_t fmt;
101     const vlc_chroma_description_t *chroma;
102
103     int        tex_target;
104     int        tex_format;
105     int        tex_type;
106
107     int        tex_width[PICTURE_PLANE_MAX];
108     int        tex_height[PICTURE_PLANE_MAX];
109
110     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
111
112     int         region_count;
113     gl_region_t *region;
114
115
116     picture_pool_t *pool;
117
118     GLuint     program;
119     int        local_count;
120     GLfloat    local_value[16][4];
121
122     /* fragment_program */
123     void (*GenProgramsARB)(GLsizei, GLuint *);
124     void (*BindProgramARB)(GLenum, GLuint);
125     void (*ProgramStringARB)(GLenum, GLenum, GLsizei, const GLvoid *);
126     void (*DeleteProgramsARB)(GLsizei, const GLuint *);
127     void (*ProgramLocalParameter4fvARB)(GLenum, GLuint, const GLfloat *);
128
129     /* multitexture */
130     void (*ActiveTextureARB)(GLenum);
131     void (*MultiTexCoord2fARB)(GLenum, GLfloat, GLfloat);
132 };
133
134 static inline int GetAlignedSize(unsigned size)
135 {
136     /* Return the smallest larger or equal power of 2 */
137     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
138     return ((align >> 1) == size) ? size : align;
139 }
140
141 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
142                                                const vlc_fourcc_t **subpicture_chromas,
143                                                vlc_gl_t *gl)
144 {
145     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
146     if (!vgl)
147         return NULL;
148
149     vgl->gl = gl;
150     if (vlc_gl_Lock(vgl->gl)) {
151         free(vgl);
152         return NULL;
153     }
154
155     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
156     if (!extensions)
157         extensions = "";
158
159     /* Load extensions */
160     bool supports_fp = false;
161     if (strstr(extensions, "GL_ARB_fragment_program")) {
162         vgl->GenProgramsARB    = (void (*)(GLsizei, GLuint *))vlc_gl_GetProcAddress(vgl->gl, "glGenProgramsARB");
163         vgl->BindProgramARB    = (void (*)(GLenum, GLuint))vlc_gl_GetProcAddress(vgl->gl, "glBindProgramARB");
164         vgl->ProgramStringARB  = (void (*)(GLenum, GLenum, GLsizei, const GLvoid *))vlc_gl_GetProcAddress(vgl->gl, "glProgramStringARB");
165         vgl->DeleteProgramsARB = (void (*)(GLsizei, const GLuint *))vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgramsARB");
166         vgl->ProgramLocalParameter4fvARB = (void (*)(GLenum, GLuint, const GLfloat *))vlc_gl_GetProcAddress(vgl->gl, "glProgramLocalParameter4fvARB");
167
168         supports_fp = vgl->GenProgramsARB &&
169                       vgl->BindProgramARB &&
170                       vgl->ProgramStringARB &&
171                       vgl->DeleteProgramsARB &&
172                       vgl->ProgramLocalParameter4fvARB;
173     }
174     bool supports_multitexture = false;
175     if (strstr(extensions, "GL_ARB_multitexture")) {
176         vgl->ActiveTextureARB   = (void (*)(GLenum))vlc_gl_GetProcAddress(vgl->gl, "glActiveTextureARB");
177         vgl->MultiTexCoord2fARB = (void (*)(GLenum, GLfloat, GLfloat))vlc_gl_GetProcAddress(vgl->gl, "glMultiTexCoord2fARB");
178
179         supports_multitexture = vgl->ActiveTextureARB &&
180                                 vgl->MultiTexCoord2fARB;
181     }
182
183     /* Initialize with default chroma */
184     vgl->fmt = *fmt;
185 #if USE_OPENGL_ES
186     vgl->fmt.i_chroma = VLC_CODEC_RGB16;
187 #   if defined(WORDS_BIGENDIAN)
188     vgl->fmt.i_rmask  = 0x001f;
189     vgl->fmt.i_gmask  = 0x07e0;
190     vgl->fmt.i_bmask  = 0xf800;
191 #   else
192     vgl->fmt.i_rmask  = 0xf800;
193     vgl->fmt.i_gmask  = 0x07e0;
194     vgl->fmt.i_bmask  = 0x001f;
195 #   endif
196     vgl->tex_target   = GL_TEXTURE_2D;
197     vgl->tex_format   = GL_RGB;
198     vgl->tex_type     = GL_UNSIGNED_SHORT_5_6_5;
199 #elif defined(MACOS_OPENGL)
200 #   if defined(WORDS_BIGENDIAN)
201     vgl->fmt.i_chroma = VLC_CODEC_YUYV;
202 #   else
203     vgl->fmt.i_chroma = VLC_CODEC_UYVY;
204 #   endif
205     vgl->tex_target   = GL_TEXTURE_RECTANGLE_EXT;
206     vgl->tex_format   = GL_YCBCR_422_APPLE;
207     vgl->tex_type     = GL_UNSIGNED_SHORT_8_8_APPLE;
208 #else
209     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
210 #   if defined(WORDS_BIGENDIAN)
211     vgl->fmt.i_rmask  = 0xff000000;
212     vgl->fmt.i_gmask  = 0x00ff0000;
213     vgl->fmt.i_bmask  = 0x0000ff00;
214 #   else
215     vgl->fmt.i_rmask  = 0x000000ff;
216     vgl->fmt.i_gmask  = 0x0000ff00;
217     vgl->fmt.i_bmask  = 0x00ff0000;
218 #   endif
219     vgl->tex_target   = GL_TEXTURE_2D;
220     vgl->tex_format   = GL_RGBA;
221     vgl->tex_type     = GL_UNSIGNED_BYTE;
222 #endif
223     /* Use YUV if possible and needed */
224     bool need_fs_yuv = false;
225     if (supports_fp && supports_multitexture &&
226         vlc_fourcc_IsYUV(fmt->i_chroma) && !vlc_fourcc_IsYUV(vgl->fmt.i_chroma)) {
227         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
228         while (*list) {
229             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
230             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
231                 need_fs_yuv       = true;
232                 vgl->fmt          = *fmt;
233                 vgl->fmt.i_chroma = *list;
234                 vgl->tex_format   = GL_LUMINANCE;
235                 vgl->tex_type     = GL_UNSIGNED_BYTE;
236                 break;
237             }
238             list++;
239         }
240     }
241
242     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
243
244     bool supports_npot = false;
245 #if USE_OPENGL_ES == 2
246     supports_npot = true;
247 #elif defined(MACOS_OPENGL)
248     supports_npot = true;
249 #else
250     supports_npot |= strstr(extensions, "GL_APPLE_texture_2D_limited_npot") != NULL ||
251                      strstr(extensions, "GL_ARB_texture_non_power_of_two");
252 #endif
253
254     /* Texture size */
255     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
256         int w = vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
257         int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
258         if (supports_npot) {
259             vgl->tex_width[j]  = w;
260             vgl->tex_height[j] = h;
261         }
262         else {
263             /* A texture must have a size aligned on a power of 2 */
264             vgl->tex_width[j]  = GetAlignedSize(w);
265             vgl->tex_height[j] = GetAlignedSize(h);
266         }
267     }
268
269     /* Build fragment program if needed */
270     vgl->program = 0;
271     vgl->local_count = 0;
272     if (supports_fp) {
273         char *code = NULL;
274
275         if (need_fs_yuv) {
276             /* [R/G/B][Y U V O] from TV range to full range
277              * XXX we could also do hue/brightness/constrast/gamma
278              * by simply changing the coefficients
279              */
280             const float matrix_bt601_tv2full[3][4] = {
281                 { 1.1640,  0.0000,  1.4030, -0.7773 },
282                 { 1.1640, -0.3440, -0.7140,  0.4580 },
283                 { 1.1640,  1.7730,  0.0000, -0.9630 },
284             };
285             const float matrix_bt709_tv2full[3][4] = {
286                 { 1.1640,  0.0000,  1.5701, -0.8612 },
287                 { 1.1640, -0.1870, -0.4664,  0.2549 },
288                 { 1.1640,  1.8556,  0.0000, -1.0045 },
289             };
290             const float (*matrix)[4] = fmt->i_height > 576 ? matrix_bt709_tv2full
291                                                            : matrix_bt601_tv2full;
292
293             /* Basic linear YUV -> RGB conversion using bilinear interpolation */
294             const char *template_yuv =
295                 "!!ARBfp1.0"
296                 "OPTION ARB_precision_hint_fastest;"
297
298                 "TEMP src;"
299                 "TEX src.x,  fragment.texcoord[0], texture[0], 2D;"
300                 "TEX src.%c, fragment.texcoord[1], texture[1], 2D;"
301                 "TEX src.%c, fragment.texcoord[2], texture[2], 2D;"
302
303                 "PARAM coefficient[4] = { program.local[0..3] };"
304
305                 "TEMP tmp;"
306                 "MAD  tmp.rgb,          src.xxxx, coefficient[0], coefficient[3];"
307                 "MAD  tmp.rgb,          src.yyyy, coefficient[1], tmp;"
308                 "MAD  result.color.rgb, src.zzzz, coefficient[2], tmp;"
309                 "END";
310             bool swap_uv = vgl->fmt.i_chroma == VLC_CODEC_YV12 ||
311                            vgl->fmt.i_chroma == VLC_CODEC_YV9;
312             if (asprintf(&code, template_yuv,
313                          swap_uv ? 'z' : 'y',
314                          swap_uv ? 'y' : 'z') < 0)
315                 code = NULL;
316
317             for (int i = 0; i < 4; i++)
318                 for (int j = 0; j < 4; j++)
319                     vgl->local_value[vgl->local_count + i][j] = j < 3 ? matrix[j][i] : 0.0;
320             vgl->local_count += 4;
321         }
322         if (code) {
323             vgl->GenProgramsARB(1, &vgl->program);
324             vgl->BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, vgl->program);
325             vgl->ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
326                                   GL_PROGRAM_FORMAT_ASCII_ARB,
327                                   strlen(code), (const GLbyte*)code);
328             if (glGetError() == GL_INVALID_OPERATION) {
329                 /* FIXME if the program was needed for YUV, the video will be broken */
330 #if 0
331                 GLint position;
332                 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &position);
333
334                 const char *msg = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
335                 fprintf(stderr, "GL_INVALID_OPERATION: error at %d: %s\n", position, msg);
336 #endif
337                 vgl->DeleteProgramsARB(1, &vgl->program);
338                 vgl->program = 0;
339             }
340             free(code);
341         }
342     }
343
344     /* */
345     glDisable(GL_BLEND);
346     glDisable(GL_DEPTH_TEST);
347     glDepthMask(GL_FALSE);
348     glDisable(GL_CULL_FACE);
349     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
350     glClear(GL_COLOR_BUFFER_BIT);
351
352     vlc_gl_Unlock(vgl->gl);
353
354     /* */
355     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
356         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
357             vgl->texture[i][j] = 0;
358     }
359     vgl->region_count = 0;
360     vgl->region = NULL;
361     vgl->pool = NULL;
362
363     *fmt = vgl->fmt;
364     if (subpicture_chromas) {
365         *subpicture_chromas = NULL;
366 #if !defined(MACOS_OPENGL) && !USE_OPENGL_ES
367         if (supports_npot)
368             *subpicture_chromas = gl_subpicture_chromas;
369 #endif
370     }
371     return vgl;
372 }
373
374 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
375 {
376     /* */
377     if (!vlc_gl_Lock(vgl->gl)) {
378
379         glFinish();
380         glFlush();
381         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
382             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
383
384         if (vgl->program)
385             vgl->DeleteProgramsARB(1, &vgl->program);
386
387         vlc_gl_Unlock(vgl->gl);
388     }
389     if (vgl->pool)
390         picture_pool_Delete(vgl->pool);
391     free(vgl);
392 }
393
394 #ifdef MACOS_OPENGL
395 struct picture_sys_t {
396     vout_display_opengl_t *vgl;
397     GLuint *texture;
398 };
399
400 /* Small helper */
401 static inline GLuint PictureGetTexture(picture_t *picture)
402 {
403     return *picture->p_sys->texture;
404 }
405
406 static int PictureLock(picture_t *picture)
407 {
408     if (!picture->p_sys)
409         return VLC_SUCCESS;
410
411     vout_display_opengl_t *vgl = picture->p_sys->vgl;
412     if (!vlc_gl_Lock(vgl->gl)) {
413         glBindTexture(vgl->tex_target, PictureGetTexture(picture));
414         glTexSubImage2D(vgl->tex_target, 0,
415                         0, 0, vgl->fmt.i_width, vgl->fmt.i_height,
416                         vgl->tex_format, vgl->tex_type, picture->p[0].p_pixels);
417
418         vlc_gl_Unlock(vgl->gl);
419     }
420     return VLC_SUCCESS;
421 }
422
423 static void PictureUnlock(picture_t *picture)
424 {
425     VLC_UNUSED(picture);
426 }
427 #endif
428
429 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
430 {
431     if (vgl->pool)
432         return vgl->pool;
433
434     /* Allocate our pictures */
435     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
436     unsigned count = 0;
437
438     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
439         picture[count] = picture_NewFromFormat(&vgl->fmt);
440         if (!picture[count])
441             break;
442
443 #ifdef MACOS_OPENGL
444         picture_sys_t *sys = picture[count]->p_sys = malloc(sizeof(*sys));
445         if (sys) {
446             sys->vgl = vgl;
447             sys->texture = vgl->texture[count];
448         }
449 #endif
450     }
451     if (count <= 0)
452         return NULL;
453
454     /* Wrap the pictures into a pool */
455     picture_pool_configuration_t cfg;
456     memset(&cfg, 0, sizeof(cfg));
457     cfg.picture_count = count;
458     cfg.picture       = picture;
459 #ifdef MACOS_OPENGL
460     cfg.lock          = PictureLock;
461     cfg.unlock        = PictureUnlock;
462 #endif
463     vgl->pool = picture_pool_NewExtended(&cfg);
464     if (!vgl->pool)
465         goto error;
466
467     /* Allocates our textures */
468     if (vlc_gl_Lock(vgl->gl))
469         return vgl->pool;
470
471     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
472         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
473         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
474             if (vgl->chroma->plane_count > 1)
475                 vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
476             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
477
478 #if !USE_OPENGL_ES
479             /* Set the texture parameters */
480             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
481             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
482 #endif
483
484             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
485             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
486             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
487             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
488
489 #ifdef MACOS_OPENGL
490             /* Tell the driver not to make a copy of the texture but to use
491                our buffer */
492             glEnable(GL_UNPACK_CLIENT_STORAGE_APPLE);
493             glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
494
495             /* Use AGP texturing */
496             glTexParameteri(vgl->tex_target, GL_TEXTURE_STORAGE_HINT_APPLE,
497                              GL_STORAGE_SHARED_APPLE);
498 #endif
499
500             /* Call glTexImage2D only once, and use glTexSubImage2D later */
501             glTexImage2D(vgl->tex_target, 0,
502                          vgl->tex_format, vgl->tex_width[j], vgl->tex_height[j],
503                          0, vgl->tex_format, vgl->tex_type, NULL);
504         }
505     }
506
507     vlc_gl_Unlock(vgl->gl);
508
509     return vgl->pool;
510
511 error:
512     for (unsigned i = 0; i < count; i++)
513         picture_Delete(picture[i]);
514     return NULL;
515 }
516
517 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
518                                 picture_t *picture, subpicture_t *subpicture)
519 {
520     /* On Win32/GLX, we do this the usual way:
521        + Fill the buffer with new content,
522        + Reload the texture,
523        + Use the texture.
524
525        On OS X with VRAM or AGP texturing, the order has to be:
526        + Reload the texture,
527        + Fill the buffer with new content,
528        + Use the texture.
529
530        (Thanks to gcc from the Arstechnica forums for the tip)
531
532        Therefore on OSX, we have to use two buffers and textures and use a
533        lock(/unlock) managed picture pool.
534      */
535
536     if (vlc_gl_Lock(vgl->gl))
537         return VLC_EGENERIC;
538
539 #ifdef MACOS_OPENGL
540     /* Bind to the texture for drawing */
541     glBindTexture(vgl->tex_target, PictureGetTexture(picture));
542 #else
543     /* Update the texture */
544     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
545         if (vgl->chroma->plane_count > 1)
546             vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
547         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
548         glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch);
549         glTexSubImage2D(vgl->tex_target, 0,
550                         0, 0,
551                         vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
552                         vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
553                         vgl->tex_format, vgl->tex_type, picture->p[j].p_pixels);
554     }
555 #endif
556
557     int         last_count = vgl->region_count;
558     gl_region_t *last = vgl->region;
559
560     vgl->region_count = 0;
561     vgl->region       = NULL;
562
563     if (subpicture) {
564
565         int count = 0;
566         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
567             count++;
568
569         vgl->region_count = count;
570         vgl->region       = calloc(count, sizeof(*vgl->region));
571
572         if (vgl->chroma->plane_count > 1)
573             vgl->ActiveTextureARB(GL_TEXTURE0_ARB + 0);
574         int i = 0;
575         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
576             gl_region_t *glr = &vgl->region[i];
577
578             glr->format = GL_RGBA;
579             glr->type   = GL_UNSIGNED_BYTE;
580             glr->width  = r->fmt.i_visible_width;
581             glr->height = r->fmt.i_visible_height;
582             glr->alpha  = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
583             glr->left   =  2.0 * (r->i_x                          ) / subpicture->i_original_picture_width  - 1.0;
584             glr->top    = -2.0 * (r->i_y                          ) / subpicture->i_original_picture_height + 1.0;
585             glr->right  =  2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width  - 1.0;
586             glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
587
588             glr->texture = 0;
589             for (int j = 0; j < last_count; j++) {
590                 if (last[i].texture &&
591                     last[i].width  == glr->width &&
592                     last[i].height == glr->height &&
593                     last[i].format == glr->format &&
594                     last[i].type   == glr->type) {
595                     glr->texture = last[i].texture;
596                     memset(&last[i], 0, sizeof(last[i]));
597                     break;
598                 }
599             }
600
601             if (glr->texture) {
602                 glBindTexture(GL_TEXTURE_2D, glr->texture);
603                 /* TODO set GL_UNPACK_ALIGNMENT */
604                 glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
605                 glTexSubImage2D(GL_TEXTURE_2D, 0,
606                                 0, 0, glr->width, glr->height,
607                                 glr->format, glr->type, r->p_picture->p->p_pixels);
608             } else {
609                 glGenTextures(1, &glr->texture);
610                 glBindTexture(GL_TEXTURE_2D, glr->texture);
611                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
612                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
613                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
614                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
615                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
616                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
617                 /* TODO set GL_UNPACK_ALIGNMENT */
618                 glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
619                 glTexImage2D(GL_TEXTURE_2D, 0, glr->format,
620                              glr->width, glr->height, 0, glr->format, glr->type,
621                              r->p_picture->p->p_pixels);
622             }
623         }
624     }
625     for (int i = 0; i < last_count; i++) {
626         if (last[i].texture)
627             glDeleteTextures(1, &last[i].texture);
628     }
629     free(last);
630
631     vlc_gl_Unlock(vgl->gl);
632     VLC_UNUSED(subpicture);
633     return VLC_SUCCESS;
634 }
635
636 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
637                                 const video_format_t *source)
638 {
639     if (vlc_gl_Lock(vgl->gl))
640         return VLC_EGENERIC;
641
642     /* glTexCoord works differently with GL_TEXTURE_2D and
643        GL_TEXTURE_RECTANGLE_EXT */
644     float left[PICTURE_PLANE_MAX];
645     float top[PICTURE_PLANE_MAX];
646     float right[PICTURE_PLANE_MAX];
647     float bottom[PICTURE_PLANE_MAX];
648     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
649         float scale_w, scale_h;
650         if (vgl->tex_target == GL_TEXTURE_2D) {
651             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
652             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
653
654         } else {
655             scale_w = 1.0;
656             scale_h = 1.0;
657         }
658         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
659         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
660         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
661         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
662     }
663
664
665     /* Why drawing here and not in Render()? Because this way, the
666        OpenGL providers can call vout_display_opengl_Display to force redraw.i
667        Currently, the OS X provider uses it to get a smooth window resizing */
668
669     glClear(GL_COLOR_BUFFER_BIT);
670
671     if (vgl->program) {
672         glEnable(GL_FRAGMENT_PROGRAM_ARB);
673         for (int i = 0; i < vgl->local_count; i++)
674             vgl->ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, vgl->local_value[i]);
675     } else {
676         glEnable(vgl->tex_target);
677     }
678
679 #if USE_OPENGL_ES
680     static const GLfloat vertexCoord[] = {
681         -1.0f, -1.0f,
682          1.0f, -1.0f,
683         -1.0f,  1.0f,
684          1.0f,  1.0f,
685     };
686
687     const GLfloat textureCoord[8] = {
688         left[0],  bottom[0],
689         right[0], bottom[0],
690         left[0],  top[0],
691         right[0], top[0]
692     };
693
694     glEnableClientState(GL_VERTEX_ARRAY);
695     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
696     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
697     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
698
699     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
700 #else
701 #if !defined(MACOS_OPENGL)
702     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
703         if (vgl->chroma->plane_count > 1)
704             vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
705         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
706     }
707 #endif
708     glBegin(GL_POLYGON);
709
710     glTexCoord2f(left[0],  top[0]);
711     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
712         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, left[j], top[j]);
713     glVertex2f(-1.0,  1.0);
714
715     glTexCoord2f(right[0], top[0]);
716     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
717         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, right[j], top[j]);
718     glVertex2f( 1.0,  1.0);
719
720     glTexCoord2f(right[0], bottom[0]);
721     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
722         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, right[j], bottom[j]);
723     glVertex2f( 1.0, -1.0);
724
725     glTexCoord2f(left[0],  bottom[0]);
726     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
727         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, left[j], bottom[j]);
728     glVertex2f(-1.0, -1.0);
729
730     glEnd();
731 #endif
732
733     if (vgl->program)
734         glDisable(GL_FRAGMENT_PROGRAM_ARB);
735     else
736         glDisable(vgl->tex_target);
737
738 #if !USE_OPENGL_ES
739     if (vgl->chroma->plane_count > 1)
740         vgl->ActiveTextureARB(GL_TEXTURE0_ARB + 0);
741     glEnable(GL_TEXTURE_2D);
742     glEnable(GL_BLEND);
743     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
744     for (int i = 0; i < vgl->region_count; i++) {
745         gl_region_t *glr = &vgl->region[i];
746
747         glBindTexture(GL_TEXTURE_2D, glr->texture);
748
749         glBegin(GL_POLYGON);
750
751         glColor4f(1.0, 1.0, 1.0, glr->alpha);
752
753         glTexCoord2f(0.0, 0.0);
754         glVertex2f(glr->left, glr->top);
755
756         glTexCoord2f(1.0, 0.0);
757         glVertex2f(glr->right, glr->top);
758
759         glTexCoord2f(1.0, 1.0);
760         glVertex2f(glr->right, glr->bottom);
761
762         glTexCoord2f(0.0, 1.0);
763         glVertex2f(glr->left, glr->bottom);
764
765         glEnd();
766     }
767     glDisable(GL_BLEND);
768     glDisable(GL_TEXTURE_2D);
769 #endif
770
771     vlc_gl_Swap(vgl->gl);
772
773     vlc_gl_Unlock(vgl->gl);
774     return VLC_SUCCESS;
775 }
776