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