]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
26cd383eca9467e145c299a1dea0877c53f0d05e
[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_opengl.h>
34
35 #include "opengl.h"
36 // Define USE_OPENGL_ES to the GL ES Version you want to select
37
38 #if !defined (__APPLE__)
39 # if USE_OPENGL_ES == 2
40 #  include <GLES2/gl2ext.h>
41 # elif USE_OPENGL_ES == 1
42 #  include <GLES/glext.h>
43 # else
44 #   include <GL/glext.h>
45 # endif
46 #else
47 # if USE_OPENGL_ES == 2
48 #  include <OpenGLES/ES2/gl.h>
49 # elif USE_OPENGL_ES == 1
50 #  include <OpenGLES/ES1/gl.h>
51 # else
52 #  define MACOS_OPENGL
53 #  include <OpenGL/glext.h>
54 # endif
55 #endif
56
57 /* RV16 */
58 #ifndef GL_UNSIGNED_SHORT_5_6_5
59 # define GL_UNSIGNED_SHORT_5_6_5 0x8363
60 #endif
61 #ifndef GL_CLAMP_TO_EDGE
62 # define GL_CLAMP_TO_EDGE 0x812F
63 #endif
64
65 #if USE_OPENGL_ES
66 #   define VLCGL_TEXTURE_COUNT 1
67 #   define VLCGL_PICTURE_MAX 1
68 #elif defined(MACOS_OPENGL)
69 #   define VLCGL_TEXTURE_COUNT 2
70 #   define VLCGL_PICTURE_MAX 2
71 #else
72 #   define VLCGL_TEXTURE_COUNT 1
73 #   define VLCGL_PICTURE_MAX 128
74 #endif
75
76 struct vout_display_opengl_t {
77     vlc_gl_t   *gl;
78
79     video_format_t fmt;
80     const vlc_chroma_description_t *chroma;
81
82     int        tex_target;
83     int        tex_format;
84     int        tex_type;
85
86     int        tex_width[PICTURE_PLANE_MAX];
87     int        tex_height[PICTURE_PLANE_MAX];
88
89     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
90
91     picture_pool_t *pool;
92
93     GLuint     program;
94     int        local_count;
95     GLfloat    local_value[16][4];
96
97     /* fragment_program */
98     void (*GenProgramsARB)(GLsizei, GLuint *);
99     void (*BindProgramARB)(GLenum, GLuint);
100     void (*ProgramStringARB)(GLenum, GLenum, GLsizei, const GLvoid *);
101     void (*DeleteProgramsARB)(GLsizei, const GLuint *);
102     void (*ProgramLocalParameter4fvARB)(GLenum, GLuint, const GLfloat *);
103
104     /* multitexture */
105     void (*ActiveTextureARB)(GLenum);
106     void (*MultiTexCoord2fARB)(GLenum, GLfloat, GLfloat);
107 };
108
109 static inline int GetAlignedSize(unsigned size)
110 {
111     /* Return the smallest larger or equal power of 2 */
112     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
113     return ((align >> 1) == size) ? size : align;
114 }
115
116 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
117                                                const vlc_fourcc_t **subpicture_chromas,
118                                                vlc_gl_t *gl)
119 {
120     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
121     if (!vgl)
122         return NULL;
123
124     vgl->gl = gl;
125     if (vlc_gl_Lock(vgl->gl)) {
126         free(vgl);
127         return NULL;
128     }
129
130     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
131     if (!extensions)
132         extensions = "";
133
134     /* Load extensions */
135     bool supports_fp = false;
136     if (strstr(extensions, "GL_ARB_fragment_program")) {
137         vgl->GenProgramsARB    = (void (*)(GLsizei, GLuint *))vlc_gl_GetProcAddress(vgl->gl, "glGenProgramsARB");
138         vgl->BindProgramARB    = (void (*)(GLenum, GLuint))vlc_gl_GetProcAddress(vgl->gl, "glBindProgramARB");
139         vgl->ProgramStringARB  = (void (*)(GLenum, GLenum, GLsizei, const GLvoid *))vlc_gl_GetProcAddress(vgl->gl, "glProgramStringARB");
140         vgl->DeleteProgramsARB = (void (*)(GLsizei, const GLuint *))vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgramsARB");
141         vgl->ProgramLocalParameter4fvARB = (void (*)(GLenum, GLuint, const GLfloat *))vlc_gl_GetProcAddress(vgl->gl, "glProgramLocalParameter4fvARB");
142
143         supports_fp = vgl->GenProgramsARB &&
144                       vgl->BindProgramARB &&
145                       vgl->ProgramStringARB &&
146                       vgl->DeleteProgramsARB &&
147                       vgl->ProgramLocalParameter4fvARB;
148     }
149     bool supports_multitexture = false;
150     if (strstr(extensions, "GL_ARB_multitexture")) {
151         vgl->ActiveTextureARB   = (void (*)(GLenum))vlc_gl_GetProcAddress(vgl->gl, "glActiveTextureARB");
152         vgl->MultiTexCoord2fARB = (void (*)(GLenum, GLfloat, GLfloat))vlc_gl_GetProcAddress(vgl->gl, "glMultiTexCoord2fARB");
153
154         supports_multitexture = vgl->ActiveTextureARB &&
155                                 vgl->MultiTexCoord2fARB;
156     }
157
158     /* Initialize with default chroma */
159     vgl->fmt = *fmt;
160 #if USE_OPENGL_ES
161     vgl->fmt.i_chroma = VLC_CODEC_RGB16;
162 #   if defined(WORDS_BIGENDIAN)
163     vgl->fmt.i_rmask  = 0x001f;
164     vgl->fmt.i_gmask  = 0x07e0;
165     vgl->fmt.i_bmask  = 0xf800;
166 #   else
167     vgl->fmt.i_rmask  = 0xf800;
168     vgl->fmt.i_gmask  = 0x07e0;
169     vgl->fmt.i_bmask  = 0x001f;
170 #   endif
171     vgl->tex_target   = GL_TEXTURE_2D;
172     vgl->tex_format   = GL_RGB;
173     vgl->tex_type     = GL_UNSIGNED_SHORT_5_6_5;
174 #elif defined(MACOS_OPENGL)
175 #   if defined(WORDS_BIGENDIAN)
176     vgl->fmt.i_chroma = VLC_CODEC_YUYV;
177 #   else
178     vgl->fmt.i_chroma = VLC_CODEC_UYVY;
179 #   endif
180     vgl->tex_target   = GL_TEXTURE_RECTANGLE_EXT;
181     vgl->tex_format   = GL_YCBCR_422_APPLE;
182     vgl->tex_type     = GL_UNSIGNED_SHORT_8_8_APPLE;
183 #else
184     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
185 #   if defined(WORDS_BIGENDIAN)
186     vgl->fmt.i_rmask  = 0xff000000;
187     vgl->fmt.i_gmask  = 0x00ff0000;
188     vgl->fmt.i_bmask  = 0x0000ff00;
189 #   else
190     vgl->fmt.i_rmask  = 0x000000ff;
191     vgl->fmt.i_gmask  = 0x0000ff00;
192     vgl->fmt.i_bmask  = 0x00ff0000;
193 #   endif
194     vgl->tex_target   = GL_TEXTURE_2D;
195     vgl->tex_format   = GL_RGBA;
196     vgl->tex_type     = GL_UNSIGNED_BYTE;
197 #endif
198     /* Use YUV if possible and needed */
199     bool need_fs_yuv = false;
200     if (supports_fp && supports_multitexture &&
201         vlc_fourcc_IsYUV(fmt->i_chroma) && !vlc_fourcc_IsYUV(vgl->fmt.i_chroma)) {
202         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
203         while (*list) {
204             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
205             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
206                 need_fs_yuv       = true;
207                 vgl->fmt          = *fmt;
208                 vgl->fmt.i_chroma = *list;
209                 vgl->tex_format   = GL_LUMINANCE;
210                 vgl->tex_type     = GL_UNSIGNED_BYTE;
211                 break;
212             }
213             list++;
214         }
215     }
216
217     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
218
219     bool supports_npot = false;
220 #if USE_OPENGL_ES == 2
221     supports_npot = true;
222 #elif defined(MACOS_OPENGL)
223     supports_npot = true;
224 #else
225     supports_npot |= strstr(extensions, "GL_APPLE_texture_2D_limited_npot") != NULL ||
226                      strstr(extensions, "GL_ARB_texture_non_power_of_two");
227 #endif
228
229     /* Texture size
230      * TODO calculate the size such that the pictures can be used as
231      * direct buffers
232      */
233     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
234         int w = vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
235         int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
236         if (supports_npot) {
237             vgl->tex_width[j]  = w;
238             vgl->tex_height[j] = h;
239         }
240         else {
241             /* A texture must have a size aligned on a power of 2 */
242             vgl->tex_width[j]  = GetAlignedSize(w);
243             vgl->tex_height[j] = GetAlignedSize(h);
244         }
245     }
246
247     /* Build fragment program if needed */
248     vgl->program = 0;
249     vgl->local_count = 0;
250     if (supports_fp) {
251         char *code = NULL;
252
253         if (need_fs_yuv) {
254             /* [R/G/B][Y U V O] from TV range to full range
255              * XXX we could also do hue/brightness/constrast/gamma
256              * by simply changing the coefficients
257              */
258             const float matrix_bt601_tv2full[3][4] = {
259                 { 1.1640,  0.0000,  1.4030, -0.7773 },
260                 { 1.1640, -0.3440, -0.7140,  0.4580 },
261                 { 1.1640,  1.7730,  0.0000, -0.9630 },
262             };
263             const float matrix_bt709_tv2full[3][4] = {
264                 { 1.1640,  0.0000,  1.5701, -0.8612 },
265                 { 1.1640, -0.1870, -0.4664,  0.2549 },
266                 { 1.1640,  1.8556,  0.0000, -1.0045 },
267             };
268             const float (*matrix)[4] = fmt->i_height > 576 ? matrix_bt709_tv2full
269                                                            : matrix_bt601_tv2full;
270
271             /* Basic linear YUV -> RGB conversion using bilinear interpolation */
272             const char *template_yuv =
273                 "!!ARBfp1.0"
274                 "OPTION ARB_precision_hint_fastest;"
275
276                 "TEMP src;"
277                 "TEX src.x,  fragment.texcoord[0], texture[0], 2D;"
278                 "TEX src.%c, fragment.texcoord[1], texture[1], 2D;"
279                 "TEX src.%c, fragment.texcoord[2], texture[2], 2D;"
280
281                 "PARAM coefficient[4] = { program.local[0..3] };"
282
283                 "TEMP tmp;"
284                 "MAD  tmp.rgb,          src.xxxx, coefficient[0], coefficient[3];"
285                 "MAD  tmp.rgb,          src.yyyy, coefficient[1], tmp;"
286                 "MAD  result.color.rgb, src.zzzz, coefficient[2], tmp;"
287                 "END";
288             bool swap_uv = vgl->fmt.i_chroma == VLC_CODEC_YV12 ||
289                            vgl->fmt.i_chroma == VLC_CODEC_YV9;
290             if (asprintf(&code, template_yuv,
291                          swap_uv ? 'z' : 'y',
292                          swap_uv ? 'y' : 'z') < 0)
293                 code = NULL;
294
295             for (int i = 0; i < 4; i++)
296                 for (int j = 0; j < 4; j++)
297                     vgl->local_value[vgl->local_count + i][j] = j < 3 ? matrix[j][i] : 0.0;
298             vgl->local_count += 4;
299         }
300         if (code) {
301             vgl->GenProgramsARB(1, &vgl->program);
302             vgl->BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, vgl->program);
303             vgl->ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
304                                   GL_PROGRAM_FORMAT_ASCII_ARB,
305                                   strlen(code), (const GLbyte*)code);
306             if (glGetError() == GL_INVALID_OPERATION) {
307                 /* FIXME if the program was needed for YUV, the video will be broken */
308 #if 0
309                 GLint position;
310                 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &position);
311
312                 const char *msg = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
313                 fprintf(stderr, "GL_INVALID_OPERATION: error at %d: %s\n", position, msg);
314 #endif
315                 vgl->DeleteProgramsARB(1, &vgl->program);
316                 vgl->program = 0;
317             }
318             free(code);
319         }
320     }
321
322     /* */
323     glDisable(GL_BLEND);
324     glDisable(GL_DEPTH_TEST);
325     glDepthMask(GL_FALSE);
326     glDisable(GL_CULL_FACE);
327     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
328     glClear(GL_COLOR_BUFFER_BIT);
329
330     vlc_gl_Unlock(vgl->gl);
331
332     /* */
333     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
334         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
335             vgl->texture[i][j] = 0;
336     }
337     vgl->pool = NULL;
338
339     *fmt = vgl->fmt;
340     if (subpicture_chromas) {
341         *subpicture_chromas = NULL;
342     }
343     return vgl;
344 }
345
346 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
347 {
348     /* */
349     if (!vlc_gl_Lock(vgl->gl)) {
350
351         glFinish();
352         glFlush();
353         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
354             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
355
356         if (vgl->program)
357             vgl->DeleteProgramsARB(1, &vgl->program);
358
359         vlc_gl_Unlock(vgl->gl);
360     }
361     if (vgl->pool)
362         picture_pool_Delete(vgl->pool);
363     free(vgl);
364 }
365
366 #ifdef MACOS_OPENGL
367 struct picture_sys_t {
368     vout_display_opengl_t *vgl;
369     GLuint *texture;
370 };
371
372 /* Small helper */
373 static inline GLuint PictureGetTexture(picture_t *picture)
374 {
375     return *picture->p_sys->texture;
376 }
377
378 static int PictureLock(picture_t *picture)
379 {
380     if (!picture->p_sys)
381         return VLC_SUCCESS;
382
383     vout_display_opengl_t *vgl = picture->p_sys->vgl;
384     if (!vlc_gl_Lock(vgl->gl)) {
385         glBindTexture(vgl->tex_target, PictureGetTexture(picture));
386         glTexSubImage2D(vgl->tex_target, 0,
387                         0, 0, vgl->fmt.i_width, vgl->fmt.i_height,
388                         vgl->tex_format, vgl->tex_type, picture->p[0].p_pixels);
389
390         vlc_gl_Unlock(vgl->gl);
391     }
392     return VLC_SUCCESS;
393 }
394
395 static void PictureUnlock(picture_t *picture)
396 {
397     VLC_UNUSED(picture);
398 }
399 #endif
400
401 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
402 {
403     if (vgl->pool)
404         return vgl->pool;
405
406     /* Allocate our pictures */
407     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
408     unsigned count = 0;
409
410     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
411         picture[count] = picture_NewFromFormat(&vgl->fmt);
412         if (!picture[count])
413             break;
414
415 #ifdef MACOS_OPENGL
416         picture_sys_t *sys = picture[count]->p_sys = malloc(sizeof(*sys));
417         if (sys) {
418             sys->vgl = vgl;
419             sys->texture = vgl->texture[count];
420         }
421 #endif
422     }
423     if (count <= 0)
424         return NULL;
425
426     /* Wrap the pictures into a pool */
427     picture_pool_configuration_t cfg;
428     memset(&cfg, 0, sizeof(cfg));
429     cfg.picture_count = count;
430     cfg.picture       = picture;
431 #ifdef MACOS_OPENGL
432     cfg.lock          = PictureLock;
433     cfg.unlock        = PictureUnlock;
434 #endif
435     vgl->pool = picture_pool_NewExtended(&cfg);
436     if (!vgl->pool)
437         goto error;
438
439     /* Allocates our textures */
440     if (vlc_gl_Lock(vgl->gl))
441         return vgl->pool;
442
443     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
444         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
445         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
446             if (vgl->chroma->plane_count > 1)
447                 vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
448             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
449
450 #if !USE_OPENGL_ES
451             /* Set the texture parameters */
452             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
453             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
454 #endif
455
456             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
457             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
458             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
459             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
460
461 #ifdef MACOS_OPENGL
462             /* Tell the driver not to make a copy of the texture but to use
463                our buffer */
464             glEnable(GL_UNPACK_CLIENT_STORAGE_APPLE);
465             glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
466
467 #if 0
468             /* Use VRAM texturing */
469             glTexParameteri(vgl->tex_target, GL_TEXTURE_STORAGE_HINT_APPLE,
470                              GL_STORAGE_CACHED_APPLE);
471 #else
472             /* Use AGP texturing */
473             glTexParameteri(vgl->tex_target, GL_TEXTURE_STORAGE_HINT_APPLE,
474                              GL_STORAGE_SHARED_APPLE);
475 #endif
476 #endif
477
478             /* Call glTexImage2D only once, and use glTexSubImage2D later */
479             glTexImage2D(vgl->tex_target, 0,
480                          vgl->tex_format, vgl->tex_width[j], vgl->tex_height[j],
481                          0, vgl->tex_format, vgl->tex_type, NULL);
482         }
483     }
484
485     vlc_gl_Unlock(vgl->gl);
486
487     return vgl->pool;
488
489 error:
490     for (unsigned i = 0; i < count; i++)
491         picture_Delete(picture[i]);
492     return NULL;
493 }
494
495 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
496                                 picture_t *picture, subpicture_t *subpicture)
497 {
498     /* On Win32/GLX, we do this the usual way:
499        + Fill the buffer with new content,
500        + Reload the texture,
501        + Use the texture.
502
503        On OS X with VRAM or AGP texturing, the order has to be:
504        + Reload the texture,
505        + Fill the buffer with new content,
506        + Use the texture.
507
508        (Thanks to gcc from the Arstechnica forums for the tip)
509
510        Therefore on OSX, we have to use two buffers and textures and use a
511        lock(/unlock) managed picture pool.
512      */
513
514     if (vlc_gl_Lock(vgl->gl))
515         return VLC_EGENERIC;
516
517 #ifdef MACOS_OPENGL
518     /* Bind to the texture for drawing */
519     glBindTexture(vgl->tex_target, PictureGetTexture(picture));
520 #else
521     /* Update the texture */
522     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
523         if (vgl->chroma->plane_count > 1)
524             vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
525         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
526         glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch);
527         glTexSubImage2D(vgl->tex_target, 0,
528                         0, 0,
529                         vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
530                         vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
531                         vgl->tex_format, vgl->tex_type, picture->p[j].p_pixels);
532     }
533 #endif
534
535     vlc_gl_Unlock(vgl->gl);
536     VLC_UNUSED(subpicture);
537     return VLC_SUCCESS;
538 }
539
540 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
541                                 const video_format_t *source)
542 {
543     if (vlc_gl_Lock(vgl->gl))
544         return VLC_EGENERIC;
545
546     /* glTexCoord works differently with GL_TEXTURE_2D and
547        GL_TEXTURE_RECTANGLE_EXT */
548     float left[PICTURE_PLANE_MAX];
549     float top[PICTURE_PLANE_MAX];
550     float right[PICTURE_PLANE_MAX];
551     float bottom[PICTURE_PLANE_MAX];
552     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
553         float scale_w, scale_h;
554         if (vgl->tex_target == GL_TEXTURE_2D) {
555             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
556             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
557
558         } else {
559             scale_w = 1.0;
560             scale_h = 1.0;
561         }
562         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
563         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
564         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
565         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
566     }
567
568
569     /* Why drawing here and not in Render()? Because this way, the
570        OpenGL providers can call vout_display_opengl_Display to force redraw.i
571        Currently, the OS X provider uses it to get a smooth window resizing */
572
573     glClear(GL_COLOR_BUFFER_BIT);
574
575     if (vgl->program) {
576         glEnable(GL_FRAGMENT_PROGRAM_ARB);
577         for (int i = 0; i < vgl->local_count; i++)
578             vgl->ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, vgl->local_value[i]);
579     } else {
580         glEnable(vgl->tex_target);
581     }
582
583 #if USE_OPENGL_ES
584     static const GLfloat vertexCoord[] = {
585         -1.0f, -1.0f,
586          1.0f, -1.0f,
587         -1.0f,  1.0f,
588          1.0f,  1.0f,
589     };
590
591     const GLfloat textureCoord[8] = {
592         left[0],  bottom[0],
593         right[0], bottom[0],
594         left[0],  top[0],
595         right[0], top[0]
596     };
597
598     glEnableClientState(GL_VERTEX_ARRAY);
599     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
600     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
601     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
602
603     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
604 #else
605 #if !defined(MACOS_OPENGL)
606     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
607         if (vgl->chroma->plane_count > 1)
608             vgl->ActiveTextureARB(GL_TEXTURE0_ARB + j);
609         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
610     }
611 #endif
612     glBegin(GL_POLYGON);
613
614     glTexCoord2f(left[0],  top[0]);
615     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
616         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, left[j], top[j]);
617     glVertex2f(-1.0,  1.0);
618
619     glTexCoord2f(right[0], top[0]);
620     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
621         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, right[j], top[j]);
622     glVertex2f( 1.0,  1.0);
623
624     glTexCoord2f(right[0], bottom[0]);
625     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
626         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, right[j], bottom[j]);
627     glVertex2f( 1.0, -1.0);
628
629     glTexCoord2f(left[0],  bottom[0]);
630     for (unsigned j = 1; j < vgl->chroma->plane_count; j++)
631         vgl->MultiTexCoord2fARB(GL_TEXTURE0_ARB + j, left[j], bottom[j]);
632     glVertex2f(-1.0, -1.0);
633
634     glEnd();
635 #endif
636
637     if (vgl->program)
638         glDisable(GL_FRAGMENT_PROGRAM_ARB);
639     else
640         glDisable(vgl->tex_target);
641
642     vlc_gl_Swap(vgl->gl);
643
644     vlc_gl_Unlock(vgl->gl);
645     return VLC_SUCCESS;
646 }
647