]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
skins2: fix animated image flickering
[vlc] / modules / video_output / opengl.c
1 /*****************************************************************************
2  * opengl.c: OpenGL and OpenGL ES output common code
3  *****************************************************************************
4  * Copyright (C) 2004-2013 VLC authors and VideoLAN
5  * Copyright (C) 2009, 2011 Laurent Aimar
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *          Ilkka Ollakka <ileoo@videolan.org>
9  *          Rémi Denis-Courmont
10  *          Adrien Maglo <magsoft at videolan dot org>
11  *          Felix Paul Kühne <fkuehne at videolan dot org>
12  *          Pierre d'Herbemont <pdherbemont at videolan dot org>
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published by
16  * the Free Software Foundation; either version 2.1 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_picture_pool.h>
34 #include <vlc_subpicture.h>
35 #include <vlc_opengl.h>
36 #include <vlc_memory.h>
37
38 #include "opengl.h"
39
40 #ifndef GL_CLAMP_TO_EDGE
41 # define GL_CLAMP_TO_EDGE 0x812F
42 #endif
43
44 #if USE_OPENGL_ES == 2 || defined(__APPLE__)
45 #   define PFNGLGETPROGRAMIVPROC             typeof(glGetProgramiv)*
46 #   define PFNGLGETPROGRAMINFOLOGPROC        typeof(glGetProgramInfoLog)*
47 #   define PFNGLGETSHADERIVPROC              typeof(glGetShaderiv)*
48 #   define PFNGLGETSHADERINFOLOGPROC         typeof(glGetShaderInfoLog)*
49 #   define PFNGLGETUNIFORMLOCATIONPROC       typeof(glGetUniformLocation)*
50 #   define PFNGLGETATTRIBLOCATIONPROC        typeof(glGetAttribLocation)*
51 #   define PFNGLVERTEXATTRIBPOINTERPROC      typeof(glVertexAttribPointer)*
52 #   define PFNGLENABLEVERTEXATTRIBARRAYPROC  typeof(glEnableVertexAttribArray)*
53 #   define PFNGLUNIFORMMATRIX4FVPROC         typeof(glUniformMatrix4fv)*
54 #   define PFNGLUNIFORM4FVPROC               typeof(glUniform4fv)*
55 #   define PFNGLUNIFORM4FPROC                typeof(glUniform4f)*
56 #   define PFNGLUNIFORM1IPROC                typeof(glUniform1i)*
57 #   define PFNGLCREATESHADERPROC             typeof(glCreateShader)*
58 #   define PFNGLSHADERSOURCEPROC             typeof(glShaderSource)*
59 #   define PFNGLCOMPILESHADERPROC            typeof(glCompileShader)*
60 #   define PFNGLDELETESHADERPROC             typeof(glDeleteShader)*
61 #   define PFNGLCREATEPROGRAMPROC            typeof(glCreateProgram)*
62 #   define PFNGLLINKPROGRAMPROC              typeof(glLinkProgram)*
63 #   define PFNGLUSEPROGRAMPROC               typeof(glUseProgram)*
64 #   define PFNGLDELETEPROGRAMPROC            typeof(glDeleteProgram)*
65 #   define PFNGLATTACHSHADERPROC             typeof(glAttachShader)*
66 #   define PFNGLGENBUFFERSPROC               typeof(glGenBuffers)*
67 #   define PFNGLBINDBUFFERPROC               typeof(glBindBuffer)*
68 #   define PFNGLBUFFERDATAPROC               typeof(glBufferData)*
69 #   define PFNGLDELETEBUFFERSPROC            typeof(glDeleteBuffers)*
70 #if defined(__APPLE__) && USE_OPENGL_ES
71 #   import <CoreFoundation/CoreFoundation.h>
72 #endif
73 #endif
74
75 #if USE_OPENGL_ES
76 #   define GLSL_VERSION "100"
77 #   define VLCGL_TEXTURE_COUNT 1
78 #   define VLCGL_PICTURE_MAX 1
79 #   define PRECISION "precision highp float;"
80 #if USE_OPENGL_ES == 2
81 #   define SUPPORTS_SHADERS
82 #   define glClientActiveTexture(x)
83 #else
84 #   define SUPPORTS_FIXED_PIPELINE
85 #   define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
86 #endif
87 #else
88 #   define GLSL_VERSION "120"
89 #   define VLCGL_TEXTURE_COUNT 1
90 #   define VLCGL_PICTURE_MAX 128
91 #   define PRECISION ""
92 #   define SUPPORTS_SHADERS
93 #   define SUPPORTS_FIXED_PIPELINE
94 #endif
95
96 static const vlc_fourcc_t gl_subpicture_chromas[] = {
97     VLC_CODEC_RGBA,
98     0
99 };
100
101 typedef struct {
102     GLuint   texture;
103     unsigned format;
104     unsigned type;
105     unsigned width;
106     unsigned height;
107
108     float    alpha;
109
110     float    top;
111     float    left;
112     float    bottom;
113     float    right;
114
115     float    tex_width;
116     float    tex_height;
117 } gl_region_t;
118
119 struct vout_display_opengl_t {
120
121     vlc_gl_t   *gl;
122
123     video_format_t fmt;
124     const vlc_chroma_description_t *chroma;
125
126     int        tex_target;
127     int        tex_format;
128     int        tex_internal;
129     int        tex_type;
130
131     int        tex_width[PICTURE_PLANE_MAX];
132     int        tex_height[PICTURE_PLANE_MAX];
133
134     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
135
136     int         region_count;
137     gl_region_t *region;
138
139
140     picture_pool_t *pool;
141
142     /* index 0 for normal and 1 for subtitle overlay */
143     GLuint     program[2];
144     GLint      shader[3]; //3. is for the common vertex shader
145     int        local_count;
146     GLfloat    local_value[16];
147
148     GLuint vertex_buffer_object;
149     GLuint texture_buffer_object[PICTURE_PLANE_MAX];
150
151     GLuint *subpicture_buffer_object;
152     int    subpicture_buffer_object_count;
153
154     /* Shader variables commands*/
155 #ifdef SUPPORTS_SHADERS
156     PFNGLGETUNIFORMLOCATIONPROC      GetUniformLocation;
157     PFNGLGETATTRIBLOCATIONPROC       GetAttribLocation;
158     PFNGLVERTEXATTRIBPOINTERPROC     VertexAttribPointer;
159     PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
160
161     PFNGLUNIFORMMATRIX4FVPROC   UniformMatrix4fv;
162     PFNGLUNIFORM4FVPROC         Uniform4fv;
163     PFNGLUNIFORM4FPROC          Uniform4f;
164     PFNGLUNIFORM1IPROC          Uniform1i;
165
166     /* Shader command */
167     PFNGLCREATESHADERPROC CreateShader;
168     PFNGLSHADERSOURCEPROC ShaderSource;
169     PFNGLCOMPILESHADERPROC CompileShader;
170     PFNGLDELETESHADERPROC   DeleteShader;
171
172     PFNGLCREATEPROGRAMPROC CreateProgram;
173     PFNGLLINKPROGRAMPROC   LinkProgram;
174     PFNGLUSEPROGRAMPROC    UseProgram;
175     PFNGLDELETEPROGRAMPROC DeleteProgram;
176
177     PFNGLATTACHSHADERPROC  AttachShader;
178
179     /* Shader log commands */
180     PFNGLGETPROGRAMIVPROC  GetProgramiv;
181     PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
182     PFNGLGETSHADERIVPROC   GetShaderiv;
183     PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
184
185     PFNGLGENBUFFERSPROC    GenBuffers;
186     PFNGLBINDBUFFERPROC    BindBuffer;
187     PFNGLBUFFERDATAPROC    BufferData;
188     PFNGLDELETEBUFFERSPROC DeleteBuffers;
189 #endif
190
191 #if defined(_WIN32)
192     PFNGLACTIVETEXTUREPROC  ActiveTexture;
193     PFNGLCLIENTACTIVETEXTUREPROC  ClientActiveTexture;
194 #endif
195
196
197     /* multitexture */
198     bool use_multitexture;
199
200     /* Non-power-of-2 texture size support */
201     bool supports_npot;
202
203     uint8_t *texture_temp_buf;
204     int      texture_temp_buf_size;
205 };
206
207 static inline int GetAlignedSize(unsigned size)
208 {
209     /* Return the smallest larger or equal power of 2 */
210     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
211     return ((align >> 1) == size) ? size : align;
212 }
213
214 #if !USE_OPENGL_ES
215 static bool IsLuminance16Supported(int target)
216 {
217     GLuint texture;
218
219     glGenTextures(1, &texture);
220     glBindTexture(target, texture);
221     glTexImage2D(target, 0, GL_LUMINANCE16,
222                  64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
223     GLint size = 0;
224     glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
225
226     glDeleteTextures(1, &texture);
227
228     return size == 16;
229 }
230 #endif
231
232 #ifdef SUPPORTS_SHADERS
233 static void BuildVertexShader(vout_display_opengl_t *vgl,
234                               GLint *shader)
235 {
236     /* Basic vertex shader */
237     const char *vertexShader =
238         "#version " GLSL_VERSION "\n"
239         PRECISION
240         "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
241         "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
242         "attribute vec2 VertexPosition;"
243         "uniform mat4 RotationMatrix;"
244         "void main() {"
245         " TexCoord0 = MultiTexCoord0;"
246         " TexCoord1 = MultiTexCoord1;"
247         " TexCoord2 = MultiTexCoord2;"
248         " gl_Position = RotationMatrix * vec4(VertexPosition, 0.0, 1.0);"
249         "}";
250
251     *shader = vgl->CreateShader(GL_VERTEX_SHADER);
252     vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
253     vgl->CompileShader(*shader);
254 }
255
256 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
257                                    GLint *shader,
258                                    int *local_count,
259                                    GLfloat *local_value,
260                                    const video_format_t *fmt,
261                                    float yuv_range_correction)
262
263 {
264     /* [R/G/B][Y U V O] from TV range to full range
265      * XXX we could also do hue/brightness/constrast/gamma
266      * by simply changing the coefficients
267      */
268     const float matrix_bt601_tv2full[12] = {
269         1.164383561643836,  0.0000,             1.596026785714286, -0.874202217873451 ,
270         1.164383561643836, -0.391762290094914, -0.812967647237771,  0.531667823499146 ,
271         1.164383561643836,  2.017232142857142,  0.0000,            -1.085630789302022 ,
272     };
273     const float matrix_bt709_tv2full[12] = {
274         1.164383561643836,  0.0000,             1.792741071428571, -0.972945075016308 ,
275         1.164383561643836, -0.21324861427373,  -0.532909328559444,  0.301482665475862 ,
276         1.164383561643836,  2.112401785714286,  0.0000,            -1.133402217873451 ,
277     };
278     const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
279                                                 : matrix_bt601_tv2full;
280
281     /* Basic linear YUV -> RGB conversion using bilinear interpolation */
282     const char *template_glsl_yuv =
283         "#version " GLSL_VERSION "\n"
284         PRECISION
285         "uniform sampler2D Texture0;"
286         "uniform sampler2D Texture1;"
287         "uniform sampler2D Texture2;"
288         "uniform vec4      Coefficient[4];"
289         "varying vec4      TexCoord0,TexCoord1,TexCoord2;"
290
291         "void main(void) {"
292         " vec4 x,y,z,result;"
293         " x  = texture2D(Texture0, TexCoord0.st);"
294         " %c = texture2D(Texture1, TexCoord1.st);"
295         " %c = texture2D(Texture2, TexCoord2.st);"
296
297         " result = x * Coefficient[0] + Coefficient[3];"
298         " result = (y * Coefficient[1]) + result;"
299         " result = (z * Coefficient[2]) + result;"
300         " gl_FragColor = result;"
301         "}";
302     bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
303                    fmt->i_chroma == VLC_CODEC_YV9;
304
305     char *code;
306     if (asprintf(&code, template_glsl_yuv,
307                  swap_uv ? 'z' : 'y',
308                  swap_uv ? 'y' : 'z') < 0)
309         code = NULL;
310
311     for (int i = 0; i < 4; i++) {
312         float correction = i < 3 ? yuv_range_correction : 1.f;
313         /* We place coefficient values for coefficient[4] in one array from matrix values.
314            Notice that we fill values from top down instead of left to right.*/
315         for (int j = 0; j < 4; j++)
316             local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
317                                                       : 0.f;
318     }
319     (*local_count) += 4;
320
321
322     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
323     vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
324     vgl->CompileShader(*shader);
325
326     free(code);
327 }
328
329 #if 0
330 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
331                                    GLint *shader)
332 {
333     // Simple shader for RGB
334     const char *code =
335         "#version " GLSL_VERSION "\n"
336         PRECISION
337         "uniform sampler2D Texture[3];"
338         "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
339         "void main()"
340         "{ "
341         "  gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
342         "}";
343     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
344     vgl->ShaderSource(*shader, 1, &code, NULL);
345     vgl->CompileShader(*shader);
346 }
347 #endif
348
349 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
350                                    GLint *shader)
351 {
352     // Simple shader for RGBA
353     const char *code =
354         "#version " GLSL_VERSION "\n"
355         PRECISION
356         "uniform sampler2D Texture;"
357         "uniform vec4 FillColor;"
358         "varying vec4 TexCoord0;"
359         "void main()"
360         "{ "
361         "  gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
362         "}";
363     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
364     vgl->ShaderSource(*shader, 1, &code, NULL);
365     vgl->CompileShader(*shader);
366 }
367
368 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
369                                    GLint *shader)
370 {
371     /* Shader for XYZ to RGB correction
372      * 3 steps :
373      *  - XYZ gamma correction
374      *  - XYZ to RGB matrix conversion
375      *  - reverse RGB gamma correction
376      */
377       const char *code =
378         "#version " GLSL_VERSION "\n"
379         PRECISION
380         "uniform sampler2D Texture0;"
381         "uniform vec4 xyz_gamma = vec4(2.6);"
382         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
383         // WARN: matrix Is filled column by column (not row !)
384         "uniform mat4 matrix_xyz_rgb = mat4("
385         "    3.240454 , -0.9692660, 0.0556434, 0.0,"
386         "   -1.5371385,  1.8760108, -0.2040259, 0.0,"
387         "    -0.4985314, 0.0415560, 1.0572252,  0.0,"
388         "    0.0,      0.0,         0.0,        1.0 "
389         " );"
390
391         "varying vec4 TexCoord0;"
392         "void main()"
393         "{ "
394         " vec4 v_in, v_out;"
395         " v_in  = texture2D(Texture0, TexCoord0.st);"
396         " v_in = pow(v_in, xyz_gamma);"
397         " v_out = matrix_xyz_rgb * v_in ;"
398         " v_out = pow(v_out, rgb_gamma) ;"
399         " v_out = clamp(v_out, 0.0, 1.0) ;"
400         " gl_FragColor = v_out;"
401         "}";
402     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
403     vgl->ShaderSource(*shader, 1, &code, NULL);
404     vgl->CompileShader(*shader);
405 }
406
407 #endif
408
409 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
410                                                const vlc_fourcc_t **subpicture_chromas,
411                                                vlc_gl_t *gl)
412 {
413     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
414     if (!vgl)
415         return NULL;
416
417     vgl->gl = gl;
418     if (vlc_gl_Lock(vgl->gl)) {
419         free(vgl);
420         return NULL;
421     }
422
423     if (vgl->gl->getProcAddress == NULL) {
424         fprintf(stderr, "getProcAddress not implemented, bailing out\n");
425         free(vgl);
426         return NULL;
427     }
428
429     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
430 #if !USE_OPENGL_ES
431     const unsigned char *ogl_version = glGetString(GL_VERSION);
432     bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
433 #else
434     bool supports_shaders = false;
435 #endif
436
437 #if USE_OPENGL_ES == 2
438     vgl->CreateShader  = glCreateShader;
439     vgl->ShaderSource  = glShaderSource;
440     vgl->CompileShader = glCompileShader;
441     vgl->AttachShader  = glAttachShader;
442
443     vgl->GetProgramiv  = glGetProgramiv;
444     vgl->GetShaderiv   = glGetShaderiv;
445     vgl->GetProgramInfoLog  = glGetProgramInfoLog;
446     vgl->GetShaderInfoLog   = glGetShaderInfoLog;
447
448     vgl->DeleteShader  = glDeleteShader;
449
450     vgl->GetUniformLocation = glGetUniformLocation;
451     vgl->GetAttribLocation  = glGetAttribLocation;
452     vgl->VertexAttribPointer= glVertexAttribPointer;
453     vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
454     vgl->UniformMatrix4fv = glUniformMatrix4fv;
455     vgl->Uniform4fv    = glUniform4fv;
456     vgl->Uniform4f     = glUniform4f;
457     vgl->Uniform1i     = glUniform1i;
458
459     vgl->CreateProgram = glCreateProgram;
460     vgl->LinkProgram   = glLinkProgram;
461     vgl->UseProgram    = glUseProgram;
462     vgl->DeleteProgram = glDeleteProgram;
463
464     vgl->GenBuffers    = glGenBuffers;
465     vgl->BindBuffer    = glBindBuffer;
466     vgl->BufferData    = glBufferData;
467     vgl->DeleteBuffers = glDeleteBuffers;
468
469     supports_shaders = true;
470 #elif defined(SUPPORTS_SHADERS)
471     vgl->CreateShader  = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
472     vgl->ShaderSource  = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
473     vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
474     vgl->AttachShader  = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
475
476     vgl->GetProgramiv  = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
477     vgl->GetShaderiv   = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
478     vgl->GetProgramInfoLog  = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
479     vgl->GetShaderInfoLog   = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
480
481     vgl->DeleteShader  = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
482
483     vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
484     vgl->GetAttribLocation  = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
485     vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
486     vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
487     vgl->UniformMatrix4fv   = (PFNGLUNIFORMMATRIX4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniformMatrix4fv");
488     vgl->Uniform4fv    = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
489     vgl->Uniform4f     = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
490     vgl->Uniform1i     = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
491
492     vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
493     vgl->LinkProgram   = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
494     vgl->UseProgram    = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
495     vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
496
497     vgl->GenBuffers    = (PFNGLGENBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glGenBuffers");
498     vgl->BindBuffer    = (PFNGLBINDBUFFERPROC)vlc_gl_GetProcAddress(vgl->gl, "glBindBuffer");
499     vgl->BufferData    = (PFNGLBUFFERDATAPROC)vlc_gl_GetProcAddress(vgl->gl, "glBufferData");
500     vgl->DeleteBuffers = (PFNGLDELETEBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteBuffers");
501
502     if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
503         supports_shaders = false;
504 #endif
505
506 #if defined(_WIN32)
507     vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
508     vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
509 #   define glActiveTexture vgl->ActiveTexture
510 #   define glClientActiveTexture vgl->ClientActiveTexture
511 #endif
512
513     vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
514                          HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
515
516 #if USE_OPENGL_ES == 2
517     /* OpenGL ES 2 includes support for non-power of 2 textures by specification
518      * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
519     vgl->supports_npot = true;
520 #endif
521
522     GLint max_texture_units = 0;
523     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
524
525 #ifdef __APPLE__
526 #if USE_OPENGL_ES
527     /* work-around an iOS 6 bug */
528     if (kCFCoreFoundationVersionNumber >= 786.)
529         max_texture_units = 8;
530     supports_shaders = true;
531 #endif
532 #endif
533
534     /* Initialize with default chroma */
535     vgl->fmt = *fmt;
536     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
537 #   if defined(WORDS_BIGENDIAN)
538     vgl->fmt.i_rmask  = 0xff000000;
539     vgl->fmt.i_gmask  = 0x00ff0000;
540     vgl->fmt.i_bmask  = 0x0000ff00;
541 #   else
542     vgl->fmt.i_rmask  = 0x000000ff;
543     vgl->fmt.i_gmask  = 0x0000ff00;
544     vgl->fmt.i_bmask  = 0x00ff0000;
545 #   endif
546     vgl->tex_target   = GL_TEXTURE_2D;
547     vgl->tex_format   = GL_RGBA;
548     vgl->tex_internal = GL_RGBA;
549     vgl->tex_type     = GL_UNSIGNED_BYTE;
550     /* Use YUV if possible and needed */
551     bool need_fs_yuv = false;
552     bool need_fs_xyz = false;
553     bool need_fs_rgba = USE_OPENGL_ES == 2;
554     float yuv_range_correction = 1.0;
555
556     if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
557         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
558         while (*list) {
559             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
560             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
561                 need_fs_yuv       = true;
562                 vgl->fmt          = *fmt;
563                 vgl->fmt.i_chroma = *list;
564                 vgl->tex_format   = GL_LUMINANCE;
565                 vgl->tex_internal = GL_LUMINANCE;
566                 vgl->tex_type     = GL_UNSIGNED_BYTE;
567                 yuv_range_correction = 1.0;
568                 break;
569 #if !USE_OPENGL_ES
570             } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
571                        IsLuminance16Supported(vgl->tex_target)) {
572                 need_fs_yuv       = true;
573                 vgl->fmt          = *fmt;
574                 vgl->fmt.i_chroma = *list;
575                 vgl->tex_format   = GL_LUMINANCE;
576                 vgl->tex_internal = GL_LUMINANCE16;
577                 vgl->tex_type     = GL_UNSIGNED_SHORT;
578                 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
579                 break;
580 #endif
581             }
582             list++;
583         }
584     }
585
586     if (fmt->i_chroma == VLC_CODEC_XYZ12) {
587         vlc_fourcc_GetChromaDescription(fmt->i_chroma);
588         need_fs_xyz       = true;
589         vgl->fmt          = *fmt;
590         vgl->fmt.i_chroma = VLC_CODEC_XYZ12;
591         vgl->tex_format   = GL_RGB;
592         vgl->tex_internal = GL_RGB;
593         vgl->tex_type     = GL_UNSIGNED_SHORT;
594     }
595     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
596     vgl->use_multitexture = vgl->chroma->plane_count > 1;
597
598     /* Texture size */
599     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
600         int w = vgl->fmt.i_visible_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
601         int h = vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
602         if (vgl->supports_npot) {
603             vgl->tex_width[j]  = w;
604             vgl->tex_height[j] = h;
605         } else {
606             vgl->tex_width[j]  = GetAlignedSize(w);
607             vgl->tex_height[j] = GetAlignedSize(h);
608         }
609     }
610
611     /* Build program if needed */
612     vgl->program[0] =
613     vgl->program[1] = 0;
614     vgl->shader[0] =
615     vgl->shader[1] =
616     vgl->shader[2] = -1;
617     vgl->local_count = 0;
618     if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
619 #ifdef SUPPORTS_SHADERS
620         if (need_fs_xyz)
621             BuildXYZFragmentShader(vgl, &vgl->shader[0]);
622         else
623             BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
624                                 vgl->local_value, fmt, yuv_range_correction);
625
626         BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
627         BuildVertexShader(vgl, &vgl->shader[2]);
628
629         /* Check shaders messages */
630         for (unsigned j = 0; j < 3; j++) {
631             int infoLength;
632             vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
633             if (infoLength <= 1)
634                 continue;
635
636             char *infolog = malloc(infoLength);
637             int charsWritten;
638             vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
639             fprintf(stderr, "shader %d: %s\n", j, infolog);
640             free(infolog);
641         }
642
643         vgl->program[0] = vgl->CreateProgram();
644         vgl->AttachShader(vgl->program[0], vgl->shader[0]);
645         vgl->AttachShader(vgl->program[0], vgl->shader[2]);
646         vgl->LinkProgram(vgl->program[0]);
647
648         vgl->program[1] = vgl->CreateProgram();
649         vgl->AttachShader(vgl->program[1], vgl->shader[1]);
650         vgl->AttachShader(vgl->program[1], vgl->shader[2]);
651         vgl->LinkProgram(vgl->program[1]);
652
653         /* Check program messages */
654         for (GLuint i = 0; i < 2; i++) {
655             int infoLength = 0;
656             vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
657             if (infoLength <= 1)
658                 continue;
659             char *infolog = malloc(infoLength);
660             int charsWritten;
661             vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
662             fprintf(stderr, "shader program %d: %s\n", i, infolog);
663             free(infolog);
664
665             /* If there is some message, better to check linking is ok */
666             GLint link_status = GL_TRUE;
667             vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
668             if (link_status == GL_FALSE) {
669                 fprintf(stderr, "Unable to use program %d\n", i);
670                 free(vgl);
671                 return NULL;
672             }
673         }
674 #else
675         (void)yuv_range_correction;
676 #endif
677     }
678
679     /* */
680     glDisable(GL_BLEND);
681     glDisable(GL_DEPTH_TEST);
682     glDepthMask(GL_FALSE);
683     glDisable(GL_CULL_FACE);
684     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
685     glClear(GL_COLOR_BUFFER_BIT);
686
687 #ifdef SUPPORTS_SHADERS
688     vgl->GenBuffers(1, &vgl->vertex_buffer_object);
689     vgl->GenBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
690
691     /* Initial number of allocated buffer objects for subpictures, will grow dynamically. */
692     int subpicture_buffer_object_count = 8;
693     vgl->subpicture_buffer_object = malloc(subpicture_buffer_object_count * sizeof(GLuint));
694     if (!vgl->subpicture_buffer_object) {
695         vlc_gl_Unlock(vgl->gl);
696         vout_display_opengl_Delete(vgl);
697         return NULL;
698     }
699     vgl->subpicture_buffer_object_count = subpicture_buffer_object_count;
700     vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
701 #endif
702
703     vlc_gl_Unlock(vgl->gl);
704
705     /* */
706     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
707         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
708             vgl->texture[i][j] = 0;
709     }
710     vgl->region_count = 0;
711     vgl->region = NULL;
712     vgl->pool = NULL;
713
714     *fmt = vgl->fmt;
715     if (subpicture_chromas) {
716         *subpicture_chromas = gl_subpicture_chromas;
717     }
718     return vgl;
719 }
720
721 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
722 {
723     /* */
724     if (!vlc_gl_Lock(vgl->gl)) {
725         glFinish();
726         glFlush();
727         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
728             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
729         for (int i = 0; i < vgl->region_count; i++) {
730             if (vgl->region[i].texture)
731                 glDeleteTextures(1, &vgl->region[i].texture);
732         }
733         free(vgl->region);
734
735 #ifdef SUPPORTS_SHADERS
736         if (vgl->program[0]) {
737             for (int i = 0; i < 2; i++)
738                 vgl->DeleteProgram(vgl->program[i]);
739             for (int i = 0; i < 3; i++)
740                 vgl->DeleteShader(vgl->shader[i]);
741         }
742         vgl->DeleteBuffers(1, &vgl->vertex_buffer_object);
743         vgl->DeleteBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
744         if (vgl->subpicture_buffer_object_count > 0)
745             vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
746         free(vgl->subpicture_buffer_object);
747 #endif
748
749         free(vgl->texture_temp_buf);
750         vlc_gl_Unlock(vgl->gl);
751     }
752     if (vgl->pool)
753         picture_pool_Delete(vgl->pool);
754     free(vgl);
755 }
756
757 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
758 {
759     if (vgl->pool)
760         return vgl->pool;
761
762     /* Allocate our pictures */
763     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
764     unsigned count;
765
766     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
767         picture[count] = picture_NewFromFormat(&vgl->fmt);
768         if (!picture[count])
769             break;
770     }
771     if (count <= 0)
772         return NULL;
773
774     /* Wrap the pictures into a pool */
775     picture_pool_configuration_t cfg;
776     memset(&cfg, 0, sizeof(cfg));
777     cfg.picture_count = count;
778     cfg.picture       = picture;
779     vgl->pool = picture_pool_NewExtended(&cfg);
780     if (!vgl->pool)
781         goto error;
782
783     /* Allocates our textures */
784     if (vlc_gl_Lock(vgl->gl))
785         return vgl->pool;
786
787     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
788         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
789         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
790             if (vgl->use_multitexture) {
791                 glActiveTexture(GL_TEXTURE0 + j);
792                 glClientActiveTexture(GL_TEXTURE0 + j);
793             }
794             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
795
796 #if !USE_OPENGL_ES
797             /* Set the texture parameters */
798             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
799             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
800 #endif
801
802             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
803             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
804             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
805             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
806
807             /* Call glTexImage2D only once, and use glTexSubImage2D later */
808             glTexImage2D(vgl->tex_target, 0,
809                          vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
810                          0, vgl->tex_format, vgl->tex_type, NULL);
811         }
812     }
813
814     vlc_gl_Unlock(vgl->gl);
815
816     return vgl->pool;
817
818 error:
819     for (unsigned i = 0; i < count; i++)
820         picture_Release(picture[i]);
821     return NULL;
822 }
823
824 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
825 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
826                    int in_full_width, int in_full_height,
827                    int w_num, int w_den, int h_num, int h_den,
828                    int pitch, int pixel_pitch,
829                    int full_upload, const uint8_t *pixels,
830                    int tex_target, int tex_format, int tex_type)
831 {
832     int width       =       in_width * w_num / w_den;
833     int full_width  =  in_full_width * w_num / w_den;
834     int height      =      in_height * h_num / h_den;
835     int full_height = in_full_height * h_num / h_den;
836     // This unpack alignment is the default, but setting it just in case.
837     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
838 #ifndef GL_UNPACK_ROW_LENGTH
839     int dst_width = full_upload ? full_width : width;
840     int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
841     if ( pitch != dst_pitch )
842     {
843         int buf_size = dst_pitch * full_height * pixel_pitch;
844         const uint8_t *source = pixels;
845         uint8_t *destination;
846         if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
847         {
848             free( vgl->texture_temp_buf );
849             vgl->texture_temp_buf = xmalloc( buf_size );
850             vgl->texture_temp_buf_size = buf_size;
851         }
852         destination = vgl->texture_temp_buf;
853
854         for( int h = 0; h < height ; h++ )
855         {
856             memcpy( destination, source, width * pixel_pitch );
857             source += pitch;
858             destination += dst_pitch;
859         }
860         if (full_upload)
861             glTexImage2D( tex_target, 0, tex_format,
862                           full_width, full_height,
863                           0, tex_format, tex_type, vgl->texture_temp_buf );
864         else
865             glTexSubImage2D( tex_target, 0,
866                              0, 0,
867                              width, height,
868                              tex_format, tex_type, vgl->texture_temp_buf );
869     } else {
870 #else
871     (void) width;
872     (void) height;
873     (void) vgl;
874     {
875         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
876 #endif
877         if (full_upload)
878             glTexImage2D(tex_target, 0, tex_format,
879                          full_width, full_height,
880                          0, tex_format, tex_type, pixels);
881         else
882             glTexSubImage2D(tex_target, 0,
883                             0, 0,
884                             width, height,
885                             tex_format, tex_type, pixels);
886     }
887 }
888
889 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
890                                 picture_t *picture, subpicture_t *subpicture)
891 {
892     if (vlc_gl_Lock(vgl->gl))
893         return VLC_EGENERIC;
894
895     /* Update the texture */
896     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
897         if (vgl->use_multitexture) {
898             glActiveTexture(GL_TEXTURE0 + j);
899             glClientActiveTexture(GL_TEXTURE0 + j);
900         }
901         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
902
903         Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
904                vgl->fmt.i_width, vgl->fmt.i_height,
905                vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
906                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);
907     }
908
909     int         last_count = vgl->region_count;
910     gl_region_t *last = vgl->region;
911
912     vgl->region_count = 0;
913     vgl->region       = NULL;
914
915     if (subpicture) {
916
917         int count = 0;
918         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
919             count++;
920
921         vgl->region_count = count;
922         vgl->region       = calloc(count, sizeof(*vgl->region));
923
924         if (vgl->use_multitexture) {
925             glActiveTexture(GL_TEXTURE0 + 0);
926             glClientActiveTexture(GL_TEXTURE0 + 0);
927         }
928         int i = 0;
929         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
930             gl_region_t *glr = &vgl->region[i];
931
932             glr->format = GL_RGBA;
933             glr->type   = GL_UNSIGNED_BYTE;
934             glr->width  = r->fmt.i_visible_width;
935             glr->height = r->fmt.i_visible_height;
936             if (!vgl->supports_npot) {
937                 glr->width  = GetAlignedSize(glr->width);
938                 glr->height = GetAlignedSize(glr->height);
939                 glr->tex_width  = (float) r->fmt.i_visible_width  / glr->width;
940                 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
941             } else {
942                 glr->tex_width  = 1.0;
943                 glr->tex_height = 1.0;
944             }
945             glr->alpha  = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
946             glr->left   =  2.0 * (r->i_x                          ) / subpicture->i_original_picture_width  - 1.0;
947             glr->top    = -2.0 * (r->i_y                          ) / subpicture->i_original_picture_height + 1.0;
948             glr->right  =  2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width  - 1.0;
949             glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
950
951             glr->texture = 0;
952             /* Try to recycle the textures allocated by the previous
953                call to this function. */
954             for (int j = 0; j < last_count; j++) {
955                 if (last[j].texture &&
956                     last[j].width  == glr->width &&
957                     last[j].height == glr->height &&
958                     last[j].format == glr->format &&
959                     last[j].type   == glr->type) {
960                     glr->texture = last[j].texture;
961                     memset(&last[j], 0, sizeof(last[j]));
962                     break;
963                 }
964             }
965
966             const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
967                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
968             if (glr->texture) {
969                 /* A texture was successfully recycled, reuse it. */
970                 glBindTexture(GL_TEXTURE_2D, glr->texture);
971                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
972                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
973                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
974             } else {
975                 /* Could not recycle a previous texture, generate a new one. */
976                 glGenTextures(1, &glr->texture);
977                 glBindTexture(GL_TEXTURE_2D, glr->texture);
978 #if !USE_OPENGL_ES
979                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
980                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
981 #endif
982                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
983                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
984                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
985                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
986                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
987                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
988                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
989             }
990         }
991     }
992     for (int i = 0; i < last_count; i++) {
993         if (last[i].texture)
994             glDeleteTextures(1, &last[i].texture);
995     }
996     free(last);
997
998     vlc_gl_Unlock(vgl->gl);
999     VLC_UNUSED(subpicture);
1000     return VLC_SUCCESS;
1001 }
1002
1003 static const GLfloat identity[] = {
1004     1.0f, 0.0f, 0.0f, 0.0f,
1005     0.0f, 1.0f, 0.0f, 0.0f,
1006     0.0f, 0.0f, 1.0f, 0.0f,
1007     0.0f, 0.0f, 0.0f, 1.0f
1008 };
1009
1010 static void orientationTransformMatrix(GLfloat matrix[static 16], video_orientation_t orientation) {
1011
1012     memcpy(matrix, identity, sizeof(identity));
1013
1014     const int k_cos_pi = -1;
1015     const int k_cos_pi_2 = 0;
1016     const int k_cos_n_pi_2 = 0;
1017
1018     const int k_sin_pi = 0;
1019     const int k_sin_pi_2 = 1;
1020     const int k_sin_n_pi_2 = -1;
1021
1022     bool rotate = false;
1023     int cos = 0, sin = 0;
1024
1025     switch (orientation) {
1026
1027         case ORIENT_ROTATED_90:
1028             cos = k_cos_pi_2;
1029             sin = k_sin_pi_2;
1030             rotate = true;
1031             break;
1032         case ORIENT_ROTATED_180:
1033             cos = k_cos_pi;
1034             sin = k_sin_pi;
1035             rotate = true;
1036             break;
1037         case ORIENT_ROTATED_270:
1038             cos = k_cos_n_pi_2;
1039             sin = k_sin_n_pi_2;
1040             rotate = true;
1041             break;
1042         case ORIENT_HFLIPPED:
1043             matrix[0 * 4 + 0] = -1;
1044             break;
1045         case ORIENT_VFLIPPED:
1046             matrix[1 * 4 + 1] = -1;
1047             break;
1048         case ORIENT_TRANSPOSED:
1049             matrix[0 * 4 + 0] = 0;
1050             matrix[0 * 4 + 1] = -1;
1051             matrix[1 * 4 + 0] = -1;
1052             matrix[1 * 4 + 1] = 0;
1053             break;
1054         case ORIENT_ANTI_TRANSPOSED:
1055             matrix[0 * 4 + 0] = 0;
1056             matrix[0 * 4 + 1] = 1;
1057             matrix[1 * 4 + 0] = 1;
1058             matrix[1 * 4 + 1] = 0;
1059             break;
1060         default:
1061             break;
1062     }
1063
1064     if (rotate) {
1065
1066         matrix[0 * 4 + 0] = cos;
1067         matrix[0 * 4 + 1] = -sin;
1068         matrix[1 * 4 + 0] = sin;
1069         matrix[1 * 4 + 1] = cos;
1070     }
1071 }
1072
1073 #ifdef SUPPORTS_FIXED_PIPELINE
1074 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
1075                                float *left, float *top, float *right, float *bottom)
1076 {
1077     static const GLfloat vertexCoord[] = {
1078         -1.0f, -1.0f,
1079          1.0f, -1.0f,
1080         -1.0f,  1.0f,
1081          1.0f,  1.0f,
1082     };
1083
1084     const GLfloat textureCoord[] = {
1085         left[0],  bottom[0],
1086         right[0], bottom[0],
1087         left[0],  top[0],
1088         right[0], top[0]
1089     };
1090
1091     GLfloat transformMatrix[16];
1092     orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1093
1094     glPushMatrix();
1095     glMatrixMode(GL_MODELVIEW);
1096     glLoadMatrixf(transformMatrix);
1097
1098     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1099     glEnable(vgl->tex_target);
1100     glActiveTexture(GL_TEXTURE0 + 0);
1101     glClientActiveTexture(GL_TEXTURE0 + 0);
1102
1103     glBindTexture(vgl->tex_target, vgl->texture[0][0]);
1104
1105     glEnableClientState(GL_VERTEX_ARRAY);
1106     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1107
1108     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1109     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1110
1111     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1112
1113     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1114     glDisableClientState(GL_VERTEX_ARRAY);
1115     glDisable(vgl->tex_target);
1116
1117     glPopMatrix();
1118
1119 }
1120 #endif
1121
1122 #ifdef SUPPORTS_SHADERS
1123 static void DrawWithShaders(vout_display_opengl_t *vgl,
1124                             float *left, float *top, float *right, float *bottom,
1125                             int program)
1126 {
1127     vgl->UseProgram(vgl->program[program]);
1128     if (program == 0) {
1129         if (vgl->chroma->plane_count == 3) {
1130             vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
1131             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1132             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
1133             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
1134         }
1135         else if (vgl->chroma->plane_count == 1) {
1136             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1137         }
1138     } else {
1139         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
1140         vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
1141     }
1142
1143     static const GLfloat vertexCoord[] = {
1144         -1.0,  1.0,
1145         -1.0, -1.0,
1146          1.0,  1.0,
1147          1.0, -1.0,
1148     };
1149
1150     GLfloat transformMatrix[16];
1151     orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1152
1153     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1154         const GLfloat textureCoord[] = {
1155             left[j],  top[j],
1156             left[j],  bottom[j],
1157             right[j], top[j],
1158             right[j], bottom[j],
1159         };
1160         glActiveTexture(GL_TEXTURE0+j);
1161         glClientActiveTexture(GL_TEXTURE0+j);
1162         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1163
1164         vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->texture_buffer_object[j]);
1165         vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1166
1167         char attribute[20];
1168         snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1169         vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1170         vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, 0);
1171     }
1172     glActiveTexture(GL_TEXTURE0 + 0);
1173     glClientActiveTexture(GL_TEXTURE0 + 0);
1174
1175     vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->vertex_buffer_object);
1176     vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1177     vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1178     vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1179
1180     vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[program], "RotationMatrix"), 1, GL_FALSE, transformMatrix);
1181
1182     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1183 }
1184 #endif
1185
1186 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1187                                 const video_format_t *source)
1188 {
1189     if (vlc_gl_Lock(vgl->gl))
1190         return VLC_EGENERIC;
1191
1192     /* Why drawing here and not in Render()? Because this way, the
1193        OpenGL providers can call vout_display_opengl_Display to force redraw.i
1194        Currently, the OS X provider uses it to get a smooth window resizing */
1195     glClear(GL_COLOR_BUFFER_BIT);
1196
1197     /* Draw the picture */
1198     float left[PICTURE_PLANE_MAX];
1199     float top[PICTURE_PLANE_MAX];
1200     float right[PICTURE_PLANE_MAX];
1201     float bottom[PICTURE_PLANE_MAX];
1202     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1203         /* glTexCoord works differently with GL_TEXTURE_2D and
1204            GL_TEXTURE_RECTANGLE_EXT */
1205         float scale_w, scale_h;
1206
1207         if (vgl->tex_target == GL_TEXTURE_2D) {
1208             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1209             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1210
1211         } else {
1212             scale_w = 1.0;
1213             scale_h = 1.0;
1214         }
1215         /* Warning: if NPOT is not supported a larger texture is
1216            allocated. This will cause right and bottom coordinates to
1217            land on the edge of two texels with the texels to the
1218            right/bottom uninitialized by the call to
1219            glTexSubImage2D. This might cause a green line to appear on
1220            the right/bottom of the display.
1221            There are two possible solutions:
1222            - Manually mirror the edges of the texture.
1223            - Add a "-1" when computing right and bottom, however the
1224            last row/column might not be displayed at all.
1225         */
1226         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
1227         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
1228         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
1229         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1230     }
1231
1232 #ifdef SUPPORTS_SHADERS
1233     if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1234         DrawWithShaders(vgl, left, top, right, bottom, 0);
1235     else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1236         DrawWithShaders(vgl, left, top, right, bottom, 1);
1237     else
1238 #endif
1239     {
1240 #ifdef SUPPORTS_FIXED_PIPELINE
1241         DrawWithoutShaders(vgl, left, top, right, bottom);
1242 #endif
1243     }
1244
1245     /* Draw the subpictures */
1246     if (vgl->program[1]) {
1247 #ifdef SUPPORTS_SHADERS
1248         // Change the program for overlays
1249         vgl->UseProgram(vgl->program[1]);
1250         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1251 #endif
1252     }
1253
1254 #ifdef SUPPORTS_FIXED_PIPELINE
1255     glEnable(GL_TEXTURE_2D);
1256 #endif
1257     glEnable(GL_BLEND);
1258     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1259
1260 #ifdef SUPPORTS_SHADERS
1261     /* We need two buffer objects for each region: for vertex and texture coordinates. */
1262     if (2 * vgl->region_count > vgl->subpicture_buffer_object_count) {
1263         if (vgl->subpicture_buffer_object_count > 0)
1264             vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1265         vgl->subpicture_buffer_object_count = 0;
1266
1267         int new_count = 2 * vgl->region_count;
1268         vgl->subpicture_buffer_object = realloc_or_free(vgl->subpicture_buffer_object, new_count * sizeof(GLuint));
1269         if (!vgl->subpicture_buffer_object) {
1270             vlc_gl_Unlock(vgl->gl);
1271             return VLC_ENOMEM;
1272         }
1273
1274         vgl->subpicture_buffer_object_count = new_count;
1275         vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1276     }
1277 #endif
1278
1279     glActiveTexture(GL_TEXTURE0 + 0);
1280     glClientActiveTexture(GL_TEXTURE0 + 0);
1281     for (int i = 0; i < vgl->region_count; i++) {
1282         gl_region_t *glr = &vgl->region[i];
1283         const GLfloat vertexCoord[] = {
1284             glr->left,  glr->top,
1285             glr->left,  glr->bottom,
1286             glr->right, glr->top,
1287             glr->right, glr->bottom,
1288         };
1289         const GLfloat textureCoord[] = {
1290             0.0, 0.0,
1291             0.0, glr->tex_height,
1292             glr->tex_width, 0.0,
1293             glr->tex_width, glr->tex_height,
1294         };
1295
1296         glBindTexture(GL_TEXTURE_2D, glr->texture);
1297         if (vgl->program[1]) {
1298 #ifdef SUPPORTS_SHADERS
1299             vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1300
1301             vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i]);
1302             vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1303             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1304             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, 0);
1305
1306             vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i + 1]);
1307             vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1308             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1309             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1310
1311             // Subpictures have the correct orientation:
1312             vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[1], "RotationMatrix"), 1, GL_FALSE, identity);
1313 #endif
1314         } else {
1315 #ifdef SUPPORTS_FIXED_PIPELINE
1316             glEnableClientState(GL_VERTEX_ARRAY);
1317             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1318             glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1319             glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1320             glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1321 #endif
1322         }
1323
1324         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1325
1326         if (!vgl->program[1]) {
1327 #ifdef SUPPORTS_FIXED_PIPELINE
1328             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1329             glDisableClientState(GL_VERTEX_ARRAY);
1330 #endif
1331         }
1332     }
1333     glDisable(GL_BLEND);
1334 #ifdef SUPPORTS_FIXED_PIPELINE
1335     glDisable(GL_TEXTURE_2D);
1336 #endif
1337
1338     /* Display */
1339     vlc_gl_Swap(vgl->gl);
1340
1341     vlc_gl_Unlock(vgl->gl);
1342     return VLC_SUCCESS;
1343 }
1344